Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(store): replace exhaustMap #2254

Merged
merged 2 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading