diff --git a/CHANGELOG.md b/CHANGELOG.md index f931b3de2..b01e5bc59 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ $ npm install @ngxs/store@dev - Refactor: Allow tree-shaking of dev-only code [#2259](https://github.com/ngxs/store/pull/2259) - Fix(store): Run plugins in injection context [#2256](https://github.com/ngxs/store/pull/2256) - Fix(websocket-plugin): Do not dispatch action when root injector is destroyed [#2257](https://github.com/ngxs/store/pull/2257) +- Refactor(store): Replace `exhaustMap` [#2254](https://github.com/ngxs/store/pull/2254) - Refactor(store): Tree-shake development options token [#2260](https://github.com/ngxs/store/pull/2260) ### 18.1.5 2024-11-12 diff --git a/packages/store/src/internal/dispatcher.ts b/packages/store/src/internal/dispatcher.ts index 364d7a225..7ea1edfbf 100644 --- a/packages/store/src/internal/dispatcher.ts +++ b/packages/store/src/internal/dispatcher.ts @@ -1,6 +1,6 @@ import { inject, Injectable, Injector, NgZone, runInInjectionContext } from '@angular/core'; -import { EMPTY, forkJoin, Observable, of, Subject, throwError } from 'rxjs'; -import { exhaustMap, filter, map, shareReplay, take } from 'rxjs/operators'; +import { forkJoin, Observable, of, Subject, throwError } from 'rxjs'; +import { filter, map, mergeMap, shareReplay, take } from 'rxjs/operators'; import { getActionTypeFromInstance } from '@ngxs/store/plugins'; import { ɵPlainObject, ɵStateStream } from '@ngxs/store/internals'; @@ -97,22 +97,23 @@ export class InternalDispatcher { private createDispatchObservable( actionResult$: Observable ): Observable<ɵPlainObject> { - return actionResult$ - .pipe( - exhaustMap((ctx: ActionContext) => { - switch (ctx.status) { - case ActionStatus.Successful: - // The `createDispatchObservable` function should return the - // state, as its result is utilized by plugins. - return of(this._stateStream.getValue()); - case ActionStatus.Errored: - return throwError(() => ctx.error); - default: - return EMPTY; - } - }) - ) - .pipe(shareReplay()); + return actionResult$.pipe( + mergeMap((ctx: ActionContext) => { + switch (ctx.status) { + case ActionStatus.Successful: + // The `createDispatchObservable` function should return the + // state, as its result is used by plugins. + return of(this._stateStream.getValue()); + case ActionStatus.Errored: + return throwError(() => ctx.error); + default: + // Once dispatched or canceled, we complete it immediately because + // `dispatch()` should emit (or error, or complete) as soon as it succeeds or fails. + return of(); + } + }), + shareReplay() + ); } }