|
| 1 | +import { inject, Injectable, ɵisPromise } from '@angular/core'; |
| 2 | +import { |
| 3 | + defaultIfEmpty, |
| 4 | + finalize, |
| 5 | + from, |
| 6 | + isObservable, |
| 7 | + mergeMap, |
| 8 | + type Observable, |
| 9 | + of, |
| 10 | + takeUntil |
| 11 | +} from 'rxjs'; |
| 12 | +import type { ɵActionOptions } from '@ngxs/store/internals'; |
| 13 | + |
| 14 | +import { InternalActions } from '../actions-stream'; |
| 15 | +import { ofActionDispatched } from '../operators/of-action'; |
| 16 | +import { StateContextFactory } from './state-context-factory'; |
| 17 | +import type { StateContext } from '../symbols'; |
| 18 | + |
| 19 | +@Injectable({ providedIn: 'root' }) |
| 20 | +export class InternalActionHandlerFactory { |
| 21 | + private readonly _actions = inject(InternalActions); |
| 22 | + private readonly _stateContextFactory = inject(StateContextFactory); |
| 23 | + |
| 24 | + createActionHandler( |
| 25 | + path: string, |
| 26 | + handlerFn: (ctx: StateContext<any>, action: any) => any, |
| 27 | + options: ɵActionOptions |
| 28 | + ): (action: any) => Observable<any> { |
| 29 | + const { dispatched$ } = this._actions; |
| 30 | + |
| 31 | + return (action: any) => { |
| 32 | + const stateContext = this._stateContextFactory.createStateContext(path); |
| 33 | + |
| 34 | + let result = handlerFn(stateContext, action); |
| 35 | + |
| 36 | + // We need to use `isPromise` instead of checking whether |
| 37 | + // `result instanceof Promise`. In zone.js patched environments, `global.Promise` |
| 38 | + // is the `ZoneAwarePromise`. Some APIs, which are likely not patched by zone.js |
| 39 | + // for certain reasons, might not work with `instanceof`. For instance, the dynamic |
| 40 | + // import returns a native promise (not a `ZoneAwarePromise`), causing this check to |
| 41 | + // be falsy. |
| 42 | + if (ɵisPromise(result)) { |
| 43 | + result = from(result); |
| 44 | + } |
| 45 | + |
| 46 | + if (isObservable(result)) { |
| 47 | + result = result.pipe( |
| 48 | + mergeMap(value => (ɵisPromise(value) || isObservable(value) ? value : of(value))), |
| 49 | + // If this observable has completed without emitting any values, |
| 50 | + // we wouldn't want to complete the entire chain of actions. |
| 51 | + // If any observable completes, then the action will be canceled. |
| 52 | + // For instance, if any action handler had a statement like |
| 53 | + // `handler(ctx) { return EMPTY; }`, then the action would be canceled. |
| 54 | + // See https://github.com/ngxs/store/issues/1568 |
| 55 | + // Note that we actually don't care about the return type; we only care |
| 56 | + // about emission, and thus `undefined` is applicable by the framework. |
| 57 | + defaultIfEmpty(undefined) |
| 58 | + ); |
| 59 | + |
| 60 | + if (options.cancelUncompleted) { |
| 61 | + const canceled = dispatched$.pipe(ofActionDispatched(action)); |
| 62 | + result = result.pipe(takeUntil(canceled)); |
| 63 | + } |
| 64 | + |
| 65 | + result = result.pipe( |
| 66 | + // Note that we use the `finalize` operator only when the action handler |
| 67 | + // explicitly returns an observable (or a promise) to wait for. This means |
| 68 | + // the action handler is written in a "fire & wait" style. If the handler’s |
| 69 | + // result is unsubscribed (either because the observable has completed or |
| 70 | + // it was unsubscribed by `takeUntil` due to a new action being dispatched), |
| 71 | + // we prevent writing to the state context. |
| 72 | + finalize(() => { |
| 73 | + if (typeof ngDevMode !== 'undefined' && ngDevMode) { |
| 74 | + function noopAndWarn() { |
| 75 | + console.warn( |
| 76 | + `"${action}" attempted to change the state, but the change was ignored because state updates are not allowed after the action handler has completed.` |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + stateContext.setState = noopAndWarn; |
| 81 | + stateContext.patchState = noopAndWarn; |
| 82 | + } else { |
| 83 | + stateContext.setState = noop; |
| 84 | + stateContext.patchState = noop; |
| 85 | + } |
| 86 | + }) |
| 87 | + ); |
| 88 | + } else { |
| 89 | + // If the action handler is synchronous and returns nothing (`void`), we |
| 90 | + // still have to convert the result to a synchronous observable. |
| 91 | + result = of(undefined); |
| 92 | + } |
| 93 | + |
| 94 | + return result; |
| 95 | + }; |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +// This is used to replace `setState` and `patchState` once the action |
| 100 | +// handler has been unsubscribed or completed, to prevent writing |
| 101 | +// to the state context. |
| 102 | +function noop() {} |
0 commit comments