I have the following situation:
Alpha, can make changes to a window variable, e.g. window.App.messages.Beta, needs to execute a given function but only after window.App.messages has changed.We have the following constraints:
window.Beta to change window.App.messages in any way.window.App.messages should be agnostic with regards to which component is going to set and get its data.I found a number of relevant Stack Overflow threads, but they all seem to suggest solutions that don't actually work to solve this use case. Specifically:
Proxy (example 1, example 2). The problem with Proxy is that you then need to listen to changes to the proxy and not the original object. This implementation wouldn't work as it would require Alpha and Beta to communicate directly. It breaks [A].setters and getters via defineProperty (example). This solution requires Beta to change the original object and that's not okay. It breaks both [B] and [C].setters and getters. This too doesn't work since it breaks [C].What I would need is something like this:
var window.App.messages = ["Hello, world!"];
function logChange(variable) {
console.log(`New value: ${variable.at(-1)}`)
};
var varListener = Listener(window.App.messages, logChange);
window.app.messages.push("The answer is 42.");
>> New value: The answer is 42.