Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/orange-chefs-float.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: ensure async deriveds always get dependencies from thennable
3 changes: 3 additions & 0 deletions packages/svelte/src/internal/client/reactivity/deriveds.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ export function async_derived(fn, location) {

try {
var p = fn();
// Make sure to always access the then property to read any signals
// it might access, so that we track them as dependencies.
if (prev) Promise.resolve(p).catch(() => {}); // avoid unhandled rejection
} catch (error) {
p = Promise.reject(error);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { tick } from 'svelte';
import { test } from '../../test';

export default test({
async test({ assert, target }) {
const [increment, pop] = target.querySelectorAll('button');

increment.click();
await tick();

pop.click();
await tick();

pop.click();
await tick();

assert.htmlEqual(
target.innerHTML,
`
<button>increment</button>
<button>pop</button>
<p>1</p>
`
);

increment.click();
await tick();

pop.click();
await tick();

assert.htmlEqual(
target.innerHTML,
`
<button>increment</button>
<button>pop</button>
<p>2</p>
`
);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

<script>
let count = $state(0);

let deferreds = [];

class X {
constructor(promise) {
this.promise = promise;
}

get then() {
count;

return (resolve) => this.promise.then(() => count).then(resolve)
}
}

function push() {
const deferred = Promise.withResolvers();
deferreds.push(deferred);
return new X(deferred.promise);
}
</script>

<button onclick={() => count += 1}>increment</button>
<button onclick={() => deferreds.pop()?.resolve(count)}>pop</button>

<svelte:boundary>
<p>{await push()}</p>

{#snippet pending()}
<p>loading...</p>
{/snippet}
</svelte:boundary>
Loading