/
test101.ts
69 lines (44 loc) 路 1.12 KB
/
test101.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
60
61
62
63
64
65
66
67
68
69
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;
(async () => {
const timer = setTimeout(
()=> {
assert(false);
},
4000
);
const console = { "log": (str: string | number) => console.stdOut += `${str}`, "stdOut": "" };
const evtStr = Evt.create<string>();
const ctx = Evt.newCtx();
setTimeout(
()=>{
evtStr.post("one");
evtStr.post("two");
evtStr.post("three");
ctx.done();
},
0
);
ctx.evtDoneOrAborted.attachOnce(event=> {
if( event.type === "ABORTED" ){
const { error }= event;
if( error instanceof TimeoutEvtError ){
console.log("TIMEOUT");
}else{
console.log(`Aborter: ${error.message}`);
}
return;
}
console.log("DONE");
});
for await (const str of evtStr.iter(ctx, 60)) {
await new Promise(resolve=> setTimeout(resolve,100));
console.log(str);
}
console.log("loop_end");
assert(console.stdOut === "DONEonetwothreeloop_end");
clearTimeout(timer);
assert(evtStr.getHandlers().length === 0);
log("PASS");
})();