forked from vkurchatkin/deasync
-
-
Notifications
You must be signed in to change notification settings - Fork 75
Open
Description
Looking trough the issue i see that there are a bunch of gyp, electron and loop / freezes and high cpu issues floating around.
What i was looking for was a solution that did not only works in nodejs but also works for other env. too. like Deno, Bun.js and heck even web workers.
And there where basically nothing out there what i could find. everything is so NodeJS specific or poorly implemented that uses eg child processes or worker_threads, so transferring data to other threads where very limited.
So now i introduce to you: await-sync
Here it's in its simplest form:
import { createWorker } from 'to-sync'
const awaitSync = createWorker()
const get = awaitSync(async url => {
const res = await fetch(url)
const ab = await res.arrayBuffer()
return new Uint8Array(ab)
})
const uint8 = get('https://httpbin.org/get') // Uint8ArrayUsing this solution will:
- spin up a new Worker,
- transfer the function stringifyed code to that worker
- and once you call the
getfn, a postMessage is sent to the worker with all the arguments and the current thread will freeze usingAtomic.wait()... The entire event loop is halted. - only the worker can unfreeze it now. now the worker instead dose all the work async.
- and to achieve transferring the content back to the thread without using
onmessageor even using worker.receiveMessageOnPort(port) which isn't so cross env. friendly btw... is achived using only SharedArrayBuffer + Atomic dance.
oyyd
Metadata
Metadata
Assignees
Labels
No labels