Consider the following JavaScript Proxy:
const queue = new Proxy([], {
get: (target, property) => {
return target[property];
},
set: (target, property, value) => {
target[property] = value;
this._processQueue();
return true;
}
});
The purpose of the above is to create a queue which is automatically processed whenever an item is added to it.
The problem is that once the queue is processed, I need to call flushQueue to remove the processed items. In other words, I need to empty the queue Proxy array.
Can anyone advise how to do this?
What I have tried...
// Can't do this as a) queue is a constant, and b) it overrides the Proxy
queue = [];
// For some reason this doesn't work
queue.length = 0;
// This empties the array but for some reason does not reset the length...
queue.splice(0, queue.length);
Update
Please see my full example here:
class Foo {
/**
* Foo constructor.
*
* @return void
*/
constructor() {
this.queue = new Proxy([], {
get: (target, property) => {
return target[property];
},
set: (target, property, value) => {
this._processQueue();
target[property] = value;
return true;
}
});
}
/**
* Add an event to the queue.
*
* @param {object} event
* @return void
*/
_addToQueue(event) {
this.queue.push(event);
}
/**
* Process the event queue.
*
* @return void
*/
_processQueue() {
console.log('process queue', this.queue, this.queue.length);
if (this.queue.length) {
this.queue.forEach((event, index) => {
console.log(event);
const method = this._resolveEvent(event.type);
const payload = typeof event.payload !== 'undefined' ? event.payload : {};
//this[method](payload);
});
this._flushQueue();
}
}
/**
* Flush the event queue.
*
* @return void
*/
_flushQueue() {
this.queue.splice(0, this.queue.length);
}
}
this.queue = [];then it will remove theProxyand just be a normal array. Or am I wrong? It seemed to do that when I did it before....this._processQueue();inside set trap, it seems to create a infinite loopsplicedoes not seem to be resetting the length of the array when I remove the items, thereforeif (this.queue.length)always evaluates to true once an item has been added. Does that make sense?this._processQueueyou're calling it before setting the value, whereas you should call it after setting the value