Created
September 2, 2013 23:14
-
-
Save cwharris/6418090 to your computer and use it in GitHub Desktop.
Rx.Observable.latestOn
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
var model = new Rx.Subject(); | |
var click = new Rx.Subject(); | |
model.latestOn(click, function (model, e) { return [model, e]; }) | |
.subscribe(function (x) { | |
console.log(x); | |
}); |
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
Rx.Observable.prototype.latestOn = function (trigger, selector) { | |
var source = this; | |
return Rx.Observable.createWithDisposable(function (o) { | |
var hasValue = false, | |
value; | |
var sourceSubscription = source.subscribe(function (x) { | |
latestValue = x; | |
hasValue = true; | |
}); | |
var triggerSubscription = trigger.subscribe( | |
function (y) { | |
if (hasValue) { o.onNext(selector(latestValue, y)); } | |
} | |
); | |
return new Rx.CompositeDisposable(sourceSubscription, triggerSubscription); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment