import produce from 'immer' ; import { isAsyncAction , isSimpleSyncAction } from '../../src/impl' ; import { DecoratedActions , StoreActions } from '../../src/types' ; function immerifyEffects < S , Ctx extends { state : S ; s : S ; } > ( effects ) { return ( ( ctx ) => produce ( ctx . state , ( state ) => { effects ( { ... ctx , state , s : state } ) ; } ) ) as ( ctx : Ctx ) => S ; } export function immerizeActions < S , A extends DecoratedActions , P > ( actions ) { const keys = Object . keys ( actions ) ; return keys . reduce ( ( acc , actionName ) => { const baseAction = actions [ actionName ] as any ; if ( isAsyncAction ( baseAction ) ) { const newAction = { ... baseAction } ; const effects = [ 'effects' , 'preEffects' , 'errorEffects' , 'optimisticEffects' ] ; effects . forEach ( ( effects ) => { if ( baseAction [ effects ] ) { newAction [ effects ] = immerifyEffects ( baseAction [ effects ] ) ; } } ) ; acc [ actionName ] = newAction ; } else if ( isSimpleSyncAction ( baseAction ) ) { acc [ actionName ] = immerifyEffects ( baseAction as any ) ; } else { const newAction = { ... baseAction } ; const effects = [ 'effects' ] ; effects . forEach ( ( effects ) => { if ( baseAction [ effects ] ) { newAction [ effects ] = immerifyEffects ( baseAction [ effects ] ) ; } } ) ; acc [ actionName ] = newAction ; } return acc ; } , { } as StoreActions < S , A , P , S > ) ; }