Nodejs processes callbacks added by process.nextTick in a queue (=> LIFO order). Lowjs uses a stack (=>FIFO order).
This causes issues, e.g in readable-streams, where I had 'readable' events after the 'end' event, which in turn caused a deadlock in code unprepared for this event order.
Demo code to check lowjs and nodejs behavior:
function a () {
console.log('a');
process.nextTick(b);
process.nextTick(b);
}
function b () {
console.log('b');
process.nextTick(c, '1');
process.nextTick(c, '2');
}
function c (arg) {
console.log('c', arg);
}
a();
This PR fixes the order used by lowjs by inserting new callbacks at the bottom of the stack. While this is suboptimal for a large number of callbacks, in real world code the number of callbacks usually is not that large, so the O(n²) behaviour of the insertion method should not matter.
Nodejs processes callbacks added by process.nextTick in a queue (=> LIFO order). Lowjs uses a stack (=>FIFO order). This causes issues, e.g in readable-streams, where I had 'readable' events after the 'end' event, which in turn caused a deadlock in code unprepared for this event order.
Demo code to check lowjs and nodejs behavior:
This PR fixes the order used by lowjs by inserting new callbacks at the bottom of the stack. While this is suboptimal for a large number of callbacks, in real world code the number of callbacks usually is not that large, so the O(n²) behaviour of the insertion method should not matter.