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

@ -3,14 +3,24 @@ import { test } from '../../test';
export default test({ export default test({
html: `<button>increment</button><p>loading...</p>`, 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'); 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>'); assert.htmlEqual(target.innerHTML, '<button>increment</button><p>0</p>');
button.click(); 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.htmlEqual(target.innerHTML, '<button>increment</button><p>2</p>');
assert.deepEqual(logs, ['aborted', { stale: true }]);
} }
}); });

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

Loading…
Cancel
Save