-
Notifications
You must be signed in to change notification settings - Fork 405
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
feat(store): expose abort controller on state context #2244
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,7 +25,8 @@ import { | |
mergeMap, | ||
takeUntil, | ||
finalize, | ||
Observable | ||
Observable, | ||
fromEvent | ||
} from 'rxjs'; | ||
|
||
import { NgxsConfig } from '../symbols'; | ||
|
@@ -345,10 +346,12 @@ export class StateFactory implements OnDestroy { | |
const { dispatched$ } = this._actions; | ||
for (const actionType of Object.keys(actions)) { | ||
const actionHandlers = actions[actionType].map(actionMeta => { | ||
const cancelable = !!actionMeta.options.cancelUncompleted; | ||
const abortController = new AbortController(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If cancellation is enabled we would retain a reference to the previous |
||
const abortSignal = abortController.signal; | ||
const cancellable = !!actionMeta.options.cancelUncompleted; | ||
|
||
return (action: any) => { | ||
const stateContext = this._stateContextFactory.createStateContext(path); | ||
const stateContext = this._stateContextFactory.createStateContext(path, abortSignal); | ||
|
||
let result = instance[actionMeta.fn](stateContext, action); | ||
|
||
|
@@ -384,12 +387,16 @@ export class StateFactory implements OnDestroy { | |
defaultIfEmpty(undefined) | ||
); | ||
|
||
if (cancelable) { | ||
const notifier$ = dispatched$.pipe(ofActionDispatched(action)); | ||
result = result.pipe(takeUntil(notifier$)); | ||
if (cancellable) { | ||
const cancelled = dispatched$.pipe(ofActionDispatched(action)); | ||
result = result.pipe(takeUntil(cancelled)); | ||
} | ||
|
||
const aborted = fromEvent(abortSignal, 'abort'); | ||
|
||
result = result.pipe( | ||
takeUntil(aborted), | ||
|
||
// Note that we use the `finalize` operator only when the action handler | ||
// returns an observable. If the action handler is synchronous, we do not | ||
// need to set the state context functions to `noop`, as the absence of a | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that the
abortSignal
could be available for lifecycle hooks.For example, when we have a local state, the abort signal for the lifecycle hooks would be triggered when the state should be unloaded.