I have a dedicated web worker who, after receiving the start signal, goes into a long cycle and, based on some start parameters, the cycle will “yield” at certain points of execution.
This is a simplified version of my code.
var mode = null;
var generator = null;
function* loop() {
for(var i=0;i<10000;i++) {
for(var j=0;j<10000;j++) {
if( mode == 'inner' ){
yield 2;
}
}
if( mode == 'outer' ){
yield 1;
}
}
return null;
}
generator = loop();
self.onmessage = function(event) {
var m = event.data;
if(m.operation == 'run') {
mode = m.mode;
generator.next();
}
if(m.operation == 'pause') {
}
}
What I want to do is allow the employee to pause on demand, the problem is that during the cycle the worker will not process messages, and onmessage will not be called, so I can not send the message "pause" which sets the flag, and then I I check this flag in a loop.
What I was thinking of doing was getting my function after each iteration so that the workflow would process the message queue and then resume the function again if there were no pause signals, however this is a bit hacked.
WebWorker , ? , , onmessage()?