-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
test63.ts
124 lines (96 loc) 路 2.79 KB
/
test63.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import { Evt } from "../lib/index.ts";
import { EventEmitter } from "https://deno.land/std@0.159.0/node/events.ts";
import { assert } from "https://deno.land/x/tsafe@v1.1.3/assert.ts";
const log = global.console.log;
const console = { "log": (str: string | number) => console.stdOut += `${str}`, "stdOut": "" };
const ctx= Evt.newCtx();
const evtText = new Evt<string>();
const evtTime = new Evt<number>();
evtText.$attach(
text=> [ text.length ],
ctx,
count => console.log("1: " + count)
);
evtTime.waitFor(
time => time < 0,
ctx,
).then(time=> console.log("2: " + time));
evtText
.pipe(ctx)
.pipe(text => [text.toUpperCase()])
.attach(upperCaseText=> console.log("3: " + upperCaseText))
;
Evt.merge(ctx, [ evtText, evtTime ])
.attach(textOrTime => console.log("4: " + textOrTime))
;
const ee= new EventEmitter();
Evt.from<string>(ctx, ee, "text")
.attach(text=> console.log("5: " + text))
;
evtText.post("foo"); //Prints "1: 3" "3: FOO" "4: foo"
assert(
console.stdOut
===
[
"1: 3",
"3: FOO",
"4: foo"
].join("")
);
console.stdOut = "";
ee.emit("text", "bar"); //Prints "5: bar"
assert(console.stdOut === "5: bar");
console.stdOut = "";
console.log(evtText.getHandlers().length); //Prints "3"
assert(console.stdOut === "3");
console.stdOut = "";
console.log(evtTime.getHandlers().length); //Prints "2"
assert(console.stdOut === "2");
console.stdOut = "";
console.log(ee.listenerCount("text")); //Print "1"
assert(console.stdOut === "1");
console.stdOut = "";
ctx.evtDoneOrAborted.attachOnce(
({handlers})=> {
console.log(
handlers.filter(({ evt })=> evt === evtText).length +
" handlers detached from evtText"
);
console.log(
handlers.filter(({ evt })=> evt === evtTime).length +
" handlers detached from evtTime"
);
console.log(
handlers.length + " handlers detached total"
);
}
);
//Prints:
//"3 handlers detached from evtText"
//"2 handlers detached from evtTime"
//"5 handlers detached total"
ctx.done();
assert(
console.stdOut
===
[
"3 handlers detached from evtText",
"2 handlers detached from evtTime",
"5 handlers detached total"
].join("")
);
console.stdOut = "";
console.log(evtText.getHandlers().length); //Prints "0"
assert(console.stdOut === "0");
console.stdOut = "";
console.log(evtTime.getHandlers().length); //Prints "0"
assert(console.stdOut === "0");
console.stdOut = "";
console.log(ee.listenerCount("text")); //Print "0"
assert(console.stdOut === "0");
console.stdOut = "";
evtText.post("foo"); //Prints nothing
assert(console.stdOut === "");
ee.emit("text", "bar"); //Prints nothing
assert(console.stdOut === "");
log("PASS");