-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
test17.ts
62 lines (41 loc) 路 1.13 KB
/
test17.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 } from "../lib/index.ts";
interface Animal {
type: "CAT" | "SPIDER";
cute: boolean;
name: string;
}
interface Cat extends Animal {
type: "CAT";
cute: true;
}
//@ts-ignore: unused
interface Spider extends Animal {
type: "SPIDER";
cute: false;
}
const animals: Animal[] = [
{ "type": "CAT", "cute": true, "name": "boubou" },
{ "type": "SPIDER", "cute": false, "name": "spiderMan" },
{ "type": "CAT", "cute": true, "name": "miaou" },
{ "type": "SPIDER", "cute": false, "name": "aracnide" }
];
let cats = animals.filter(({ type }) => type === "CAT");
let spiders = animals.filter(({ type }) => type === "SPIDER");
let evtAnimal = new Evt<Animal>();
evtAnimal.attach(
animal => console.assert(spiders.shift() === animal)
);
const ctx= Evt.newCtx();
evtAnimal.attachExtract(
(animal: Animal): animal is Cat => animal.type === "CAT",
ctx,
cat => {
console.assert(cats.shift() === cat);
if (!cats.length) evtAnimal.detach(ctx);
console.log("PASS");
}
);
(function main() {
for (let animal of animals)
evtAnimal.post(animal);
})();