Skip to content

Commit

Permalink
refactor(store): replace exhaustMap (#2254)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
arturovt authored Nov 18, 2024
1 parent a6757af commit 0577575
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 19 additions & 18 deletions packages/store/src/internal/dispatcher.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -97,22 +97,23 @@ export class InternalDispatcher {
private createDispatchObservable(
actionResult$: Observable<ActionContext>
): 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()
);
}
}

Expand Down

0 comments on commit 0577575

Please sign in to comment.