Skip to content

Instantly share code, notes, and snippets.

@cdimascio
Last active January 2, 2016 18:26
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 cdimascio/831332aee5fead4b98dc to your computer and use it in GitHub Desktop.
Save cdimascio/831332aee5fead4b98dc to your computer and use it in GitHub Desktop.
Adapt ES7 Object.observe to Rx.Observable
var Observable = Rx.Observable;
Observable.fromObservations = function(obj) {
return Observable.create(function forEach(observer) {
var handler = changes => observer.onNext(changes);
Object.observe(obj, handler);
return function dispose() {
Object.unobserve(obj, handler);
};
});
};
var person = {
name: 'Jim'
};
Observable.
fromObservations(person).
take(1).
forEach(x => console.log(x));
person.age = 23;
person.name = 'Don';
setTimeout(() => person.name = 'Thomas', 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment