Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] DeasyncEndpoint #642

Closed
loynoir opened this issue Aug 25, 2023 · 2 comments
Closed

[feat] DeasyncEndpoint #642

loynoir opened this issue Aug 25, 2023 · 2 comments

Comments

@loynoir
Copy link

loynoir commented Aug 25, 2023

Usage

const dohMathSync = api.doMathAsync.bind(api)

main.mjs

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

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

const dohMathSync = api.doMathAsync.bind(api)

worker.mjs

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

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

Comlink.expose(api, Comlink.DeasyncEndpoint(parentPort))

Description

I think DeasyncEndpoint is possible.

But I don't know how to wrap comlink with SharedArrayBuffer and Atomics.wait.

https://github.com/un-ts/synckit/blob/v0.8.5/src/index.ts#L340-L346

    const sharedBuffer = new SharedArrayBuffer(bufferSize)
    const sharedBufferView = new Int32Array(sharedBuffer)

    const msg: MainToWorkerMessage<Parameters<T>> = { sharedBuffer, id, args }
    worker.postMessage(msg)

    const status = Atomics.wait(sharedBufferView, 0, 0, timeout)
@alexmojaki
Copy link
Contributor

Sounds like you would find https://github.com/pyodide/synclink useful. cc @hoodmane

@loynoir
Copy link
Author

loynoir commented Sep 15, 2023

@alexmojaki

Thanks for the link.

Seems it is what I want.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants