@@ -1,173 +1,191 | |||
|
1 | 1 | import { empty, observe, of } from "./observable"; |
|
2 | 2 | import * as tap from "tap"; |
|
3 | 3 | import { Cancellation } from "@implab/core-amd/Cancellation"; |
|
4 | 4 | import { delay } from "@implab/core-amd/safe"; |
|
5 | 5 | |
|
6 | 6 | const subj1 = observe<number>(({ next, complete }) => { |
|
7 | 7 | next(1); |
|
8 | 8 | complete(); |
|
9 | 9 | next(2); |
|
10 | 10 | }); |
|
11 | 11 | |
|
12 | 12 | const consumer1 = { |
|
13 | 13 | sum: 0, |
|
14 | 14 | next(v: number) { |
|
15 | 15 | this.sum += v; |
|
16 | 16 | } |
|
17 | 17 | }; |
|
18 | 18 | |
|
19 | 19 | subj1.subscribe(consumer1); |
|
20 | 20 | tap.equal(consumer1.sum, 1, "Should get only one value"); |
|
21 | 21 | |
|
22 | 22 | subj1.subscribe(consumer1); |
|
23 | 23 | tap.equal(consumer1.sum, 2, "Should get the value again"); |
|
24 | 24 | |
|
25 | 25 | const consumer2 = { |
|
26 | 26 | value: 0, |
|
27 | 27 | completed: false, |
|
28 | 28 | next(v: number) { this.value = v; }, |
|
29 | 29 | complete() { this.completed = true; } |
|
30 | 30 | }; |
|
31 | 31 | |
|
32 | 32 | let maps = 0; |
|
33 | 33 | |
|
34 | 34 | subj1 |
|
35 | 35 | .map(v => { |
|
36 | 36 | tap.comment(`map1: ${v * 2}`); |
|
37 | 37 | maps++; |
|
38 | 38 | return v * 2; |
|
39 | 39 | }) |
|
40 | 40 | .map(v => { |
|
41 | 41 | tap.comment(`map2: ${v * 2}`); |
|
42 | 42 | maps++; |
|
43 | 43 | return v * 2; |
|
44 | 44 | }) |
|
45 | 45 | .map(v => { |
|
46 | 46 | tap.comment(`map3: ${v * 2}`); |
|
47 | 47 | maps++; |
|
48 | 48 | return v * 2; |
|
49 | 49 | }) |
|
50 | 50 | .subscribe(consumer2); |
|
51 | 51 | |
|
52 | 52 | tap.equal(consumer2.value, 8, "Should map"); |
|
53 | 53 | tap.equal(maps, 3, "The map chain should not be executed after completion"); |
|
54 | 54 | tap.ok(consumer2.completed, "The completion signal should pass through"); |
|
55 | 55 | |
|
56 | 56 | const subj2 = observe<number>(({ next, complete }) => { |
|
57 | 57 | [1, 2, 3, 4, 5].forEach(next); |
|
58 | 58 | complete(); |
|
59 | 59 | return () => { |
|
60 | 60 | tap.comment("subj2: unsubscribe"); |
|
61 | 61 | }; |
|
62 | 62 | }); |
|
63 | 63 | |
|
64 | 64 | const consumer3 = { |
|
65 | 65 | even: 0, |
|
66 | 66 | odd: 0, |
|
67 | 67 | completed: false, |
|
68 | 68 | subscribed: 0, |
|
69 | 69 | unsubscribed: 0, |
|
70 | 70 | next(v: "even" | "odd") { |
|
71 | 71 | this[v]++; |
|
72 | 72 | }, |
|
73 | 73 | complete() { |
|
74 | 74 | this.completed = true; |
|
75 | 75 | }, |
|
76 | 76 | subscribe() { |
|
77 | 77 | this.subscribed++; |
|
78 | 78 | }, |
|
79 | 79 | unsubscribe() { |
|
80 | 80 | this.unsubscribed++; |
|
81 | 81 | } |
|
82 | 82 | }; |
|
83 | 83 | |
|
84 | 84 | |
|
85 | 85 | const subj3 = subj2.pipe<"even" | "odd">(self => observe(({ next, complete, error }) => { |
|
86 | 86 | consumer3.subscribe(); |
|
87 | 87 | let count = 0; |
|
88 | 88 | const h = self.subscribe({ |
|
89 | 89 | next: val => { |
|
90 | 90 | if (val % 2 === 0) |
|
91 | 91 | next("odd"); |
|
92 | 92 | else |
|
93 | 93 | next("even"); |
|
94 | 94 | if (++count === 4) |
|
95 | 95 | complete(); |
|
96 | 96 | }, |
|
97 | 97 | complete, |
|
98 | 98 | error |
|
99 | 99 | }); |
|
100 | 100 | return () => { |
|
101 | 101 | consumer3.unsubscribe(); |
|
102 | 102 | h.unsubscribe(); |
|
103 | 103 | }; |
|
104 | 104 | })); |
|
105 | 105 | |
|
106 | 106 | subj3.subscribe(consumer3); |
|
107 | 107 | |
|
108 | 108 | tap.equal(consumer3.odd, 2, "Should get 2 odd elements"); |
|
109 | 109 | tap.equal(consumer3.even, 2, "Should get 2 even elements"); |
|
110 | 110 | tap.ok(consumer3.completed, "The sequence should completed"); |
|
111 | 111 | tap.equal(consumer3.subscribed, 1, "The subscription should be done once"); |
|
112 | 112 | tap.equal(consumer3.unsubscribed, 1, "The cleanup should be done after completion"); |
|
113 | 113 | |
|
114 | 114 | subj2.reduce((a, b) => a + b).subscribe({ |
|
115 | 115 | next: val => tap.comment("subj2: reduce =", val), |
|
116 | 116 | complete: () => tap.comment("subj2: complete") |
|
117 | 117 | }); |
|
118 | 118 | |
|
119 | 119 | tap.test("of(...) tests", async t => { |
|
120 | 120 | await subj2.reduce((a, b) => a + b).next() |
|
121 | 121 | .then(value => t.comment("subj2: next reduce=", value)); |
|
122 | 122 | |
|
123 | 123 | await subj2.next().then(val => t.equal(val, 1, "Should peek the first element")); |
|
124 | 124 | |
|
125 | 125 | const cancelled = new Cancellation(cancel => cancel()); |
|
126 | 126 | await t.rejects(subj2.next(cancelled), "Cancelled next() method should fail"); |
|
127 | 127 | |
|
128 | 128 | await t.rejects(empty.next(), "Empty sequence should fail to get next element"); |
|
129 | 129 | |
|
130 | 130 | await of(delay(1).then(() => 1), Promise.resolve(2), 3) |
|
131 | 131 | .reduce<number[]>((a, x) => [...a, x], []) |
|
132 | 132 | .next() |
|
133 | 133 | .then(res => t.same(res, [1, 2, 3], "of(...) should keep the order")); |
|
134 | 134 | |
|
135 | 135 | const rejected = Promise.reject("DIE!"); |
|
136 | 136 | rejected.catch(() => { }); // SAFE AND SOUND |
|
137 | 137 | |
|
138 | 138 | await t.resolves( |
|
139 | 139 | of(Promise.resolve(1), rejected).next(), |
|
140 | 140 | "of(...) should emit non-rejected items" |
|
141 | 141 | ); |
|
142 | 142 | await t.rejects( |
|
143 | 143 | of(1, Promise.reject("DIE!")).reduce((a) => a).next(), |
|
144 | 144 | "of(...) should terminate with error when a parameter is rejected" |
|
145 | 145 | ); |
|
146 | 146 | |
|
147 | t.same(await of(1,2,3).collect(), [1,2,3], ".collect() should return the collected sequence"); | |
|
148 | await t.rejects(of(1,2,3).collect(cancelled), ".collect() should support cancellation"); | |
|
147 | t.same(await of(1, 2, 3).collect(), [1, 2, 3], ".collect() should return the collected sequence"); | |
|
148 | await t.rejects(of(1, 2, 3).collect(cancelled), ".collect() should support cancellation"); | |
|
149 | 149 | |
|
150 | 150 | }).catch(() => { }); |
|
151 | 151 | |
|
152 | 152 | tap.test(".tap() tests", async t => { |
|
153 | 153 | const side: number[] = []; |
|
154 | 154 | |
|
155 | of(1,2) | |
|
156 | .tap({next: v => side.push(v), complete: () => side.push(0)}) | |
|
157 | .tap({next: v => side.push(v*v)}) | |
|
155 | of(1, 2) | |
|
156 | .tap({ next: v => side.push(v), complete: () => side.push(0) }) | |
|
157 | .tap({ next: v => side.push(v * v) }) | |
|
158 | 158 | .subscribe({}); |
|
159 | 159 | |
|
160 | t.same(side, [1,1,2,4,0], ".tap() should be called in the order of registration"); | |
|
160 | t.same(side, [1, 1, 2, 4, 0], ".tap() should be called in the order of registration"); | |
|
161 | 161 | |
|
162 | 162 | side.length = 0; |
|
163 | 163 | |
|
164 | 164 | await new Promise<void>(resolve => { |
|
165 | of(1,2,delay(1).then(() => 3)) | |
|
166 | .tap({next: v => side.push(v)}) | |
|
167 | .tap({ next: v => v === 1 && resolve()}) | |
|
165 | of(1, 2, delay(1).then(() => 3)) | |
|
166 | .tap({ next: v => side.push(v) }) | |
|
167 | .tap({ next: v => v === 1 && resolve() }) | |
|
168 | 168 | .subscribe({}); |
|
169 | 169 | }); |
|
170 | 170 | |
|
171 | t.same(side, [1,2], ".tap() should be processed synchronously"); | |
|
171 | t.same(side, [1, 2], ".tap() should be processed synchronously"); | |
|
172 | ||
|
173 | }).catch(() => { }); | |
|
174 | ||
|
175 | tap.test(".while() tests", async t => { | |
|
176 | ||
|
177 | const seq = of(1, 2, 3, 4).while(v => v <= 2); | |
|
178 | ||
|
179 | t.same(await seq.collect(), [1, 2], "Should collect only taken elements"); | |
|
172 | 180 | |
|
173 | }).catch(() => {}); No newline at end of file | |
|
181 | const data: number[] = []; | |
|
182 | let complete = 0; | |
|
183 | seq.subscribe({ | |
|
184 | next: v => data.push(v), | |
|
185 | complete: () => complete++ | |
|
186 | }); | |
|
187 | ||
|
188 | t.same(data, [1, 2], "Should receive only taken elements"); | |
|
189 | t.equal(complete, 1, "Complete should run once"); | |
|
190 | ||
|
191 | }).catch(() => { }); No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now