Sunday 18 November 2018

Question. Handle one event at a time.

So, lets say I have an event with a bunch of listeners. But among those there is one special - it is async, and, if this event is fired while previous special listeners are still not finished, I want it to wait for that previous listeners to resolve. So, these listeners can only work one after another. In the end, I came up with this thing:function queueify(handler) { handler.queue = []; handler.locked = false; async function dummy(...args) { handler.queue.push(args) if(handler.locked) return; else { handler.locked = true; while(handler.queue.length) { args = handler.queue.shift(); await handler(...args) } handler.locked = false; } } return dummy; } While it works, it looks over-complicated, I am sure there is a better way to do my task.

Submitted November 18, 2018 at 10:22PM by Haalonean

No comments:

Post a Comment