-
Notifications
You must be signed in to change notification settings - Fork 181
Add withSignal to reactive action store for per-dispatch cancellation
#1614
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
Open
mcintyre94
wants to merge
1
commit into
reactive-store/no-auto-dispatch
Choose a base branch
from
reactive-store/abort-signal
base: reactive-store/no-auto-dispatch
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| --- | ||
| '@solana/subscribable': minor | ||
| --- | ||
|
|
||
| Add `store.withSignal(signal)` on `ReactiveActionStore` for attaching a caller-provided `AbortSignal` to a dispatch. The method returns a thin wrapper exposing only `dispatch` / `dispatchAsync`; the supplied signal is composed with the store's internal per-dispatch controller via `AbortSignal.any`, so aborting either cancels the in-flight call and surfaces the abort reason on state. The bare `dispatch` / `dispatchAsync` signatures are unchanged — this is additive. | ||
|
|
||
| Two common patterns the wrapper enables: | ||
|
|
||
| - **Per-attempt timeout.** `store.withSignal(AbortSignal.timeout(5_000)).dispatch(args)` — a fresh clock per call. Different call sites can pass different timeouts. | ||
| - **Shared kill switch.** Hold one `AbortController`, bind the wrapper once (`const killable = store.withSignal(killCtrl.signal)`), and use `killable.dispatch(...)` everywhere. Aborting the controller cancels the current call and makes future calls on the wrapper start aborted. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,5 +10,35 @@ export default class BrowserEnvironment extends TestEnvironment { | |
| */ | ||
| this.global.ArrayBuffer = globalThis.ArrayBuffer; | ||
| this.global.Uint8Array = globalThis.Uint8Array; | ||
| /** | ||
| * Polyfill `AbortSignal.any` on jsdom's `AbortSignal` class if missing. jsdom 22 doesn't | ||
| * implement it (added natively in jsdom 24, in Node 20.3, and shipped in all current | ||
| * browsers). Cross-realm replacement of the whole class would break jsdom's internal | ||
| * brand checks elsewhere, so this patches just the static method. | ||
| */ | ||
| const JsdomAbortSignal = this.global.AbortSignal as typeof globalThis.AbortSignal & { | ||
| any?: typeof globalThis.AbortSignal.any; | ||
| }; | ||
| const JsdomAbortController = this.global.AbortController as typeof globalThis.AbortController; | ||
| if (typeof JsdomAbortSignal.any !== 'function') { | ||
| JsdomAbortSignal.any = function any(signals: readonly AbortSignal[]): AbortSignal { | ||
| const controller = new JsdomAbortController(); | ||
| const alreadyAborted = signals.find(s => s.aborted); | ||
| if (alreadyAborted) { | ||
| controller.abort(alreadyAborted.reason); | ||
| return controller.signal; | ||
| } | ||
| for (const inputSignal of signals) { | ||
| inputSignal.addEventListener( | ||
| 'abort', | ||
| () => { | ||
| if (!controller.signal.aborted) controller.abort(inputSignal.reason); | ||
| }, | ||
| { once: true, signal: controller.signal }, | ||
| ); | ||
| } | ||
|
Comment on lines
+24
to
+39
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. Polyfill looks correct. Two small things:
|
||
| return controller.signal; | ||
| }; | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Should we consider bumping jsdom to version 24 in a subsequent PR?