It is relatively trivial to watch for changes in an array in Javascript.
One method I use is like this:
// subscribe to add, update, delete, and splice changes Array.observe(viewHelpFiles, function(changes) { // handle changes... in this case, we'll just log them changes.forEach(function(change) { console.log(Object.keys(change).reduce(function(p, c) { if (c !== "object" && c in change) { p.push(c + ": " + JSON.stringify(change[c])); } return p; }, []).join(", ")); }); });
However, I have recently read that Array.observe
is deprecated and we should use the proxy object instead.
How can we detect changes in an array the Proxy object? I am unable to find any examples, anyone interested in elaborating?
In JavaScript, proxies (proxy object) are used to wrap an object and redefine various operations into the object such as reading, insertion, validation, etc. Proxy allows you to add custom behavior to an object or a function.
Description. The Proxy object allows you to create an object that can be used in place of the original object, but which may redefine fundamental Object operations like getting, setting, and defining properties. Proxy objects are commonly used to log property accesses, validate, format, or sanitize inputs, and so on.
The Proxy object is used to define custom behavior for fundamental operations (e.g. property lookup, assignment, enumeration, function invocation, etc). And Reflect in that way: Reflect is a built-in object that provides methods for interceptable JavaScript operations.
A proxy allows you to perform meta-programming operations such as intercepting a call to inspect or change an object's property. The original object the proxy will virtualize.
From what I can read from the MDN page, you can create a general handler where you can handle all the changes to any object.
In a sense, you write an interceptor, that will intervene each time you get a value from the array or set a value. You can then write your own logic to follow the changes.
var arrayChangeHandler = { get: function(target, property) { console.log('getting ' + property + ' for ' + target); // property is index in this case return target[property]; }, set: function(target, property, value, receiver) { console.log('setting ' + property + ' for ' + target + ' with value ' + value); target[property] = value; // you have to return true to accept the changes return true; } }; var originalArray = []; var proxyToArray = new Proxy( originalArray, arrayChangeHandler ); proxyToArray.push('Test'); console.log(proxyToArray[0]); // pushing to the original array won't go through the proxy methods originalArray.push('test2'); // the will however contain the same data, // as the items get added to the referenced array console.log('Both proxy and original array have the same content? ' + (proxyToArray.join(',') === originalArray.join(','))); // expect false here, as strict equality is incorrect console.log('They strict equal to eachother? ' + (proxyToArray === originalArray));
Which then outputs:
getting push for getting length for setting 0 for with value Test setting length for Test with value 1 getting 0 for Test Test
The caveat for the proxy, is that everything which is defined on an object, will be intercepted, which can be observed when using the push
method.
The original object that will be proxied doesn't mutate, and changes done to the original object will not be caught by the proxy.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With