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

feat(store): expose abort controller on state context #2244

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

arturovt
Copy link
Member

@arturovt arturovt commented Nov 2, 2024

No description provided.

Copy link

nx-cloud bot commented Nov 2, 2024

☁️ Nx Cloud Report

CI is running/has finished running commands for commit 8ae35ba. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this CI Pipeline Execution


✅ Successfully ran 4 targets

Sent with 💌 from NxCloud.

Copy link

pkg-pr-new bot commented Nov 3, 2024

Open in Stackblitz

@ngxs/devtools-plugin

yarn add https://pkg.pr.new/@ngxs/devtools-plugin@2244.tgz

@ngxs/router-plugin

yarn add https://pkg.pr.new/@ngxs/router-plugin@2244.tgz

@ngxs/hmr-plugin

yarn add https://pkg.pr.new/@ngxs/hmr-plugin@2244.tgz

@ngxs/form-plugin

yarn add https://pkg.pr.new/@ngxs/form-plugin@2244.tgz

@ngxs/storage-plugin

yarn add https://pkg.pr.new/@ngxs/storage-plugin@2244.tgz

@ngxs/store

yarn add https://pkg.pr.new/@ngxs/store@2244.tgz

@ngxs/websocket-plugin

yarn add https://pkg.pr.new/@ngxs/websocket-plugin@2244.tgz

commit: 8ae35ba

Copy link

bundlemon bot commented Nov 3, 2024

BundleMon

Files updated (2)
Status Path Size Limits
fesm2022/ngxs-store-internals.mjs
11.64KB (+348B +3.01%) 13KB / +0.5%
fesm2022/ngxs-store.mjs
102.77KB (+268B +0.26%) 103KB / +0.5%
Unchanged files (4)
Status Path Size Limits
fesm2022/ngxs-store-internals-testing.mjs
6.83KB 7KB / +0.5%
fesm2022/ngxs-store-operators.mjs
6.22KB 7KB / +0.5%
fesm2022/ngxs-store-plugins.mjs
2.04KB 3KB / +0.5%
fesm2022/ngxs-store-experimental.mjs
1.4KB 2KB / +0.5%

Total files change +616B +0.46%

Groups updated (2)
Status Path Size Limits
@ngxs/store(esm2022)[gzip]
./esm2022/**/*.mjs
225.07KB (+920B +0.4%) +1%
@ngxs/store(fesm2022)[gzip]
./fesm2022/*.mjs
31.36KB (+236B +0.74%) +1%

Final result: ❌

View report in BundleMon website ➡️


Current branch size history | Target branch size history

Copy link

bundlemon bot commented Nov 3, 2024

BundleMon (NGXS Plugins)

Files updated (1)
Status Path Size Limits
Plugins(fesm2022)[gzip]
hmr-plugin/fesm2022/ngxs-hmr-plugin.mjs
2.64KB (+25B +0.93%) +0.5%
Unchanged files (8)
Status Path Size Limits
Plugins(fesm2022)[gzip]
storage-plugin/fesm2022/ngxs-storage-plugin.m
js
4.15KB +0.5%
Plugins(fesm2022)[gzip]
router-plugin/fesm2022/ngxs-router-plugin.mjs
3.2KB +0.5%
Plugins(fesm2022)[gzip]
websocket-plugin/fesm2022/ngxs-websocket-plug
in.mjs
2.64KB +0.5%
Plugins(fesm2022)[gzip]
form-plugin/fesm2022/ngxs-form-plugin.mjs
2.59KB +0.5%
Plugins(fesm2022)[gzip]
devtools-plugin/fesm2022/ngxs-devtools-plugin
.mjs
2.23KB +0.5%
Plugins(fesm2022)[gzip]
logger-plugin/fesm2022/ngxs-logger-plugin.mjs
2.03KB +0.5%
Plugins(fesm2022)[gzip]
storage-plugin/fesm2022/ngxs-storage-plugin-i
nternals.mjs
875B +0.5%
Plugins(fesm2022)[gzip]
router-plugin/fesm2022/ngxs-router-plugin-int
ernals.mjs
411B +0.5%

Total files change +25B +0.12%

Groups updated (1)
Status Path Size Limits
All Plugins(fesm2022)[gzip]
./-plugin/fesm2022/.mjs
20.73KB (+25B +0.12%) +0.5%

Final result: ❌

View report in BundleMon website ➡️


Current branch size history | Target branch size history

Copy link

bundlemon bot commented Nov 3, 2024

BundleMon (Integration Projects)

Files updated (3)
Status Path Size Limits
Main bundles(Gzip)
hello-world-ng18/dist-integration/browser/mai
n-(hash).js
71.91KB (+99B +0.13%) +1%
Main bundles(Gzip)
hello-world-ng17/dist-integration/main.(hash)
.js
68.72KB (+75B +0.11%) +1%
Main bundles(Gzip)
hello-world-ng16/dist-integration/main.(hash)
.js
67.81KB (+57B +0.08%) +1%

Total files change +231B +0.11%

Final result: ✅

View report in BundleMon website ➡️


Current branch size history | Target branch size history

@@ -99,6 +99,8 @@ export { StateOperator };
* State context provided to the actions in the state.
*/
export interface StateContext<T> {
abortController: AbortController;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
abortController: AbortController;
abortSignal: AbortSignal;

Comment on lines 353 to 356
const stateContext = this._stateContextFactory.createStateContext(
path,
abortController
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const stateContext = this._stateContextFactory.createStateContext(
path,
abortController
);
const stateContext = this._stateContextFactory.createStateContext(
path,
abortSignal
);

Comment on lines 349 to 351
const abortController = new AbortController();
const cancellable = !!actionMeta.options.cancelUncompleted;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const abortController = new AbortController();
const cancellable = !!actionMeta.options.cancelUncompleted;
const abortController = new AbortController();
const abortSignal = abortController.signal;
const cancellable = !!actionMeta.options.cancelUncompleted;

}

const aborted = fromEvent(abortController.signal, 'abort');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const aborted = fromEvent(abortController.signal, 'abort');
const aborted = fromEvent(abortSignal, 'abort');

const root = this._internalStateOperations.getRootStateOperations();

return {
abortController: abortController!,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
abortController: abortController!,
abortSignal: abortSignal!,


/**
* Create the state context
*/
createStateContext<T>(path: string): StateContext<T> {
createStateContext<T>(path: string, abortController?: AbortController): StateContext<T> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
createStateContext<T>(path: string, abortController?: AbortController): StateContext<T> {
createStateContext<T>(path: string, abortSignal?: AbortSignal): StateContext<T> {

@@ -99,6 +99,7 @@ export class LifecycleStateManager implements OnDestroy {
}

private _getStateContext(mappedStore: MappedStore): StateContext<any> {
// Question: abort controller is not gonna be available for lifecycle hooks.
Copy link
Member

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.

@@ -345,10 +346,14 @@ 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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If cancellation is enabled we would retain a reference to the previous abortController for the handler and when the next action hits this handler we would call abortController.abort() on the previous abortController to trigger the cancellation of the previous handler and of any related work that has used the abort signal from the context.
When the abortSignal fires for some work in progress in the handler (like fetch), the handler would throw an error, we should catch this error and not let it propagate to the action error handlers.

Copy link

codeclimate bot commented Nov 8, 2024

Code Climate has analyzed commit 8ae35ba and detected 0 issues on this pull request.

The test coverage on the diff in this pull request is 100.0% (50% is the threshold).

This pull request will bring the total coverage in the repository to 95.4% (0.0% change).

View more on Code Climate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants