mirror of https://github.com/sveltejs/svelte
parent
5839bd89a9
commit
5180d7fafa
@ -0,0 +1,51 @@
|
||||
import { tick } from 'svelte';
|
||||
import { test } from '../../test';
|
||||
|
||||
export default test({
|
||||
async test({ assert, target }) {
|
||||
const [x, y, resolve] = target.querySelectorAll('button');
|
||||
|
||||
x.click();
|
||||
await tick();
|
||||
assert.htmlEqual(
|
||||
target.innerHTML,
|
||||
`
|
||||
<button>x</button>
|
||||
<button>y++</button>
|
||||
<button>resolve</button>
|
||||
<h1>WORLD</h1>
|
||||
`
|
||||
);
|
||||
|
||||
y.click();
|
||||
await tick();
|
||||
// the new branch reads `upper` — a derived owned by the pending batch — as
|
||||
// a new dependency. The two batches entangle, so the new branch is held
|
||||
// back until the async work completes (rather than rendering with the
|
||||
// derived's pre-write value, 'WORLD')
|
||||
assert.htmlEqual(
|
||||
target.innerHTML,
|
||||
`
|
||||
<button>x</button>
|
||||
<button>y++</button>
|
||||
<button>resolve</button>
|
||||
<h1>WORLD</h1>
|
||||
`
|
||||
);
|
||||
|
||||
resolve.click();
|
||||
await tick();
|
||||
// both branches commit together, fully consistent
|
||||
assert.htmlEqual(
|
||||
target.innerHTML,
|
||||
`
|
||||
<button>x</button>
|
||||
<button>y++</button>
|
||||
<button>resolve</button>
|
||||
<h1>UNIVERSE</h1>
|
||||
universe
|
||||
<p>UNIVERSE</p>
|
||||
`
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -0,0 +1,29 @@
|
||||
<script>
|
||||
let x = $state({ x: 'world' });
|
||||
let y = $state(0);
|
||||
let deferred = [];
|
||||
|
||||
const upper = $derived(x.x.toUpperCase());
|
||||
|
||||
function delay(s) {
|
||||
const d = Promise.withResolvers();
|
||||
deferred.push(() => d.resolve(s));
|
||||
return d.promise;
|
||||
}
|
||||
</script>
|
||||
|
||||
<button onclick={() => (x = { x: 'universe' })}>x</button>
|
||||
|
||||
<button onclick={() => y++}>y++</button>
|
||||
|
||||
<button onclick={() => deferred.shift()()}>resolve</button>
|
||||
|
||||
<h1>{upper}</h1>
|
||||
|
||||
{#if x.x === 'universe'}
|
||||
{await delay(x.x)}
|
||||
{/if}
|
||||
|
||||
{#if y > 0}
|
||||
<p>{upper}</p>
|
||||
{/if}
|
||||
Loading…
Reference in new issue