Class: Actor

Actor

An actor to exchange data from main-thread to workers contains code from [mapbox-gl-js](https://github.com/mapbox/mapbox-gl-js)

new worker.Actor() [source]

const workerKey = 'test_worker_key';
    maptalks.registerWorkerAdapter(workerKey, function (exports, global) {
      //will be called only for once when loaded in worker thread
      exports.initialize = function () {
        console.log('[worker] initialized');
      };
      //to receive message from main thread sent by maptalks.worker.Actor
      exports.onmessage = function (message, postResponse) {
        const data = message.data;
        console.log(`[worker] received data : ` + data);
        //send message back to main thread
        //the parameters:
        //error, data, buffers (arraybuffers in data)
        postResponse(null, 'message from worker thread', null);
      };
    });

    const MyActor = class extends maptalks.worker.Actor {
      test(info, cb) {
        //send data to worker thread
        this.send(info, null, cb);
      }
    }

    //must be same with workerKey for maptalks.registerWorkerAdapter
    const actor = new MyActor(workerKey);
    actor.test('hi', (err, data) => {
      //received data from worker thread
      console.log(data);
    });

Methods

  • isActive() [source]

  • If the actor is active
    Returns:
    Boolean:
  • broadcast(data, buffers, cb) [source]

  • Broadcast a message to all Workers.
    Parameter Type Description
    data Object data to send to worker thread
    buffers Array.<ArrayBuffer> arraybuffers in data as transferables
    cb function callback function when received message from worker thread

  • send(data, buffers, cb, workerIdopt) [source]

  • Sends a message from a main-thread to a Worker and call callback when response received.
    Parameter Type Description
    data Object data to send to worker thread
    buffers Array.<ArrayBuffer> arraybuffers in data as transferables
    cb function callback function when received message from worker thread
    workerId opt Number Optional, a particular worker id to which to send this message.

  • receive(message) [source]

  • A listener callback for incoming message from worker thread. SHOULD NOT BE OVERRIDED only if you know what you are doing.
    Parameter Type Description
    message Object response message from worker thread

  • remove() [source]

  • Remove the actor

  • post(data, buffers, targetID) [source]

  • Send a message to a Worker.
    Parameter Type Description
    data Object data to send
    buffers Array.<ArrayBuffer> arraybuffers in data
    targetID Number The ID of the Worker to which to send this message. Omit to allow the dispatcher to choose.
    Returns:
    Number: The ID of the worker to which the message was sent.
  • getDedicatedWorker() [source]

  • Get a dedicated worker in a round-robin fashion