/
test107.ts
59 lines (39 loc) 路 1.16 KB
/
test107.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { Evt, TimeoutEvtError } from '../lib/index.ts';
import { assert } from "https://raw.githubusercontent.com/garronej/tsafe/v1.0.0/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");
})();
}