pull/16266/head
Rich Harris 3 months ago
parent 480f55d143
commit f0aac15fca

@ -2,6 +2,7 @@
import { current_component } from './internal/server/context.js';
import { noop } from './internal/shared/utils.js';
import * as e from './internal/server/errors.js';
import { STALE_REACTION } from '#client/constants';
/** @param {() => void} fn */
export function onDestroy(fn) {
@ -42,7 +43,7 @@ export function getAbortSignal() {
if (controller === null) {
const c = (controller = new AbortController());
queueMicrotask(() => {
c.abort();
c.abort(STALE_REACTION);
controller = null;
});
}

@ -3,14 +3,24 @@ import { test } from '../../test';
export default test({
html: `<button>increment</button><p>loading...</p>`,
async test({ assert, target }) {
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)); // TODO replace with `tick` once `await` lands
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)); // TODO replace with `tick` once `await` lands
await new Promise((f) => setTimeout(f, 50));
assert.htmlEqual(target.innerHTML, '<button>increment</button><p>2</p>');
assert.deepEqual(logs, ['aborted', { stale: true }]);
}
});

@ -4,11 +4,17 @@
let count = $state(0);
let delayed_count = $derived.by(async () => {
const response = await fetch(`data:text/plain;charset=utf-8,${count}`, {
signal: getAbortSignal()
});
let c = count;
return await response.json();
const signal = getAbortSignal();
await new Promise((f) => setTimeout(f));
if (signal.aborted) {
console.log('aborted', signal.reason);
}
return c;
});
</script>

Loading…
Cancel
Save