Last active
January 30, 2016 00:28
-
-
Save tlivings/112cf3a132439908664d to your computer and use it in GitHub Desktop.
RxJS Observable from Object.observe
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
let obj = { | |
value: 1 | |
}; | |
Rx.Observable.fromObserving(obj).filter((change) => change.name === 'value').doOnNext((x) => console.log(x)).subscribe(); | |
obj.value = 2; |
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
Rx.Observable.fromObserving = (obj) => { | |
return Rx.Observable.create((observer) => { | |
var handler = (changes) => { | |
changes.forEach((x) => observer.onNext(x)); | |
observer.onCompleted(); | |
}; | |
Object.observe(obj, handler); | |
return () => Object.unobserve(obj, handler); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hey @tlivings thanks for the snippet! do you know if there is an easier way (a shortcut?) to observe a property on an object? It feels like such a simple task but I can't find how to do it with RxJS (other RxSwift and others do have it though)