-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
feat: add getAbortSignal()
#16266
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
Merged
Merged
feat: add getAbortSignal()
#16266
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
bc10186
WIP getAbortSignal
Rich-Harris edec6cd
add test
Rich-Harris c6b475c
regenerate
Rich-Harris a1250ed
add error code
Rich-Harris d754aad
changeset
Rich-Harris 02835be
regenerate
Rich-Harris 57144a0
try this
Rich-Harris 480f55d
{ stale: true }
Rich-Harris f0aac15
fix test
Rich-Harris c4a948d
lint
Rich-Harris 9312594
abort synchronously in SSR
Rich-Harris 5e056cd
make STALE_REACTION a `StaleReactionError extends Error`
Rich-Harris 61c0eaa
make non-optional
Rich-Harris 1b5857f
Update packages/svelte/src/internal/server/abort-signal.js
Rich-Harris 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,5 @@ | ||
| --- | ||
| 'svelte': minor | ||
| --- | ||
|
|
||
| feat: add `getAbortSignal()` |
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
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
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,13 @@ | ||
| import { STALE_REACTION } from '#client/constants'; | ||
|
|
||
| /** @type {AbortController | null} */ | ||
| export let controller = null; | ||
|
|
||
| export function abort() { | ||
| controller?.abort(STALE_REACTION); | ||
| controller = null; | ||
| } | ||
|
|
||
| export function getAbortSignal() { | ||
| return (controller ??= new AbortController()).signal; | ||
| } |
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
34 changes: 34 additions & 0 deletions
34
packages/svelte/tests/runtime-runes/samples/get-abort-signal/_config.js
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,34 @@ | ||
| import { test } from '../../test'; | ||
|
|
||
| export default test({ | ||
| html: `<button>increment</button><p>loading...</p>`, | ||
|
|
||
| async test({ assert, target, variant, logs }) { | ||
| await new Promise((f) => setTimeout(f, 50)); | ||
|
|
||
| if (variant === 'hydrate') { | ||
| assert.deepEqual(logs, [ | ||
| 'aborted', | ||
| 'StaleReactionError', | ||
| 'The reaction that called `getAbortSignal()` was re-run or destroyed' | ||
| ]); | ||
| } | ||
|
|
||
| logs.length = 0; | ||
|
|
||
| const [button] = target.querySelectorAll('button'); | ||
|
|
||
| await new Promise((f) => setTimeout(f, 50)); | ||
| assert.htmlEqual(target.innerHTML, '<button>increment</button><p>0</p>'); | ||
|
|
||
| button.click(); | ||
| await new Promise((f) => setTimeout(f, 50)); | ||
| assert.htmlEqual(target.innerHTML, '<button>increment</button><p>2</p>'); | ||
|
|
||
| assert.deepEqual(logs, [ | ||
| 'aborted', | ||
| 'StaleReactionError', | ||
| 'The reaction that called `getAbortSignal()` was re-run or destroyed' | ||
| ]); | ||
| } | ||
| }); |
33 changes: 33 additions & 0 deletions
33
packages/svelte/tests/runtime-runes/samples/get-abort-signal/main.svelte
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,33 @@ | ||
| <script> | ||
| import { getAbortSignal } from 'svelte'; | ||
|
|
||
| let count = $state(0); | ||
|
|
||
| let delayed_count = $derived.by(async () => { | ||
| let c = count; | ||
|
|
||
| const signal = getAbortSignal(); | ||
|
|
||
| await new Promise((f) => setTimeout(f)); | ||
|
|
||
| if (signal.aborted) { | ||
| console.log('aborted', signal.reason.name, signal.reason.message); | ||
| } | ||
|
|
||
| return c; | ||
| }); | ||
| </script> | ||
|
|
||
| <button onclick={async () => { | ||
| count += 1; | ||
| await Promise.resolve(); | ||
| count += 1; | ||
| }}>increment</button> | ||
|
|
||
| {#await delayed_count} | ||
| <p>loading...</p> | ||
| {:then count} | ||
| <p>{count}</p> | ||
| {:catch error} | ||
| {console.log('this should never be rendered')} | ||
| {/await} |
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.
nasty. effective. love it