/
shareReplay-spec.ts
427 lines (356 loc) · 16.7 KB
/
shareReplay-spec.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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
import { expect } from 'chai';
import * as sinon from 'sinon';
import { shareReplay, mergeMapTo, retry, take } from 'rxjs/operators';
import { TestScheduler } from 'rxjs/testing';
import { Observable, Operator, Observer, of, from, defer, pipe, combineLatest, firstValueFrom, BehaviorSubject } from 'rxjs';
import { observableMatcher } from '../helpers/observableMatcher';
/** @test {shareReplay} */
describe('shareReplay', () => {
let testScheduler: TestScheduler;
beforeEach(() => {
testScheduler = new TestScheduler(observableMatcher);
});
it('should mirror a simple source Observable', () => {
testScheduler.run(({ cold, expectObservable, expectSubscriptions }) => {
const source = cold('--1-2---3-4--5-|');
const sourceSubs = ' ^--------------!';
const expected = ' --1-2---3-4--5-|';
const published = source.pipe(shareReplay());
expectObservable(published).toBe(expected);
expectSubscriptions(source.subscriptions).toBe(sourceSubs);
});
});
it('should do nothing if result is not subscribed', () => {
let subscribed = false;
const source = new Observable(() => {
subscribed = true;
});
source.pipe(shareReplay());
expect(subscribed).to.be.false;
});
it('should multicast the same values to multiple observers, bufferSize=1', () => {
testScheduler.run(({ cold, hot, expectObservable, expectSubscriptions }) => {
const source = cold(' -1-2-3----4-|');
const sourceSubs = ' ^-----------!';
const subscriber1 = hot('a| ');
const expected1 = ' -1-2-3----4-|';
const subscriber2 = hot('----b| ');
const expected2 = ' ----23----4-|';
const subscriber3 = hot('--------c| ');
const expected3 = ' --------3-4-|';
const shared = source.pipe(shareReplay(1));
expectObservable(subscriber1.pipe(mergeMapTo(shared))).toBe(expected1);
expectObservable(subscriber2.pipe(mergeMapTo(shared))).toBe(expected2);
expectObservable(subscriber3.pipe(mergeMapTo(shared))).toBe(expected3);
expectSubscriptions(source.subscriptions).toBe(sourceSubs);
});
});
it('should multicast the same values to multiple observers, bufferSize=2', () => {
testScheduler.run(({ cold, hot, expectObservable, expectSubscriptions }) => {
const source = cold(' -1-2-----3------4-|');
const sourceSubs = ' ^-----------------!';
const subscriber1 = hot('a| ');
const expected1 = ' -1-2-----3------4-|';
const subscriber2 = hot('----b| ');
const expected2 = ' ----(12)-3------4-|';
const subscriber3 = hot('-----------c| ');
const expected3 = ' -----------(23)-4-|';
const shared = source.pipe(shareReplay(2));
expectObservable(subscriber1.pipe(mergeMapTo(shared))).toBe(expected1);
expectObservable(subscriber2.pipe(mergeMapTo(shared))).toBe(expected2);
expectObservable(subscriber3.pipe(mergeMapTo(shared))).toBe(expected3);
expectSubscriptions(source.subscriptions).toBe(sourceSubs);
});
});
it('should multicast an error from the source to multiple observers', () => {
testScheduler.run(({ cold, hot, expectObservable, expectSubscriptions }) => {
const source = cold(' -1-2-3----4-#');
const sourceSubs = ' ^-----------!';
const subscriber1 = hot('a| ');
const expected1 = ' -1-2-3----4-#';
const subscriber2 = hot('----b| ');
const expected2 = ' ----23----4-#';
const subscriber3 = hot('--------c| ');
const expected3 = ' --------3-4-#';
const shared = source.pipe(shareReplay(1));
expectObservable(subscriber1.pipe(mergeMapTo(shared))).toBe(expected1);
expectObservable(subscriber2.pipe(mergeMapTo(shared))).toBe(expected2);
expectObservable(subscriber3.pipe(mergeMapTo(shared))).toBe(expected3);
expectSubscriptions(source.subscriptions).toBe(sourceSubs);
});
});
it('should multicast an empty source', () => {
testScheduler.run(({ cold, expectObservable, expectSubscriptions }) => {
const source = cold('| ');
const sourceSubs = ' (^!)';
const expected = ' | ';
const shared = source.pipe(shareReplay(1));
expectObservable(shared).toBe(expected);
expectSubscriptions(source.subscriptions).toBe(sourceSubs);
});
});
it('should multicast a never source', () => {
testScheduler.run(({ cold, expectObservable, expectSubscriptions }) => {
const source = cold('-');
const sourceSubs = ' ^';
const expected = ' -';
const shared = source.pipe(shareReplay(1));
expectObservable(shared).toBe(expected);
expectSubscriptions(source.subscriptions).toBe(sourceSubs);
});
});
it('should multicast a throw source', () => {
testScheduler.run(({ cold, expectObservable, expectSubscriptions }) => {
const source = cold('# ');
const sourceSubs = ' (^!)';
const expected = ' # ';
const shared = source.pipe(shareReplay(1));
expectObservable(shared).toBe(expected);
expectSubscriptions(source.subscriptions).toBe(sourceSubs);
});
});
it('should replay results to subsequent subscriptions if source completes, bufferSize=2', () => {
testScheduler.run(({ cold, hot, expectObservable, expectSubscriptions }) => {
const source = cold(' -1-2-----3-| ');
const sourceSubs = ' ^----------! ';
const subscriber1 = hot('a| ');
const expected1 = ' -1-2-----3-| ';
const subscriber2 = hot('----b| ');
const expected2 = ' ----(12)-3-| ';
const subscriber3 = hot('---------------(c|) ');
const expected3 = ' ---------------(23|)';
const shared = source.pipe(shareReplay(2));
expectObservable(subscriber1.pipe(mergeMapTo(shared))).toBe(expected1);
expectObservable(subscriber2.pipe(mergeMapTo(shared))).toBe(expected2);
expectObservable(subscriber3.pipe(mergeMapTo(shared))).toBe(expected3);
expectSubscriptions(source.subscriptions).toBe(sourceSubs);
});
});
it('should completely restart for subsequent subscriptions if source errors, bufferSize=2', () => {
testScheduler.run(({ cold, hot, expectObservable, expectSubscriptions }) => {
const source = cold(' -1-2-----3-# ');
const sourceSubs1 = ' ^----------! ';
const subscriber1 = hot('a| ');
const expected1 = ' -1-2-----3-# ';
const subscriber2 = hot('----b| ');
const expected2 = ' ----(12)-3-# ';
const subscriber3 = hot('---------------(c|) ');
const expected3 = ' ----------------1-2-----3-#';
const sourceSubs2 = ' ---------------^----------!';
const shared = source.pipe(shareReplay(2));
expectObservable(subscriber1.pipe(mergeMapTo(shared))).toBe(expected1);
expectObservable(subscriber2.pipe(mergeMapTo(shared))).toBe(expected2);
expectObservable(subscriber3.pipe(mergeMapTo(shared))).toBe(expected3);
expectSubscriptions(source.subscriptions).toBe([sourceSubs1, sourceSubs2]);
});
});
it('should be retryable, bufferSize=2', () => {
testScheduler.run(({ cold, hot, expectObservable, expectSubscriptions }) => {
const subs = [];
const source = cold(' -1-2-----3-# ');
subs.push(' ^----------! ');
subs.push(' -----------^----------! ');
subs.push(' ----------------------^----------!');
const subscriber1 = hot('a| ');
const expected1 = ' -1-2-----3--1-2-----3-# ';
const subscriber2 = hot('----b| ');
const expected2 = ' ----(12)-3--1-2-----3-# ';
const subscriber3 = hot('---------------(c|) ');
const expected3 = ' ---------------(12)-3--1-2-----3-#';
const shared = source.pipe(shareReplay(2), retry(1));
expectObservable(subscriber1.pipe(mergeMapTo(shared))).toBe(expected1);
expectObservable(subscriber2.pipe(mergeMapTo(shared))).toBe(expected2);
expectObservable(subscriber3.pipe(mergeMapTo(shared))).toBe(expected3);
expectSubscriptions(source.subscriptions).toBe(subs);
});
});
it('when no windowTime is given ReplaySubject should be in _infiniteTimeWindow mode', () => {
const spy = sinon.spy(testScheduler, 'now');
of(1).pipe(shareReplay(1, undefined, testScheduler)).subscribe();
spy.restore();
expect(spy, 'ReplaySubject should not call scheduler.now() when no windowTime is given').to.be.not.called;
});
it('should not restart due to unsubscriptions if refCount is false', () => {
testScheduler.run(({ cold, expectObservable, expectSubscriptions }) => {
const source = cold('a-b-c-d-e-f-g-h-i-j');
const sourceSubs = ' ^------------------';
const sub1 = ' ^------! ';
const expected1 = ' a-b-c-d- ';
const sub2 = ' -----------^-------';
const expected2 = ' -----------fg-h-i-j';
const shared = source.pipe(shareReplay({ bufferSize: 1, refCount: false }));
expectObservable(shared, sub1).toBe(expected1);
expectObservable(shared, sub2).toBe(expected2);
expectSubscriptions(source.subscriptions).toBe(sourceSubs);
});
});
it('should restart due to unsubscriptions if refCount is true', () => {
testScheduler.run(({ cold, expectObservable, expectSubscriptions }) => {
const sourceSubs = [];
const source = cold('a-b-c-d-e-f-g-h-i-j ');
sourceSubs.push(' ^------!----------------------');
sourceSubs.push(' -----------^------------------');
const sub1 = ' ^------! ';
const expected1 = ' a-b-c-d- ';
const sub2 = ' -----------^------------------';
const expected2 = ' -----------a-b-c-d-e-f-g-h-i-j';
const shared = source.pipe(shareReplay({ bufferSize: 1, refCount: true }));
expectObservable(shared, sub1).toBe(expected1);
expectObservable(shared, sub2).toBe(expected2);
expectSubscriptions(source.subscriptions).toBe(sourceSubs);
});
});
it('should not restart due to unsubscriptions if refCount is true when the source has completed', () => {
testScheduler.run(({ cold, expectObservable, expectSubscriptions }) => {
const source = cold('a-(b|) ');
const sourceSubs = ' ^-! ';
const sub1 = ' ^------! ';
const expected1 = ' a-(b|) ';
const sub2 = ' -----------^! ';
const expected2 = ' -----------(b|)';
const shared = source.pipe(shareReplay({ bufferSize: 1, refCount: true }));
expectObservable(shared, sub1).toBe(expected1);
expectObservable(shared, sub2).toBe(expected2);
expectSubscriptions(source.subscriptions).toBe(sourceSubs);
});
});
it('should not restart a synchronous source due to unsubscriptions if refCount is true when the source has completed', () => {
// The test above this one doesn't actually test completely synchronous
// behaviour because of this problem:
// https://github.com/ReactiveX/rxjs/issues/5523
let subscriptions = 0;
const source = defer(() => {
++subscriptions;
return of(42);
}).pipe(shareReplay({ bufferSize: 1, refCount: true }));
source.subscribe();
source.subscribe();
expect(subscriptions).to.equal(1);
});
it('should only subscribe once each with multiple synchronous subscriptions and unsubscriptions ', async () => {
// This may seem very specific, but it's a regression test for https://github.com/ReactiveX/rxjs/issues/6760
let subscriptions = 0;
const source = defer(() => {
++subscriptions;
// Needs to be an observable that doesn't complete
return new BehaviorSubject(1);
}).pipe(shareReplay({ bufferSize: 1, refCount: true }));
await firstValueFrom(combineLatest([source, source]));
expect(subscriptions).to.equal(1);
});
it('should default to refCount being false', () => {
testScheduler.run(({ cold, expectObservable, expectSubscriptions }) => {
const source = cold('a-b-c-d-e-f-g-h-i-j');
const sourceSubs = ' ^------------------';
const sub1 = ' ^------! ';
const expected1 = ' a-b-c-d- ';
const sub2 = ' -----------^-------';
const expected2 = ' -----------fg-h-i-j';
const shared = source.pipe(shareReplay(1));
expectObservable(shared, sub1).toBe(expected1);
expectObservable(shared, sub2).toBe(expected2);
expectSubscriptions(source.subscriptions).toBe(sourceSubs);
});
});
it('should not break lift() composability', (done) => {
class MyCustomObservable<T> extends Observable<T> {
lift<R>(operator: Operator<T, R>): Observable<R> {
const observable = new MyCustomObservable<R>();
(<any>observable).source = this;
(<any>observable).operator = operator;
return observable;
}
}
const result = new MyCustomObservable((observer: Observer<number>) => {
observer.next(1);
observer.next(2);
observer.next(3);
observer.complete();
}).pipe(shareReplay());
expect(result instanceof MyCustomObservable).to.be.true;
const expected = [1, 2, 3];
result.subscribe({
next(n: any) {
expect(expected.length).to.be.greaterThan(0);
expect(n).to.equal(expected.shift());
},
error() {
done(new Error('should not be called'));
},
complete() {
done();
},
});
});
it('should not skip values on a sync source', () => {
testScheduler.run(({ cold, expectObservable }) => {
const a = from(['a', 'b', 'c', 'd']);
// We would like for the previous line to read like this:
//
// const a = cold('(abcd|)');
//
// However, that would synchronously emit multiple values at frame 0,
// but it's not synchronous upon-subscription.
// TODO: revisit once https://github.com/ReactiveX/rxjs/issues/5523 is fixed
const x = cold(' x-------x');
const expected = '(abcd)--d';
const shared = a.pipe(shareReplay(1));
const result = x.pipe(mergeMapTo(shared));
expectObservable(result).toBe(expected);
});
});
it('should stop listening to a synchronous observable when unsubscribed', () => {
const sideEffects: number[] = [];
const synchronousObservable = new Observable<number>((subscriber) => {
// This will check to see if the subscriber was closed on each loop
// when the unsubscribe hits (from the `take`), it should be closed
for (let i = 0; !subscriber.closed && i < 10; i++) {
sideEffects.push(i);
subscriber.next(i);
}
});
synchronousObservable.pipe(shareReplay({ refCount: true }), take(3)).subscribe(() => {
/* noop */
});
expect(sideEffects).to.deep.equal([0, 1, 2]);
});
const FinalizationRegistry = (global as any).FinalizationRegistry;
if (FinalizationRegistry && global.gc) {
it('should not leak the subscriber for sync sources', (done) => {
let callback: (() => void) | undefined = () => {
/* noop */
};
const registry = new FinalizationRegistry((value: any) => {
expect(value).to.equal('callback');
done();
});
registry.register(callback, 'callback');
const shared = of(42).pipe(shareReplay(1));
shared.subscribe(callback);
callback = undefined;
global.gc?.();
});
} else {
console.warn(`No support for FinalizationRegistry in Node ${process.version}`);
}
it('should be referentially-transparent', () => {
testScheduler.run(({ cold, expectObservable, expectSubscriptions }) => {
const source1 = cold('-1-2-3-4-5-|');
const source1Subs = ' ^----------!';
const expected1 = ' -1-2-3-4-5-|';
const source2 = cold('-6-7-8-9-0-|');
const source2Subs = ' ^----------!';
const expected2 = ' -6-7-8-9-0-|';
// Calls to the _operator_ must be referentially-transparent.
const partialPipeLine = pipe(shareReplay({ refCount: false }));
// The non-referentially-transparent sharing occurs within the _operator function_
// returned by the _operator_ and that happens when the complete pipeline is composed.
const shared1 = source1.pipe(partialPipeLine);
const shared2 = source2.pipe(partialPipeLine);
expectObservable(shared1).toBe(expected1);
expectSubscriptions(source1.subscriptions).toBe(source1Subs);
expectObservable(shared2).toBe(expected2);
expectSubscriptions(source2.subscriptions).toBe(source2Subs);
});
});
});