From 05775757a715f4904efef80d067aa05d232652ad Mon Sep 17 00:00:00 2001 From: Artur Date: Mon, 18 Nov 2024 18:46:32 +0000 Subject: [PATCH] refactor(store): replace `exhaustMap` (#2254) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In this commit, we replace `exhaustMap` with `mergeMap`. `exhaustMap` might not even be used by developers who consume NGXS, so there’s no reason to bundle it. `exhaustMap` would only make sense if the inner observable emitted asynchronously, but the inner observable either completes, synchronously emits `getValue()`, or throws an error. --- CHANGELOG.md | 1 + packages/store/src/internal/dispatcher.ts | 37 ++++++++++++----------- 2 files changed, 20 insertions(+), 18 deletions(-) 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() + ); } }