3 ms·
I think the thinking has become that while there are many great libraries out there that do some sort of observing changes in an object, it isn't general enough
by WorldMaker 11y ago
I think the thinking has become that while there are many great libraries out there that do some sort of observing changes in an object, it isn't general enough to be a language feature. There was a lot of disagreement on what the observable system should look like and how it should operate. For instance, some of the data-binding frameworks though the proposed spec for Object.observe was too heavyweight as they often don't need every change on an object.
At least for the time being it seems like this sort of thing is still better handled by library choice.
Personally, I felt like Object.observe should have used something more like RxJS Observables than events, and that a base class similar to the RxJS Observable would be great to generally add to the language alongside Iterable and Promise.
- Lazare 11y agoHave you seen the Observables proposal? It's Stage 1 but seems to be moving forward nicely, and it's more-or-less what I think you're looking for. https://github.com/zenparsing/es-observable https://github.com/zenparsing/es-observable
- WorldMaker 11y agoI have seen that and IMHO I think it is something that ES should adopt. Just as Promises can solve the callback mess I think Observables can do a lot to solve the event mess, and given how deeply important events and event processing is to ES (hi DOM, hi EventEmitter), I think that could be huge. (In the meantime RxJS is great and moving at a good pace, but deeper language support would encourage other people to explore it beyond the already converted cultists like myself.)
- api 11y agoCan observables be implemented efficiency without VM support? I can't think of a good way to do it without constant deep comparisons -- effectively "busy waiting" on changes.
- WorldMaker 11y agoSeveral of the libraries out there (including maybe the polymer "polyfill" for Object.observe, observe-js, although I haven't actually checked) use the ES5 tool Object.defineProperty to convert the members on an object to have observable semantics. There is also the new ES6/ES2015 Proxy class that can be used effectively and efficiently for the sort of meta-programming of wiring up an entire object for observation. That said, there's also the tried and true methods of doing things "manually" a property at a time. Knockout and its observable functions remains rather efficient with a very long tail of older browser support.