import { NgZone , ɵisPromise } from '@angular/core' ; import { Store } from '@ngxs/store' ; import { isObservable , Observable } from 'rxjs' ; import { takeUntil } from 'rxjs/operators' ; import { ActionCompleter } from './action-completer' ; import { Wrapped , ActionOrActions } from './internals' ; function dispatch ( store , ngZone , actionOrActions ) { ngZone . run ( ( ) => store . dispatch ( actionOrActions ) ) ; } function unwrapObservable ( store , ngZone , wrapped , actionCompleter ) { if ( actionCompleter !== null ) { wrapped = wrapped . pipe ( takeUntil ( actionCompleter . cancelUncompleted$ ) ) ; } wrapped . subscribe ( { next : actionOrActions => dispatch ( store , ngZone , actionOrActions ) } ) ; return wrapped ; } function unwrapPromise ( store , ngZone , wrapped ) { return wrapped . then ( actionOrActions => { dispatch ( store , ngZone , actionOrActions ) ; return actionOrActions ; } ) ; } /**
 * As dispatchers can have different return types, they can be either
 * synchronous or asynchronous, we have to determine its return type
 * and unwrap `Promise` or `Observable`
 */ export function distributeActions ( store , ngZone , wrapped , actionCompleter ) { if ( ɵisPromise ( wrapped ) ) { return unwrapPromise ( store , ngZone , wrapped ) ; } else if ( isObservable ( wrapped ) ) { return unwrapObservable ( store , ngZone , wrapped , actionCompleter ) ; } else { dispatch ( store , ngZone , wrapped ) ; return wrapped ; } }