Last active
January 2, 2016 18:26
-
-
Save cdimascio/831332aee5fead4b98dc to your computer and use it in GitHub Desktop.
Adapt ES7 Object.observe to Rx.Observable
This file contains hidden or 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 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); | |
}; | |
}); | |
}; |
This file contains hidden or 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 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