UNPKG

@reactivex/rxjs

Version:

Reactive Extensions for modern JavaScript

420 lines (410 loc) 24 kB
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <base data-ice="baseUrl" href="../../../"> <title data-ice="title">spec-js/operators/publishReplay-spec.js | RxJS API Document</title> <link type="text/css" rel="stylesheet" href="css/style.css"> <link type="text/css" rel="stylesheet" href="css/prettify-tomorrow.css"> <script src="script/prettify/prettify.js"></script> <script src="script/manual.js"></script> <script data-ice="userScript" src="user/script/0-Rx.js"></script> <script data-ice="userScript" src="user/script/1-devtools-welcome.js"></script> <script data-ice="userScript" src="user/script/2-custom-manual-styles.js"></script> <script data-ice="userScript" src="user/script/3-decision-tree-widget.min.js"></script> <script data-ice="userScript" src="user/script/4-theme-toggler.js"></script> <link data-ice="userStyle" rel="stylesheet" href="user/css/0-main.css"> </head> <body class="layout-container" data-ice="rootContainer"> <header> <a href="./">Home</a> <a href="./manual/index.html" data-ice="manualHeaderLink">Manual</a> <a href="identifiers.html">Reference</a> <a href="source.html">Source</a> <a href="test.html" data-ice="testLink">Test</a> <a data-ice="repoURL" href="https://github.com/ReactiveX/RxJS" class="repo-url-github">Repository</a> <div class="search-box"> <span> <img src="./image/search.png"> <span class="search-input-edge"></span><input class="search-input"><span class="search-input-edge"></span> </span> <ul class="search-result"></ul> </div> </header> <nav class="navigation" data-ice="nav"><div> <ul> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-AsyncSubject">AsyncSubject</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-BehaviorSubject">BehaviorSubject</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-Notification">Notification</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-Observable">Observable</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-ReplaySubject">ReplaySubject</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-Scheduler">Scheduler</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-AnonymousSubject">AnonymousSubject</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-Subject">Subject</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-SubjectSubscriber">SubjectSubscriber</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-Subscriber">Subscriber</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-Subscription">Subscription</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-Rx.Scheduler">Rx.Scheduler</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-Rx.Symbol">Rx.Symbol</a></span></span></li> <li data-ice="doc"><div data-ice="dirPath" class="nav-dir-path">observable</div><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-ConnectableObservable">ConnectableObservable</a></span></span></li> <li data-ice="doc"><div data-ice="dirPath" class="nav-dir-path">observable/dom</div><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-AjaxError">AjaxError</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-AjaxResponse">AjaxResponse</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-AjaxTimeoutError">AjaxTimeoutError</a></span></span></li> <li data-ice="doc"><div data-ice="dirPath" class="nav-dir-path">operator</div><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-GroupedObservable">GroupedObservable</a></span></span></li> <li data-ice="doc"><div data-ice="dirPath" class="nav-dir-path">scheduler</div><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-Action">Action</a></span></span></li> <li data-ice="doc"><div data-ice="dirPath" class="nav-dir-path">util</div><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-ArgumentOutOfRangeError">ArgumentOutOfRangeError</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-EmptyError">EmptyError</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-ObjectUnsubscribedError">ObjectUnsubscribedError</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-TimeoutError">TimeoutError</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-UnsubscriptionError">UnsubscriptionError</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-root">root</a></span></span></li> </ul> </div> </nav> <div class="content" data-ice="content"><h1 data-ice="title">spec-js/operators/publishReplay-spec.js</h1> <pre class="source-code line-number raw-source-code"><code class="prettyprint linenums" data-ice="content">&quot;use strict&quot;; var chai_1 = require(&apos;chai&apos;); var Rx = require(&apos;../../dist/cjs/Rx&apos;); var Observable = Rx.Observable; /** @test {publishReplay} */ describe(&apos;Observable.prototype.publishReplay&apos;, function () { asDiagram(&apos;publishReplay(1)&apos;)(&apos;should mirror a simple source Observable&apos;, function () { var source = cold(&apos;--1-2---3-4--5-|&apos;); var sourceSubs = &apos;^ !&apos;; var published = source.publishReplay(1); var expected = &apos;--1-2---3-4--5-|&apos;; expectObservable(published).toBe(expected); expectSubscriptions(source.subscriptions).toBe(sourceSubs); published.connect(); }); it(&apos;should return a ConnectableObservable-ish&apos;, function () { var source = Observable.of(1).publishReplay(); chai_1.expect(typeof source._subscribe === &apos;function&apos;).to.be.true; chai_1.expect(typeof source.getSubject === &apos;function&apos;).to.be.true; chai_1.expect(typeof source.connect === &apos;function&apos;).to.be.true; chai_1.expect(typeof source.refCount === &apos;function&apos;).to.be.true; }); it(&apos;should do nothing if connect is not called, despite subscriptions&apos;, function () { var source = cold(&apos;--1-2---3-4--5-|&apos;); var sourceSubs = []; var published = source.publishReplay(1); var expected = &apos;-&apos;; expectObservable(published).toBe(expected); expectSubscriptions(source.subscriptions).toBe(sourceSubs); }); it(&apos;should multicast the same values to multiple observers, bufferSize=1&apos;, function () { var source = cold(&apos;-1-2-3----4-|&apos;); var sourceSubs = &apos;^ !&apos;; var published = source.publishReplay(1); var subscriber1 = hot(&apos;a| &apos;).mergeMapTo(published); var expected1 = &apos;-1-2-3----4-|&apos;; var subscriber2 = hot(&apos; b| &apos;).mergeMapTo(published); var expected2 = &apos; 23----4-|&apos;; var subscriber3 = hot(&apos; c| &apos;).mergeMapTo(published); var expected3 = &apos; 3-4-|&apos;; expectObservable(subscriber1).toBe(expected1); expectObservable(subscriber2).toBe(expected2); expectObservable(subscriber3).toBe(expected3); expectSubscriptions(source.subscriptions).toBe(sourceSubs); published.connect(); }); it(&apos;should multicast the same values to multiple observers, bufferSize=2&apos;, function () { var source = cold(&apos;-1-2-----3------4-|&apos;); var sourceSubs = &apos;^ !&apos;; var published = source.publishReplay(2); var subscriber1 = hot(&apos;a| &apos;).mergeMapTo(published); var expected1 = &apos;-1-2-----3------4-|&apos;; var subscriber2 = hot(&apos; b| &apos;).mergeMapTo(published); var expected2 = &apos; (12)-3------4-|&apos;; var subscriber3 = hot(&apos; c| &apos;).mergeMapTo(published); var expected3 = &apos; (23)-4-|&apos;; expectObservable(subscriber1).toBe(expected1); expectObservable(subscriber2).toBe(expected2); expectObservable(subscriber3).toBe(expected3); expectSubscriptions(source.subscriptions).toBe(sourceSubs); published.connect(); }); it(&apos;should multicast an error from the source to multiple observers&apos;, function () { var source = cold(&apos;-1-2-3----4-#&apos;); var sourceSubs = &apos;^ !&apos;; var published = source.publishReplay(1); var subscriber1 = hot(&apos;a| &apos;).mergeMapTo(published); var expected1 = &apos;-1-2-3----4-#&apos;; var subscriber2 = hot(&apos; b| &apos;).mergeMapTo(published); var expected2 = &apos; 23----4-#&apos;; var subscriber3 = hot(&apos; c| &apos;).mergeMapTo(published); var expected3 = &apos; 3-4-#&apos;; expectObservable(subscriber1).toBe(expected1); expectObservable(subscriber2).toBe(expected2); expectObservable(subscriber3).toBe(expected3); expectSubscriptions(source.subscriptions).toBe(sourceSubs); published.connect(); }); it(&apos;should multicast the same values to multiple observers, &apos; + &apos;but is unsubscribed explicitly and early&apos;, function () { var source = cold(&apos;-1-2-3----4-|&apos;); var sourceSubs = &apos;^ ! &apos;; var published = source.publishReplay(1); var unsub = &apos; u &apos;; var subscriber1 = hot(&apos;a| &apos;).mergeMapTo(published); var expected1 = &apos;-1-2-3---- &apos;; var subscriber2 = hot(&apos; b| &apos;).mergeMapTo(published); var expected2 = &apos; 23---- &apos;; var subscriber3 = hot(&apos; c| &apos;).mergeMapTo(published); var expected3 = &apos; 3- &apos;; expectObservable(subscriber1).toBe(expected1); expectObservable(subscriber2).toBe(expected2); expectObservable(subscriber3).toBe(expected3); expectSubscriptions(source.subscriptions).toBe(sourceSubs); // Set up unsubscription action var connection; expectObservable(hot(unsub).do(function () { connection.unsubscribe(); })).toBe(unsub); connection = published.connect(); }); it(&apos;should not break unsubscription chains when result is unsubscribed explicitly&apos;, function () { var source = cold(&apos;-1-2-3----4-|&apos;); var sourceSubs = &apos;^ ! &apos;; var published = source .mergeMap(function (x) { return Observable.of(x); }) .publishReplay(1); var subscriber1 = hot(&apos;a| &apos;).mergeMapTo(published); var expected1 = &apos;-1-2-3---- &apos;; var subscriber2 = hot(&apos; b| &apos;).mergeMapTo(published); var expected2 = &apos; 23---- &apos;; var subscriber3 = hot(&apos; c| &apos;).mergeMapTo(published); var expected3 = &apos; 3- &apos;; var unsub = &apos; u &apos;; expectObservable(subscriber1).toBe(expected1); expectObservable(subscriber2).toBe(expected2); expectObservable(subscriber3).toBe(expected3); expectSubscriptions(source.subscriptions).toBe(sourceSubs); // Set up unsubscription action var connection; expectObservable(hot(unsub).do(function () { connection.unsubscribe(); })).toBe(unsub); connection = published.connect(); }); describe(&apos;with refCount()&apos;, function () { it(&apos;should connect when first subscriber subscribes&apos;, function () { var source = cold(&apos;-1-2-3----4-|&apos;); var sourceSubs = &apos; ^ !&apos;; var replayed = source.publishReplay(1).refCount(); var subscriber1 = hot(&apos; a| &apos;).mergeMapTo(replayed); var expected1 = &apos; -1-2-3----4-|&apos;; var subscriber2 = hot(&apos; b| &apos;).mergeMapTo(replayed); var expected2 = &apos; 23----4-|&apos;; var subscriber3 = hot(&apos; c| &apos;).mergeMapTo(replayed); var expected3 = &apos; 3-4-|&apos;; expectObservable(subscriber1).toBe(expected1); expectObservable(subscriber2).toBe(expected2); expectObservable(subscriber3).toBe(expected3); expectSubscriptions(source.subscriptions).toBe(sourceSubs); }); it(&apos;should disconnect when last subscriber unsubscribes&apos;, function () { var source = cold(&apos;-1-2-3----4-|&apos;); var sourceSubs = &apos; ^ ! &apos;; var replayed = source.publishReplay(1).refCount(); var subscriber1 = hot(&apos; a| &apos;).mergeMapTo(replayed); var unsub1 = &apos; ! &apos;; var expected1 = &apos; -1-2-3-- &apos;; var subscriber2 = hot(&apos; b| &apos;).mergeMapTo(replayed); var unsub2 = &apos; ! &apos;; var expected2 = &apos; 23---- &apos;; expectObservable(subscriber1, unsub1).toBe(expected1); expectObservable(subscriber2, unsub2).toBe(expected2); expectSubscriptions(source.subscriptions).toBe(sourceSubs); }); it(&apos;should NOT be retryable&apos;, function () { var source = cold(&apos;-1-2-3----4-#&apos;); // const sourceSubs = &apos;^ !&apos;; var published = source.publishReplay(1).refCount().retry(3); var subscriber1 = hot(&apos;a| &apos;).mergeMapTo(published); var expected1 = &apos;-1-2-3----4-(444#)&apos;; var subscriber2 = hot(&apos; b| &apos;).mergeMapTo(published); var expected2 = &apos; 23----4-(444#)&apos;; var subscriber3 = hot(&apos; c| &apos;).mergeMapTo(published); var expected3 = &apos; 3-4-(444#)&apos;; expectObservable(subscriber1).toBe(expected1); expectObservable(subscriber2).toBe(expected2); expectObservable(subscriber3).toBe(expected3); // expectSubscriptions(source.subscriptions).toBe(sourceSubs); }); it(&apos;should NOT be repeatable&apos;, function () { var source = cold(&apos;-1-2-3----4-|&apos;); // const sourceSubs = &apos;^ !&apos;; var published = source.publishReplay(1).refCount().repeat(3); var subscriber1 = hot(&apos;a| &apos;).mergeMapTo(published); var expected1 = &apos;-1-2-3----4-(44|)&apos;; var subscriber2 = hot(&apos; b| &apos;).mergeMapTo(published); var expected2 = &apos; 23----4-(44|)&apos;; var subscriber3 = hot(&apos; c| &apos;).mergeMapTo(published); var expected3 = &apos; 3-4-(44|)&apos;; expectObservable(subscriber1).toBe(expected1); expectObservable(subscriber2).toBe(expected2); expectObservable(subscriber3).toBe(expected3); // expectSubscriptions(source.subscriptions).toBe(sourceSubs); }); }); it(&apos;should multicast one observable to multiple observers&apos;, 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); }); chai_1.expect(results1).to.deep.equal([]); chai_1.expect(results2).to.deep.equal([]); connectable.connect(); chai_1.expect(results1).to.deep.equal([1, 2, 3, 4]); chai_1.expect(results2).to.deep.equal([1, 2, 3, 4]); chai_1.expect(subscriptions).to.equal(1); done(); }); it(&apos;should replay as many events as specified by the bufferSize&apos;, 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(2); connectable.subscribe(function (x) { results1.push(x); }); chai_1.expect(results1).to.deep.equal([]); chai_1.expect(results2).to.deep.equal([]); connectable.connect(); connectable.subscribe(function (x) { results2.push(x); }); chai_1.expect(results1).to.deep.equal([1, 2, 3, 4]); chai_1.expect(results2).to.deep.equal([3, 4]); chai_1.expect(subscriptions).to.equal(1); done(); }); it(&apos;should emit replayed values and resubscribe to the source when &apos; + &apos;reconnected without source completion&apos;, function () { 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(2); var subscription1 = connectable.subscribe(function (x) { results1.push(x); }); chai_1.expect(results1).to.deep.equal([]); chai_1.expect(results2).to.deep.equal([]); connectable.connect().unsubscribe(); subscription1.unsubscribe(); chai_1.expect(results1).to.deep.equal([1, 2, 3, 4]); chai_1.expect(results2).to.deep.equal([]); chai_1.expect(subscriptions).to.equal(1); var subscription2 = connectable.subscribe(function (x) { results2.push(x); }); connectable.connect().unsubscribe(); subscription2.unsubscribe(); chai_1.expect(results1).to.deep.equal([1, 2, 3, 4]); chai_1.expect(results2).to.deep.equal([3, 4, 1, 2, 3, 4]); chai_1.expect(subscriptions).to.equal(2); }); it(&apos;should emit replayed values plus completed when subscribed after completed&apos;, 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(2); connectable.subscribe(function (x) { results1.push(x); }); chai_1.expect(results1).to.deep.equal([]); chai_1.expect(results2).to.deep.equal([]); connectable.connect(); chai_1.expect(results1).to.deep.equal([1, 2, 3, 4]); chai_1.expect(results2).to.deep.equal([]); chai_1.expect(subscriptions).to.equal(1); connectable.subscribe(function (x) { results2.push(x); }, function (x) { done(new Error(&apos;should not be called&apos;)); }, function () { chai_1.expect(results2).to.deep.equal([3, 4]); done(); }); }); it(&apos;should multicast an empty source&apos;, function () { var source = cold(&apos;|&apos;); var sourceSubs = &apos;(^!)&apos;; var published = source.publishReplay(1); var expected = &apos;|&apos;; expectObservable(published).toBe(expected); expectSubscriptions(source.subscriptions).toBe(sourceSubs); published.connect(); }); it(&apos;should multicast a never source&apos;, function () { var source = cold(&apos;-&apos;); var sourceSubs = &apos;^&apos;; var published = source.publishReplay(1); var expected = &apos;-&apos;; expectObservable(published).toBe(expected); expectSubscriptions(source.subscriptions).toBe(sourceSubs); published.connect(); }); it(&apos;should multicast a throw source&apos;, function () { var source = cold(&apos;#&apos;); var sourceSubs = &apos;(^!)&apos;; var published = source.publishReplay(1); var expected = &apos;#&apos;; expectObservable(published).toBe(expected); expectSubscriptions(source.subscriptions).toBe(sourceSubs); published.connect(); }); }); //# sourceMappingURL=publishReplay-spec.js.map</code></pre> </div> <footer class="footer"> Generated by <a href="https://esdoc.org">ESDoc<span data-ice="esdocVersion">(0.4.8)</span></a> </footer> <script src="script/search_index.js"></script> <script src="script/search.js"></script> <script src="script/pretty-print.js"></script> <script src="script/inherited-summary.js"></script> <script src="script/test-summary.js"></script> <script src="script/inner-link.js"></script> <script src="script/patch-for-local.js"></script> </body> </html>