import type { Middleware } from 'redux' ; import { getTimeMeasureUtils } from './utils' ; type EntryProcessor = ( key : string , value : any ) => any ; const isProduction = process . env . NODE_ENV === 'production' ; const prefix = 'Invariant failed' ; function invariant ( condition , message ? ) { if ( condition ) { return ; } if ( isProduction ) { throw new Error ( prefix ) ; } throw new Error ( `${ prefix }: ${ message || '' }` ) ; } function stringify ( obj , serializer ? , indent ? , decycler ? ) { return JSON . stringify ( obj , getSerialize ( serializer , decycler ) , indent ) ; } function getSerialize ( serializer ? , decycler ? ) { let stack = [ ] , keys = [ ] ; if ( ! decycler ) decycler = function ( _ , value ) { if ( stack [ 0 ] === value ) return '[Circular ~]' ; return ( '[Circular ~.' + keys . slice ( 0 , stack . indexOf ( value ) ) . join ( '.' ) + ']' ) ; } ; return function ( this , key , value ) { if ( stack . length > 0 ) { var thisPos = stack . indexOf ( this ) ; ~ thisPos ? stack . splice ( thisPos + 1 ) : stack . push ( this ) ; ~ thisPos ? keys . splice ( thisPos , Infinity , key ) : keys . push ( key ) ; if ( ~ stack . indexOf ( value ) ) value = decycler ! . call ( this , key , value ) ; } else stack . push ( value ) ; return serializer == null ? value : serializer . call ( this , key , value ) ; } ; } public export function isImmutableDefault ( value ) { return ( typeof value !== 'object' || value === null || typeof value === 'undefined' || Object . isFrozen ( value ) ) ; } export function trackForMutations ( isImmutable , ignorePaths , obj ) { const trackedProperties = trackProperties ( isImmutable , ignorePaths , obj ) ; return { detectMutations ( ) { return detectMutations ( isImmutable , ignorePaths , trackedProperties , obj ) ; } , } ; } interface TrackedProperty { value : any ; children : Record < string , any > ; } function trackProperties ( isImmutable , ignorePaths = [ ] , obj , path = '' ) { const tracked = { value : obj } ; if ( ! isImmutable ( obj ) ) { tracked . children = { } ; for ( const key in obj ) { const childPath = path ? path + '.' + key : key ; if ( ignorePaths . length && ignorePaths . indexOf ( childPath ) !== - 1 ) { continue ; } tracked . children [ key ] = trackProperties ( isImmutable , ignorePaths , obj [ key ] , childPath ) ; } } return tracked as TrackedProperty ; } type IgnorePaths = readonly string [ ] ; function detectMutations ( isImmutable , ignorePaths = [ ] , trackedProperty , obj , sameParentRef = false , path = '' ) { const prevObj = trackedProperty ? trackedProperty . value : undefined ; const sameRef = prevObj === obj ; if ( sameParentRef && ! sameRef && ! Number . isNaN ( obj ) ) { return { wasMutated : true , path } ; } if ( isImmutable ( prevObj ) || isImmutable ( obj ) ) { return { wasMutated : false } ; } const keysToDetect = { } ; for ( let key in trackedProperty . children ) { keysToDetect [ key ] = true ; } for ( let key in obj ) { keysToDetect [ key ] = true ; } for ( let key in keysToDetect ) { const childPath = path ? path + '.' + key : key ; if ( ignorePaths . length && ignorePaths . indexOf ( childPath ) !== - 1 ) { continue ; } const result = detectMutations ( isImmutable , ignorePaths , trackedProperty . children [ key ] , obj [ key ] , sameRef , childPath ) ; if ( result . wasMutated ) { return result ; } } return { wasMutated : false } ; } type IsImmutableFunc = ( value : any ) => boolean ; public export interface ImmutableStateInvariantMiddlewareOptions { /**
      Callback function to check if a value is considered to be immutable.
      This function is applied recursively to every value contained in the state.
      The default implementation will return true for primitive types
      (like numbers, strings, booleans, null and undefined).
     */ isImmutable ? : IsImmutableFunc ; /**
      An array of dot-separated path strings that match named nodes from
      the root state to ignore when checking for immutability.
      Defaults to undefined
     */ ignoredPaths ? : string [ ] ; /** Print a warning if checks take longer than N ms. Default: 32ms */ warnAfter ? : number ; ignore ? : string [ ] ; } param options public export function createImmutableStateInvariantMiddleware ( options = { } ) { if ( process . env . NODE_ENV === 'production' ) { return ( ) => ( next ) => ( action ) => next ( action ) ; } let { isImmutable = isImmutableDefault , ignoredPaths , warnAfter = 32 , ignore , } = options ; ignoredPaths = ignoredPaths || ignore ; const track = trackForMutations . bind ( null , isImmutable , ignoredPaths ) ; return ( { getState } ) => { let state = getState ( ) ; let tracker = track ( state ) ; let result ; return ( next ) => ( action ) => { const measureUtils = getTimeMeasureUtils ( warnAfter , 'ImmutableStateInvariantMiddleware' ) ; measureUtils . measureTime ( ( ) => { state = getState ( ) ; result = tracker . detectMutations ( ) ; tracker = track ( state ) ; invariant ( ! result . wasMutated , `A state mutation was detected between dispatches, in the path '${ result . path || '' }'.  This may cause incorrect behavior. (https://redux.js.org/style-guide/style-guide#do-not-mutate-state)` ) ; } ) ; const dispatchedAction = next ( action ) ; measureUtils . measureTime ( ( ) => { state = getState ( ) ; result = tracker . detectMutations ( ) ; tracker = track ( state ) ; result . wasMutated && invariant ( ! result . wasMutated , `A state mutation was detected inside a dispatch, in the path: ${ result . path || '' }. Take a look at the reducer(s) handling the action ${ stringify ( action ) }. (https://redux.js.org/style-guide/style-guide#do-not-mutate-state)` ) ; } ) ; measureUtils . warnIfExceeded ( ) ; return dispatchedAction ; } ; } ; }