Skip to content

Instantly share code, notes, and snippets.

@cboden
Created July 23, 2015 14:49
Show Gist options
  • Save cboden/6a19c90c9824c0e53239 to your computer and use it in GitHub Desktop.
Save cboden/6a19c90c9824c0e53239 to your computer and use it in GitHub Desktop.
Rx.Observable.prototype.changes = function(deleteObs, keySelector) {
var source = this;
return new Rx.AnonymousObservable(function(observer) {
var store = new Map;
return new Rx.CompositeDisposable(
deleteObs.subscribe(function(key) {
var last = store.get(key);
store.delete(key);
last && observer.onNext([undefined, last]);
}),
source.subscribe(
function(ele) {
var key = keySelector(ele);
var last = store.get(key);
store.set(key, ele);
observer.onNext([ele, last]);
},
observer.onError.bind(observer),
observer.onCompleted.bind(observer)
),
Rx.Disposable.create(store.clear.bind(store))
);
}, this);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment