- 
          
 - 
                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
  
    feat: add getAbortSignal()
  
  #16266
                      Changes from 12 commits
bc10186
              edec6cd
              c6b475c
              a1250ed
              d754aad
              02835be
              57144a0
              480f55d
              f0aac15
              c4a948d
              9312594
              5e056cd
              61c0eaa
              1b5857f
              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 | 
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| 'svelte': minor | ||
| --- | ||
| 
     | 
||
| feat: add `getAbortSignal()` | 
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 
          
            
          
           | 
    @@ -22,7 +22,8 @@ import { | |||||||||||||
| ROOT_EFFECT, | ||||||||||||||
| LEGACY_DERIVED_PROP, | ||||||||||||||
| DISCONNECTED, | ||||||||||||||
| EFFECT_IS_UPDATING | ||||||||||||||
| EFFECT_IS_UPDATING, | ||||||||||||||
| STALE_REACTION | ||||||||||||||
| } from './constants.js'; | ||||||||||||||
| import { flush_tasks } from './dom/task.js'; | ||||||||||||||
| import { internal_set, old_values } from './reactivity/sources.js'; | ||||||||||||||
| 
          
            
          
           | 
    @@ -276,6 +277,11 @@ export function update_reaction(reaction) { | |||||||||||||
| 
     | 
||||||||||||||
| reaction.f |= EFFECT_IS_UPDATING; | ||||||||||||||
| 
     | 
||||||||||||||
| if (reaction.ac !== null) { | ||||||||||||||
| reaction.ac?.abort(STALE_REACTION); | ||||||||||||||
| reaction.ac = null; | ||||||||||||||
| } | ||||||||||||||
| 
         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. 
        Suggested change
       
    
 I don't think the check is buying us anything here right? 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. it's saving us from writing   | 
||||||||||||||
| 
     | 
||||||||||||||
| try { | ||||||||||||||
| var result = /** @type {Function} */ (0, reaction.fn)(); | ||||||||||||||
| var deps = reaction.deps; | ||||||||||||||
| 
          
            
          
           | 
    ||||||||||||||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import { STALE_REACTION } from '#client/constants'; | ||
| 
     | 
||
| /** @type {AbortController | null} */ | ||
| export let controller = null; | ||
| 
     | 
||
| export function abort() { | ||
| if (controller !== null) { | ||
| controller.abort(STALE_REACTION); | ||
| controller = null; | ||
| } | ||
                
      
                  Rich-Harris marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| } | ||
| 
     | 
||
| export function getAbortSignal() { | ||
| return (controller ??= new AbortController()).signal; | ||
| } | ||
| 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' | ||
| ]); | ||
| } | ||
| }); | 
| 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} | 
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