Skip to content

Instantly share code, notes, and snippets.

@cwharris
Created September 2, 2013 23:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cwharris/6418090 to your computer and use it in GitHub Desktop.
Save cwharris/6418090 to your computer and use it in GitHub Desktop.
Rx.Observable.latestOn
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);
});
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