- 
          
 - 
                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 10 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 | 
|---|---|---|
| 
          
            
          
           | 
    @@ -27,6 +27,9 @@ export const LEGACY_PROPS = Symbol('legacy props'); | |
| export const LOADING_ATTR_SYMBOL = Symbol(''); | ||
| export const PROXY_PATH_SYMBOL = Symbol('proxy path'); | ||
| 
     | 
||
| // allow users to ignore aborted signal errors if `reason.stale` | ||
| export const STALE_REACTION = { stale: true }; | ||
                
       | 
||
| 
     | 
||
| export const ELEMENT_NODE = 1; | ||
| export const TEXT_NODE = 3; | ||
| export const COMMENT_NODE = 8; | ||
| 
          
            
          
           | 
    ||
| 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,26 @@ | ||
| 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', { stale: true }]); | ||
| } | ||
| 
     | 
||
| 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', { stale: true }]); | ||
| } | ||
| }); | 
| 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); | ||
| } | ||
| 
     | 
||
| 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} | 
Uh oh!
There was an error while loading. Please reload this page.