Skip to content

Commit

Permalink
feat(multicast): support both Subject and subjectFactory arguments
Browse files Browse the repository at this point in the history
relates to #453
  • Loading branch information
benlesh committed Nov 5, 2015
1 parent c5cc06f commit f779027
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
24 changes: 24 additions & 0 deletions spec/operators/multicast-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down
10 changes: 9 additions & 1 deletion src/operators/multicast.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import Subject from '../Subject';
import ConnectableObservable from '../observables/ConnectableObservable';

export default function multicast<T>(subjectFactory: () => Subject<T>) {
export default function multicast<T>(subjectOrSubjectFactory: Subject<T>|(() => Subject<T>)) {
let subjectFactory: () => Subject<T>;
if (typeof subjectOrSubjectFactory === 'function') {
subjectFactory = <() => Subject<T>>subjectOrSubjectFactory;
} else {
subjectFactory = function subjectFactory() {
return <Subject<T>>subjectOrSubjectFactory;
};
}
return new ConnectableObservable(this, subjectFactory);
}

0 comments on commit f779027

Please sign in to comment.