/
test95.ts
123 lines (72 loc) 路 2.34 KB
/
test95.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
import { Evt, nonNullable } from "../lib/index.ts";
import { assert } from "https://raw.githubusercontent.com/garronej/tsafe/v1.0.0/deno_dist/assert.ts";;
(async () => {
{
const evt = Evt.create("foo");
const evtBis = Evt.asyncPipe(
evt,
text => text === undefined ?
[undefined] :
Promise.resolve(text + " bar").then(text => [text])
);
assert(evtBis.state === undefined);
assert( await evtBis.waitFor(nonNullable()) === "foo bar");
}
await Promise.resolve();
{
const evt = Evt.create("foo");
const evtBis = Evt.asyncPipe(
evt,
text => [text.length]
);
assert(evtBis.state === 3);
}
await Promise.resolve();
{
const f= async (text: string)=> {
await new Promise(resolve => setTimeout(resolve, 50));
return text.length;
};
const evt = Evt.create<string | undefined>("foo");
const evtBis = Evt.asyncPipe(
evt,
text => text === undefined ?
[undefined] :
f(text).then(n => [n])
);
assert(evtBis.state === undefined);
evt.state= "bar";
evt.state= "bar bar";
evt.state = undefined;
assert(evtBis.state === undefined);
const pr= evtBis.waitFor(nonNullable());
evt.state= "Hello World";
evt.state= "Okay";
assert(await pr === 4);
}
{
const f= async (text: string)=> {
await new Promise(resolve => setTimeout(resolve, text.length*100));
return text;
};
const evt = Evt.create<string | undefined>();
const evtBis = Evt.asyncPipe(
evt,
text => text === undefined ?
[undefined] :
f(text).then(n => [n])
);
const pr =(async ()=>{
assert(await evtBis.waitFor() === undefined);
assert(await evtBis.waitFor() === "bar");
assert(await evtBis.waitFor() === "bar bar");
assert(await evtBis.waitFor() === "bar bar bar");
})();
evt.post("bar bar bar");
evt.post("bar bar");
evt.post(undefined);
evt.post("bar");
await pr;
}
console.log("PASS");
})();