UNPKG

typed-utilities

Version:
69 lines (63 loc) 1.84 kB
"use strict"; var _ = require(".."); test(`useAsync`, async () => { const counters = new Set(); class Counter { state = 0; constructor(name) { this.name = name; this.state = 0; counters.add(this); } incr = async () => { this.state++; }; error = async () => { throw new Error(); }; destroy = async () => { counters.delete(this); }; value = () => this.state; } const stack = []; expect(counters.size).toEqual(0); const unused = new Counter(`unused`); expect(counters.size).toEqual(1); await unused.destroy(); expect(counters.size).toEqual(0); const counterValue = await (0, _.useAsync)(async () => { return new Counter(`counter 1`); }, async counter => { await counter.destroy(); }, async counter => { expect(counters.size).toEqual(1); stack.push(counter.value()); await counter.incr(); stack.push(counter.value()); await counter.incr(); stack.push(counter.value()); await counter.incr(); stack.push(counter.value()); return counter.value(); }); expect(counters.size).toEqual(0); expect(counterValue).toEqual(3); expect(stack).toEqual([0, 1, 2, 3]); stack.splice(0, stack.length); expect(stack).toEqual([]); await expect(async () => await (0, _.useAsync)(async () => new Counter(`counter 2`), async counter => await counter.destroy(), async counter => { expect(counters.size).toEqual(1); stack.push(counter.value()); await counter.incr(); stack.push(counter.value()); await counter.incr(); stack.push(counter.value()); await counter.incr(); stack.push(counter.value()); await counter.error(); })).rejects.toBeTruthy(); expect(counters.size).toEqual(0); expect(stack).toEqual([0, 1, 2, 3]); }); //# sourceMappingURL=useAsync.test.js.map