Skip to content

Instantly share code, notes, and snippets.

@tlivings
Last active January 30, 2016 00:28
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 tlivings/112cf3a132439908664d to your computer and use it in GitHub Desktop.
Save tlivings/112cf3a132439908664d to your computer and use it in GitHub Desktop.
RxJS Observable from Object.observe
let obj = {
value: 1
};
Rx.Observable.fromObserving(obj).filter((change) => change.name === 'value').doOnNext((x) => console.log(x)).subscribe();
obj.value = 2;
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);
});
}
@alexvbush
Copy link

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment