1.13.0 • Published 6 months ago

@wix/editor-platform-transport v1.13.0

Weekly downloads
-
License
UNLICENSED
Repository
-
Last release
6 months ago

Communication between threads

  • Browser – Worker – Browser
  • Browser – Frame – Browser

🏆🏆🏆

  • Transferred API completely copies the transferred objects, so no proxies. This allows to correctly decorate received APIs.
  • No additional utils needed, like comlink.wrap or comlink.proxy
  • Supports passing promises and callbacks between threads without wrapping it with additional decorators
  • Supports catching errors between threads (see src/demo)
  • Super small ~3kb

Worker

import { WorkerConsumerEndpoint } from '@wix/editor-platform-transport';

class WorkerAPI {
  type = 'WORKER_API';

  functionWithCallback(cb: Function) {
    cb(new Promise(resolve => {
      setTimeout(() => {
        resolve('xxx')
      }, 1000)
    }));
  }
}

WorkerConsumerEndpoint.expose(new WorkerAPI());

Browser

import { WorkerHostEndpoint } from '@wix/editor-platform-transport';

const worker = new Worker('../dist/statics/demo/worker.js');

WorkerConsumerEndpoint.connect(worker, (transport) => {
  transport.waitAPI<WorkerAPI>('WORKER_API', async (api) => {
    /**
     * api - object, exposed from the worker
     * it is shape in the main thread is exactly the same as in the worker,
     * so we can easyly decorate it or proxify, for example for tracing
     */
    await api.functionWithCallback(async (promiseFromWorker) => {
      console.log('main-thread', await promiseFromWorker)
    });
  });
});

This example will output `'main-thread xxx' after 1sec.

Expose Multiple APIs

import {ConsumerChannel} from '@wix/editor-platform-transport';


const channel = new ConsumerChannel();

class WorkerAPI {
  type = 'WORKER_API';
}

class ApplicationAPI {
  type = 'APPLICATION_API';
}

channel.expose(new WorkerAPI());
channel.expose(new ApplicationAPI());
1.13.0

6 months ago

1.12.0

6 months ago

1.11.0

7 months ago

1.10.0

11 months ago

1.9.0

11 months ago

1.8.0

1 year ago

1.5.0

1 year ago