From f7790276d0d8198333e6a97692d8ee2276194b34 Mon Sep 17 00:00:00 2001 From: Ben Lesh Date: Wed, 4 Nov 2015 14:05:00 -0800 Subject: [PATCH] feat(multicast): support both Subject and subjectFactory arguments relates to #453 --- spec/operators/multicast-spec.js | 24 ++++++++++++++++++++++++ src/operators/multicast.ts | 10 +++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/spec/operators/multicast-spec.js b/spec/operators/multicast-spec.js index 40b012a7ca..9083dc21f5 100644 --- a/spec/operators/multicast-spec.js +++ b/spec/operators/multicast-spec.js @@ -4,6 +4,30 @@ var Observable = Rx.Observable; var Subject = Rx.Subject; describe('Observable.prototype.multicast()', function () { + it('should accept Subjects', function (done) { + var expected = [1,2,3,4]; + + var connectable = Observable.of(1,2,3,4).multicast(new Subject()); + + connectable.subscribe(function (x) { expect(x).toBe(expected.shift()); }, + done.throw, + done); + + connectable.connect(); + }); + + it('should accept Subject factory functions', function (done) { + var expected = [1,2,3,4]; + + var connectable = Observable.of(1,2,3,4).multicast(function () { return new Subject(); }); + + connectable.subscribe(function (x) { expect(x).toBe(expected.shift()); }, + done.throw, + done); + + connectable.connect(); + }); + it('should multicast one observable to multiple observers', function (done) { var results1 = []; var results2 = []; diff --git a/src/operators/multicast.ts b/src/operators/multicast.ts index 3c54bdaadb..9e553887c5 100644 --- a/src/operators/multicast.ts +++ b/src/operators/multicast.ts @@ -1,6 +1,14 @@ import Subject from '../Subject'; import ConnectableObservable from '../observables/ConnectableObservable'; -export default function multicast(subjectFactory: () => Subject) { +export default function multicast(subjectOrSubjectFactory: Subject|(() => Subject)) { + let subjectFactory: () => Subject; + if (typeof subjectOrSubjectFactory === 'function') { + subjectFactory = <() => Subject>subjectOrSubjectFactory; + } else { + subjectFactory = function subjectFactory() { + return >subjectOrSubjectFactory; + }; + } return new ConnectableObservable(this, subjectFactory); }