benbria / object-spy

0 stars 0 forks source link

Monitor property additions and deletions, and even more changes #8

Open bllanos opened 9 years ago

bllanos commented 9 years ago

Current behaviour

The idea solution would be to use Object.observe(), which is part of ECMAScript 7 and not yet available in all environments.

In the meantime, there are many polyfills and full-blown libraries that approximate Object.observe(). After searching the NPM website for "object.observe" and "observe", I have a few favourites so far:

Note that polyfills perform "dirty checking", which is expensive and can be easily misused to cause memory leaks.

Details and Validation

ghost commented 9 years ago

There are a few problems with object.observe (v0.2.4) and observe-js (v0.4.2). The main issue with both libraries is that they do not guarantee that they report operations in the order they occurred. This means that some operations can even not be reported at all (if a delete was reported before a change or add, for example). It seems there is nothing that can be done about this in a Javascript polyfill. While the native implementation of Object.observe seems to preserve order, the Node's Javascript runtime does not support it yet.

There are also some likely bugs with both libraries. The object.observe library seems to continue monitoring objects even when unobserved is called (thus never terminating the script). The objserve-js library seem to have an issue where, if any property of an object that is being observed contains the value 'NaN', then it will over-report on the variable even when it doesn't change.

@bllanos and I have decided that this issue can remain for now. This documentation was provided to aid me or future developers attempting to fix the issue of addition and deletion monitoring.

bllanos commented 9 years ago

For future reference, added properties should not have their accesses checked, as it is possible for them to be accessed before the wrapper code receives notification of their existence (and has a chance to wrap them in getter/setter functions).

This should be mentioned in the documentation of the project.