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