-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(operator): add publishReplay operator and spec
- Loading branch information
Showing
4 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* globals describe, it, expect */ | ||
var Rx = require('../../dist/cjs/Rx'); | ||
var Observable = Rx.Observable; | ||
|
||
describe('Observable.prototype.publishReplay()', function () { | ||
it('should return a ConnectableObservable', function () { | ||
var source = Observable.value(1).publishReplay(); | ||
expect(source instanceof Rx.ConnectableObservable).toBe(true); | ||
}); | ||
|
||
it('should multicast one observable to multiple observers', function (done) { | ||
var results1 = []; | ||
var results2 = []; | ||
var subscriptions = 0; | ||
|
||
var source = new Observable(function (observer) { | ||
subscriptions++; | ||
observer.next(1); | ||
observer.next(2); | ||
observer.next(3); | ||
observer.next(4); | ||
observer.complete(); | ||
}); | ||
|
||
var connectable = source.publishReplay(); | ||
|
||
connectable.subscribe(function (x) { | ||
results1.push(x); | ||
}); | ||
connectable.subscribe(function (x) { | ||
results2.push(x); | ||
}); | ||
|
||
expect(results1).toEqual([]); | ||
expect(results2).toEqual([]); | ||
|
||
connectable.connect(); | ||
|
||
expect(results1).toEqual([1, 2, 3, 4]); | ||
expect(results2).toEqual([1, 2, 3, 4]); | ||
expect(subscriptions).toBe(1); | ||
done(); | ||
}); | ||
|
||
it('should replay as many events as specified by the bufferSize', function (done) { | ||
var results1 = []; | ||
var results2 = []; | ||
var subscriptions = 0; | ||
|
||
var source = new Observable(function (observer) { | ||
subscriptions++; | ||
observer.next(1); | ||
observer.next(2); | ||
observer.next(3); | ||
observer.next(4); | ||
}); | ||
|
||
var connectable = source.publishReplay(2); | ||
|
||
connectable.subscribe(function (x) { | ||
results1.push(x); | ||
}); | ||
|
||
expect(results1).toEqual([]); | ||
expect(results2).toEqual([]); | ||
|
||
connectable.connect(); | ||
|
||
connectable.subscribe(function (x) { | ||
results2.push(x); | ||
}); | ||
|
||
expect(results1).toEqual([1, 2, 3, 4]); | ||
expect(results2).toEqual([3, 4]); | ||
expect(subscriptions).toBe(1); | ||
done(); | ||
}); | ||
|
||
it('should allow you to reconnect by subscribing again', function (done) { | ||
var expected = [1, 2, 3, 4]; | ||
var i = 0; | ||
|
||
var source = Observable.of(1, 2, 3, 4).publishReplay(); | ||
|
||
source.subscribe( | ||
function (x) { | ||
expect(x).toBe(expected[i++]); | ||
}, | ||
null, | ||
function () { | ||
i = 0; | ||
|
||
source.subscribe(function (x) { | ||
expect(x).toBe(expected[i++]); | ||
}, null, done); | ||
|
||
source.connect(); | ||
}); | ||
|
||
source.connect(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import ReplaySubject from '../subjects/ReplaySubject'; | ||
import Scheduler from '../Scheduler'; | ||
import multicast from './multicast'; | ||
|
||
export default function publishReplay(bufferSize: number = Number.POSITIVE_INFINITY, | ||
windowTime: number = Number.POSITIVE_INFINITY, | ||
scheduler?: Scheduler) { | ||
return multicast.call(this, | ||
() => new ReplaySubject(bufferSize, windowTime, scheduler) | ||
); | ||
} |