You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Create a instance of Wod. It also spawn a new web worker instanceconstWorker_1=newWod();// or Wod.spawn()// Define event listenersconstlistener=(event)=>console.log(event.data);consterrorListener=(event)=>console.log(event.message);// Subscribe to any message from the Worker_1 thread// The same as WebWorker.onmessage = function() {} Worker_1.on('message',listener);// Subscribe to any errors that may occure in the Worker_1 thread// The same as WebWorker.onerror = function() {} Worker_1.on('error',errorListener);// This line execute the function in the Worker_1 thread context and post the // message to the Main thread. As result, message 'Message from Worker_1'// will output to console of the Main threadWorker_1.exec(()=>postMessage('Message from Worker_1'));// Using decorator// Create a decorator that execute the function in the Worker_1 threadletsayHelloToTheMainThread=useThread(Worker_1)(()=>postMessage('Hello from Worker1'));// This will print 'Hello from Worker1' to console of the Main threadsayHelloToTheMainThread();// Catching errors// As we was subscribe to any error event from the Worker_1 above// this will print the error message to the Main thread's consoleWorker_1.exec(()=>throw'Error from Worker_1');// Off event listenerWorker_1.off('message',listener);Worker_1.off('error',errorListener);// Terminate the workerWorker_1.terminate();
Wod
Wod.on(event: string, listener: function): void
Subscribe to event. Currently supported: message, error.
Wod.off(event: string, listener: function): void
Unsubscribe from event.
Wod.exec(prog: function[, args: any[]]): void
Execute prog in web worker thread. Optional args will be passed to prog as arguments.