un-ts/synckit

[feat] comlink endpoint adapter

loynoir opened this issue · 2 comments

Usage

const dohMathSync = api.doMathAsync.bind(api)

main.mjs

import { Worker } from 'worker_threads'
import * as Comlink from 'comlink'
import { Endpoint } from 'synckit'

const worker = new Worker(new URL('./worker.mjs', import.meta.url))
const api = Comlink.wrap(Endpoint(worker))

const dohMathSync = api.doMathAsync.bind(api)

worker.mjs

import { parentPort } from 'worker_threads'
import * as Comlink from 'comlink'
import { Endpoint } from 'synckit'

const api = {
  async doMathAsync() {
    return 4
  }
}

Comlink.expose(api, Endpoint(parentPort))

Related

GoogleChromeLabs/comlink#642

No idea what's your meaning, please describe it in details.

Close in favor of #140