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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
|
test(() => {
const source = new Observable(subscriber => {
subscriber.next(1);
subscriber.next(2);
subscriber.next(3);
subscriber.complete();
});
let projectionCalls = 0;
const results = [];
const flattened = source.flatMap(value => {
projectionCalls++;
return new Observable((subscriber) => {
subscriber.next(value * 10);
subscriber.next(value * 100);
subscriber.complete();
});
});
assert_true(flattened instanceof Observable, "flatMap() returns an Observable");
assert_equals(projectionCalls, 0,
"Projection is not called until subscription starts");
flattened.subscribe({
next: v => results.push(v),
error: () => results.push("error"),
complete: () => results.push("complete"),
});
assert_equals(projectionCalls, 3,
"Mapper is called three times, once for each source Observable value");
assert_array_equals(results, [10, 100, 20, 200, 30, 300, "complete"],
"flatMap() results are correct");
}, "flatMap(): Flattens simple source Observable properly");
test(() => {
const error = new Error("error");
const source = new Observable(subscriber => {
subscriber.next(1);
subscriber.next(2);
subscriber.error(error);
subscriber.next(3);
});
const flattened = source.flatMap(value => {
return new Observable(subscriber => {
subscriber.next(value * 10);
subscriber.next(value * 100);
subscriber.complete();
});
});
const results = [];
flattened.subscribe({
next: v => results.push(v),
error: e => results.push(e),
complete: () => results.push("complete"),
});
assert_array_equals(results, [10, 100, 20, 200, error],
"Source error is passed through to the flatMap() Observable");
}, "flatMap(): Returned Observable passes through source Observable errors");
test(() => {
const results = [];
const error = new Error("error");
const source = new Observable(subscriber => {
subscriber.next(1);
results.push(subscriber.active ? "active" : "inactive");
subscriber.next(2);
results.push(subscriber.active ? "active" : "inactive");
subscriber.next(3);
subscriber.complete();
});
const flattened = source.flatMap((value) => {
return new Observable((subscriber) => {
subscriber.next(value * 10);
subscriber.next(value * 100);
if (value === 2) {
subscriber.error(error);
} else {
subscriber.complete();
}
});
});
flattened.subscribe({
next: v => results.push(v),
error: e => results.push(e),
complete: () => results.push("complete"),
});
assert_array_equals(results, [10, 100, "active", 20, 200, error, "inactive"],
"Inner subscription error gets surfaced");
}, "flatMap(): Outer Subscription synchronously becomes inactive when an " +
"'inner' Observable emits an error");
test(() => {
const results = [];
const error = new Error("error");
const source = new Observable(subscriber => {
subscriber.next(1);
subscriber.next(2);
subscriber.next(3);
results.push(subscriber.active ? "active" : "inactive");
subscriber.complete();
});
const flattened = source.flatMap(value => {
if (value === 3) {
throw error;
}
return new Observable(subscriber => {
subscriber.next(value * 10);
subscriber.next(value * 100);
subscriber.complete();
});
});
flattened.subscribe({
next: v => results.push(v),
error: e => results.push(e),
complete: () => results.push("complete"),
});
assert_array_equals(results, [10, 100, 20, 200, error, "inactive"],
"Inner subscriber thrown error gets surfaced");
}, "flatMap(): Outer Subscription synchronously becomes inactive when an " +
"'inner' Observable throws an error");
test(() => {
const source = createTestSubject();
const inner1 = createTestSubject();
const inner2 = createTestSubject();
const flattened = source.flatMap(value => {
if (value === 1) {
return inner1;
}
return inner2;
});
const results = [];
flattened.subscribe({
next: v => results.push(v),
error: e => results.push(e),
complete: () => results.push("complete"),
});
assert_array_equals(results, []);
source.next(1);
assert_equals(inner1.subscriberCount(), 1, "inner1 gets subscribed to");
source.next(2);
assert_equals(inner2.subscriberCount(), 0,
"inner2 is queued, not subscribed to until inner1 completes");
assert_array_equals(results, []);
inner1.next(100);
inner1.next(101);
assert_array_equals(results, [100, 101]);
inner1.complete();
assert_equals(inner1.subscriberCount(), 0,
"inner1 becomes inactive once it completes");
assert_equals(inner2.subscriberCount(), 1,
"inner2 gets un-queued and subscribed to once inner1 completes");
inner2.next(200);
inner2.next(201);
assert_array_equals(results, [100, 101, 200, 201]);
inner2.complete();
assert_equals(inner2.subscriberCount(), 0,
"inner2 becomes inactive once it completes");
assert_equals(source.subscriberCount(), 1,
"source is not unsubscribed from yet, since it has not completed");
assert_array_equals(results, [100, 101, 200, 201]);
source.complete();
assert_equals(source.subscriberCount(), 0,
"source unsubscribed from after it completes");
assert_array_equals(results, [100, 101, 200, 201, "complete"]);
}, "flatMap(): result Observable does not complete until source and inner " +
"Observables all complete");
test(() => {
const source = createTestSubject();
const inner1 = createTestSubject();
const inner2 = createTestSubject();
const flattened = source.flatMap(value => {
if (value === 1) {
return inner1;
}
return inner2;
});
const results = [];
flattened.subscribe({
next: v => results.push(v),
error: e => results.push(e),
complete: () => results.push("complete"),
});
assert_array_equals(results, []);
source.next(1);
source.next(2);
assert_equals(inner1.subscriberCount(), 1, "inner1 gets subscribed to");
assert_equals(inner2.subscriberCount(), 0,
"inner2 is queued, not subscribed to until inner1 completes");
assert_array_equals(results, []);
// Before `inner1` pushes any values, we first complete the source Observable.
// This will not fire completion of the Observable returned from `flatMap()`,
// because there are two values (corresponding to inner Observables) that are
// queued to the inner queue that need to be processed first. Once the last
// one of *those* completes (i.e., `inner2.complete()` further down), then the
// returned Observable can finally complete.
source.complete();
assert_equals(source.subscriberCount(), 0,
"source becomes inactive once it completes");
inner1.next(100);
inner1.next(101);
assert_array_equals(results, [100, 101]);
inner1.complete();
assert_array_equals(results, [100, 101],
"Outer completion not triggered after inner1 completes");
assert_equals(inner2.subscriberCount(), 1,
"inner2 gets un-queued and subscribed after inner1 completes");
inner2.next(200);
inner2.next(201);
assert_array_equals(results, [100, 101, 200, 201]);
inner2.complete();
assert_equals(inner2.subscriberCount(), 0,
"inner2 becomes inactive once it completes");
assert_array_equals(results, [100, 101, 200, 201, "complete"]);
}, "flatMap(): result Observable does not complete after source Observable " +
"completes while there are still queued inner Observables to process " +
"Observables all complete");
test(() => {
const source = createTestSubject();
const inner = createTestSubject();
const result = source.flatMap(() => inner);
const ac = new AbortController();
result.subscribe({}, { signal: ac.signal, });
source.next(1);
assert_equals(inner.subscriberCount(), 1,
"inner Observable subscribed to once source emits it");
ac.abort();
assert_equals(source.subscriberCount(), 0,
"source unsubscribed from, once outer signal is aborted");
assert_equals(inner.subscriberCount(), 0,
"inner Observable unsubscribed from once the outer Observable is " +
"subscribed from, as a result of the outer signal being aborted");
}, "flatMap(): source and inner active Observables are both unsubscribed " +
"from once the outer subscription signal is aborted");
// A helper function to create an Observable that can be externally controlled
// and examined for testing purposes.
function createTestSubject() {
const subscribers = new Set();
const subject = new Observable(subscriber => {
subscribers.add(subscriber);
subscriber.addTeardown(() => subscribers.delete(subscriber));
});
subject.next = value => {
for (const subscriber of Array.from(subscribers)) {
subscriber.next(value);
}
};
subject.error = error => {
for (const subscriber of Array.from(subscribers)) {
subscriber.error(error);
}
};
subject.complete = () => {
for (const subscriber of Array.from(subscribers)) {
subscriber.complete();
}
};
subject.subscriberCount = () => {
return subscribers.size;
};
return subject;
}
|