File tree Expand file tree Collapse file tree 2 files changed +113
-0
lines changed
packages/svelte/tests/runtime-runes/samples/async-effect-pending Expand file tree Collapse file tree 2 files changed +113
-0
lines changed Original file line number Diff line number Diff line change 1+ import { tick } from 'svelte' ;
2+ import { test } from '../../test' ;
3+
4+ export default test ( {
5+ async test ( { assert, target } ) {
6+ const [ increment , shift ] = target . querySelectorAll ( 'button' ) ;
7+
8+ shift . click ( ) ;
9+ shift . click ( ) ;
10+ shift . click ( ) ;
11+
12+ await tick ( ) ;
13+ assert . htmlEqual (
14+ target . innerHTML ,
15+ `
16+ <button>increment</button>
17+ <button>shift</button>
18+ <p>0</p>
19+ <p>0</p>
20+ <p>0</p>
21+ <p>pending: 0</p>
22+ `
23+ ) ;
24+
25+ increment . click ( ) ;
26+ await tick ( ) ;
27+ assert . htmlEqual (
28+ target . innerHTML ,
29+ `
30+ <button>increment</button>
31+ <button>shift</button>
32+ <p>0</p>
33+ <p>0</p>
34+ <p>0</p>
35+ <p>pending: 3</p>
36+ `
37+ ) ;
38+
39+ shift . click ( ) ;
40+ await tick ( ) ;
41+ assert . htmlEqual (
42+ target . innerHTML ,
43+ `
44+ <button>increment</button>
45+ <button>shift</button>
46+ <p>0</p>
47+ <p>0</p>
48+ <p>0</p>
49+ <p>pending: 2</p>
50+ `
51+ ) ;
52+
53+ shift . click ( ) ;
54+ await tick ( ) ;
55+ assert . htmlEqual (
56+ target . innerHTML ,
57+ `
58+ <button>increment</button>
59+ <button>shift</button>
60+ <p>0</p>
61+ <p>0</p>
62+ <p>0</p>
63+ <p>pending: 1</p>
64+ `
65+ ) ;
66+
67+ shift . click ( ) ;
68+ await tick ( ) ;
69+ assert . htmlEqual (
70+ target . innerHTML ,
71+ `
72+ <button>increment</button>
73+ <button>shift</button>
74+ <p>1</p>
75+ <p>1</p>
76+ <p>1</p>
77+ <p>pending: 0</p>
78+ `
79+ ) ;
80+ }
81+ } ) ;
Original file line number Diff line number Diff line change 1+ <script >
2+ let value = $state (0 );
3+ let deferreds = [];
4+
5+ function push (value ) {
6+ const deferred = Promise .withResolvers ();
7+ deferreds .push ({ value, deferred });
8+ return deferred .promise ;
9+ }
10+
11+ function shift () {
12+ const d = deferreds .shift ();
13+ d? .deferred .resolve (d .value );
14+ }
15+ < / script>
16+
17+ < button onclick= {() => value++ }> increment< / button>
18+ < button onclick= {() => shift ()}> shift< / button>
19+
20+ < svelte: boundary>
21+ < p> {await push (value)}< / p>
22+ < p> {await push (value)}< / p>
23+ < p> {await push (value)}< / p>
24+
25+ < p> pending: {$effect .pending ()}< / p>
26+
27+ {#snippet pending ()}
28+ < p> loading... < / p>
29+ {/ snippet}
30+ < / svelte: boundary>
31+
32+
You can’t perform that action at this time.
0 commit comments