javascript - Merge implemented as flatMap -
theoretically should possible implement rxjs operator (except just() , flatmap()) through flatmap(). instance map() can implemented as
function map(source, selector) { return source.flatmap(x => rx.observable.just(selector(x))); } how implement merge() through flatmap()? (avoiding mergeall() too, of course)
it looks possible if take advantage of fact flatmap can take array return values.
rx.observable.prototype.merge = function(other) { var source = this; return rx.observable.just([source, other]) //flattens array observable of observables .flatmap(function(arr) { return arr; }) //flatten out observables .flatmap(function(x) { return x; }); } i wanted erik trolling :).
var source1 = rx.observable.interval(2000).timestamp().map(function(x) { return "interval 1 @ " + x.timestamp + " w/ " + x.value; }); var source2 = rx.observable.interval(3000).timestamp().map(function(x) { return "interval 2 @ " + x.timestamp + " w/ " + x.value; }); rx.observable.prototype.mergefromflatmap = function(other) { var source = this; return rx.observable.just([source, other]) .flatmap(function(arr) { return arr; }) .flatmap(function(seq) { return seq; }); }; source1.mergefromflatmap(source2).take(20).subscribe(console.log.bind(console)); <script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/2.5.3/rx.all.js"></script>
Comments
Post a Comment