import * as scopes from 'wasm-rquickjs:custom-invocation-context';
import {
beginCustomDurableInvocationAsync,
LiveCustomDurableInvocation,
} from 'golem:durability/durability@1.6.0';
import { effect, syncEffect, exchangeCount } from 'test:custom-invocation-context/effects';
import { middlewareCapture, middlewareDurable } from 'middleware';
let savedToken;
async function durableAsync(name, body) {
const parent = scopes.capture();
const live = await beginCustomDurableInvocationAsync(name);
const child = scopes.run(parent, () => scopes.enterLive(live));
try {
const result = await scopes.run(child, () => body(child));
await scopes.run(child, () => LiveCustomDurableInvocation.finishAsync(live));
return result;
} finally {
scopes.run(child, () => live[Symbol.dispose]());
scopes.close(child);
}
}
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
async function* generator(label) {
yield 1;
await effect(`${label}-gen`, 1);
yield 2;
}
export async function run() {
await Promise.all([
durableAsync('a', async () => {
await effect('a1', 30);
await effect('a2', 0);
}),
durableAsync('b', async () => {
await effect('b1', 5);
syncEffect('b2');
}),
(async () => {
await sleep(10);
await effect('root1', 0);
})(),
]);
await durableAsync('outer', async () => {
await durableAsync('inner', async () => {
await effect('i1', 0);
});
syncEffect('o1');
});
await durableAsync('cb', async () => {
await new Promise((resolve) => setTimeout(() => {
syncEffect('timer');
resolve();
}, 5));
await new Promise((resolve) => queueMicrotask(() => {
syncEffect('microtask');
resolve();
}));
await new Promise((resolve) => process.nextTick(() => {
syncEffect('tick');
resolve();
}));
await Promise.resolve().then(() => syncEffect('then'));
await { then(resolve) { syncEffect('thenable'); resolve(); } };
for await (const _ of generator('cb')) {
syncEffect('loop');
}
});
const shared = sleep(5);
await Promise.all([
durableAsync('s1', async () => { await shared; syncEffect('s1'); }),
durableAsync('s2', async () => { await shared; syncEffect('s2'); }),
]);
let closed;
await durableAsync('parent', async () => {
await durableAsync('closed', async (child) => { closed = child; });
scopes.run(closed, () => syncEffect('closed-in-parent'));
});
scopes.run(closed, () => syncEffect('closed-at-root'));
const identity = await durableAsync('identity', async (child) => {
return scopes.capture() === child;
});
await durableAsync('saved', async (child) => {
savedToken = child;
await effect('saved', 0);
});
syncEffect('root2');
return JSON.stringify({ identity });
}
export async function runAfter() {
scopes.run(savedToken, () => syncEffect('foreign'));
return 'ok';
}
export async function runPlain() {
await effect('plain1', 0);
await Promise.resolve();
syncEffect('plain2');
return 'ok';
}
export async function runEdgeCases() {
const report = {};
await durableAsync('cb2', async (child) => {
await new Promise((resolve) => {
let ticks = 0;
const handle = setInterval(() => {
syncEffect('interval');
if (++ticks === 3) {
clearInterval(handle);
resolve();
}
}, 1);
});
let late;
await durableAsync('late', async () => {
late = setInterval(() => syncEffect('late-tick'), 1);
});
await sleep(5);
clearInterval(late);
await new Promise((resolve) => setImmediate(() => {
syncEffect('immediate');
resolve();
}));
await Promise.race([sleep(1), new Promise(() => {})]);
syncEffect('race');
await sleep(1).finally(() => syncEffect('finally'));
try {
await Promise.reject(new Error('boom'));
} catch {
syncEffect('rejected');
}
const before = exchangeCount();
scopes.swap(scopes.capture());
scopes.run(child, () => syncEffect('run-current'));
report.unchangedExchanges = exchangeCount() - before;
const finalized = new Promise((resolve) => {
const registry = new FinalizationRegistry(() => {
syncEffect('finalized');
resolve();
});
(() => { registry.register({}, 'held'); })();
globalThis.registryRef = registry;
});
gc();
await Promise.race([finalized, sleep(50)]);
});
await durableAsync('p2', async (parentToken) => {
const live = await beginCustomDurableInvocationAsync('c2');
const childToken = scopes.run(parentToken, () => scopes.enterLive(live));
try {
scopes.close(parentToken);
report.closeWithChild = 'closed';
} catch (error) {
report.closeWithChild = 'refused';
}
await scopes.run(childToken, () => LiveCustomDurableInvocation.finishAsync(live));
scopes.run(childToken, () => live[Symbol.dispose]());
scopes.close(childToken);
});
await durableAsync('main', async (child) => {
report.sharedToken = middlewareCapture() === child;
await middlewareDurable('mw', async (mwToken) => {
report.sharedChild = scopes.capture() === mwToken;
await effect('mw1', 0);
});
});
return JSON.stringify(report);
}
export async function runFirstScope() {
const live = await beginCustomDurableInvocationAsync('first');
queueMicrotask(() => syncEffect('first-microtask'));
process.nextTick(() => syncEffect('first-tick'));
const timer = new Promise((resolve) => setTimeout(() => {
syncEffect('first-timer');
resolve();
}, 0));
const child = scopes.enterLive(live);
await effect('first-child', 5);
await LiveCustomDurableInvocation.finishAsync(live);
live[Symbol.dispose]();
scopes.close(child);
await timer;
syncEffect('first-after');
return 'ok';
}