@unitybase/base/worker

Execute a script in a dedicated thread.

The flow:

    var Worker = require('@unitybase/base/worker');
    // create a new thread in suspended state.
    // Evaluate a body of a function runSomething into newly created JavaScript context
    var w =  new Worker({name: 'WorkerName', onmessage: runSomething});
    // resume the thread and call a `onmessage` function with parameter, passed to postMessage
    w.postMessage({action: 'start', param: 'bla-bla'}); // wake up the thread and call a

If onmessage handler execution fail then worker call onerror handler. When thread terminates and Terminate handler assigned worker thread call onterminate handler.

In handlers you can use 2 methods:

  • postMessage(message) for posting messages from worker thread. You can get this message by function getMessage of worker object
  • terminate() for terminating current worker thread
Author:
  • v.orel

Classes

Worker