Class: module.exports


An actor to exchange data from main-thread to workers contains code from [mapbox-gl-js](

new worker.module.exports() [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 =;
        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