import { NgZone , ɵivyEnabled } from '@angular/core' ; import { Store } from '@ngxs/store' ; import { DispatchOptions } from '../internals/internals' ; import { getNgZone , getStore } from '../internals/static-injector' ; import { distributeActions } from '../internals/distribute-actions' ; import { createActionCompleter } from '../internals/action-completer' ; import { ensureLocalInjectorCaptured , localInject } from '../internals/decorator-injector-adapter' ; const defaultOptions = { cancelUncompleted : false } ; export function Dispatch ( options = defaultOptions ) { return ( target , propertyKey , descriptor ? ) => { let originalValue ; const actionCompleter = createActionCompleter ( options . cancelUncompleted ! ) ; function wrapped ( this ) { if ( actionCompleter !== null ) { actionCompleter . cancelPreviousAction ( ) ; } const store = localInject ( this , Store ) || getStore ( ) ; const ngZone = localInject ( this , NgZone ) || getNgZone ( ) ; const wrapped = originalValue . apply ( this , arguments ) ; return ngZone . runOutsideAngular ( ( ) => distributeActions ( store , ngZone , wrapped , actionCompleter ) ) ; } if ( typeof descriptor ?. value === 'function' ) { originalValue = descriptor . value ! ; descriptor . value = wrapped ; } else { Object . defineProperty ( target , propertyKey , { set : value => ( originalValue = value ) , get : ( ) => wrapped } ) ; } if ( ɵivyEnabled ) { ensureLocalInjectorCaptured ( target ) ; } } ; }