Something went wrong. Try again.
javascript for 🐜's, a tiny runtime with big ambitions antjs.org
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119const assert = require("assert");
{ const calls = []; const stack = new DisposableStack(); const first = { disposed: false }; const second = { disposed: false }; const third = { disposed: false, [Symbol.dispose]() { this.disposed = true; calls.push("third"); }, };
assert.strictEqual(stack.adopt(first, (value) => { value.disposed = true; calls.push("first"); }), first);
assert.strictEqual(stack.defer(() => { second.disposed = true; calls.push("second"); }), undefined);
const moved = stack.move(); assert.strictEqual(moved.use(third), third); let movedStackRejected = false; try { stack.defer(() => {}); } catch (error) { movedStackRejected = /already disposed/.test(error.message); } assert(movedStackRejected);
assert.strictEqual(moved.dispose(), undefined); assert.deepStrictEqual(calls, ["third", "second", "first"]); assert(first.disposed); assert(second.disposed); assert(third.disposed); assert.strictEqual(moved.dispose(), undefined);}
{ const stack = new DisposableStack(); stack.defer(() => { throw new Error("first"); }); stack.defer(() => { throw new Error("second"); });
assert.throws(() => stack.dispose(), (error) => { assert(error instanceof SuppressedError); assert.strictEqual(error.error.message, "first"); assert.strictEqual(error.suppressed.message, "second"); return true; });}
{ const calls = []; const stack = new AsyncDisposableStack(); const first = { disposed: false }; const second = { disposed: false }; const third = { disposed: false, async [Symbol.asyncDispose]() { this.disposed = true; calls.push("third"); }, };
assert.strictEqual(stack.adopt(first, async (value) => { value.disposed = true; calls.push("first"); }), first);
assert.strictEqual(stack.defer(async () => { second.disposed = true; calls.push("second"); }), undefined);
const moved = stack.move(); assert.strictEqual(moved.use(third), third);
moved.disposeAsync().then(() => { assert.deepStrictEqual(calls, ["third", "second", "first"]); assert(first.disposed); assert(second.disposed); assert(third.disposed); });}
{ const stack = new AsyncDisposableStack(); stack.defer(async () => { throw new Error("async first"); }); stack.defer(async () => { throw new Error("async second"); });
stack.disposeAsync().then( () => { throw new Error("disposeAsync should reject"); }, (error) => { assert(error instanceof SuppressedError); assert.strictEqual(error.error.message, "async first"); assert.strictEqual(error.suppressed.message, "async second"); } );}
Promise.resolve().then(() => { console.log("DisposableStack tests completed!");});