Skip to main content
Module

x/evt/test/test107.ts

💧EventEmitter's typesafe replacement
Go to Latest
File
import { Evt, TimeoutEvtError } from '../lib/index.ts';import { assert } from "https://raw.githubusercontent.com/garronej/tsafe/v0.10.1/deno_dist/assert.ts";
const log = console.log;
const timer = setTimeout( () => { assert(false); }, 400);
{
const console = { "log": (str: string | number) => console.stdOut += `${str}`, "stdOut": "" };
const evtStr = Evt.create<string>();
setTimeout( async () => {
evtStr.post("one"); await new Promise((reduce) => setTimeout(reduce, 50)); evtStr.post("two"); await new Promise((reduce) => setTimeout(reduce, 50)); evtStr.post("three");
//While we wait here the async iterator will timeout await new Promise((reduce) => setTimeout(reduce, 200));
evtStr.post("four"); evtStr.post("five");
}, 0);

(async () => { const ctx = Evt.newCtx();
for await (const str of evtStr.iter(ctx, 100)) { console.log(str); }
if (ctx.completionStatus?.error instanceof TimeoutEvtError) { console.log("We exited the loop because of a timeout"); }
assert(console.stdOut === "onetwothreeWe exited the loop because of a timeout");
clearTimeout(timer);
assert(evtStr.getHandlers().length === 0);
log("PASS");
})();
}