F
F
ff0xff2019-02-27 11:24:15
Node.js
ff0xff, 2019-02-27 11:24:15

Is it possible to detect array changes in nodejs?

Tell me if there is an opportunity in js to detect the moment of array change?
Let's say I have two synchronous functions, one changes the data in the array and the other should work when the data changes.
At the same time, the option to call the function for processing from the first one is not an option, because the data comes to the array via WebSocket - and I don’t really want to call the second function for processing by timeout. (the speed in ms plays a very important role for me), the ideal solution would be to find out when the data has changed and process it.
What are the solutions for this issue?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Laud, 2019-02-27
@ff0xff

Proxy will help you.
Something like this:

const yourArray = [];

const proxyArray = new Proxy(yourArray, {
    set: (array, key, value) => {
        console.log(`${key} setted to ${value}`);
        array[key] = value;
        return true;
    }
});
// И дальше можно работать с proxyArray, как с yourArray, но с дополнительным поведением
proxyArray[0] = 10;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question