tinylibs / tinylet

🎨 redlet(), greenlet(), bluelet(), and more threading helpers for web Workers
MIT License
34 stars 0 forks source link

[Milestone] v1 #1

Open jcbhmr opened 1 year ago

jcbhmr commented 1 year ago
Aslemammad commented 1 year ago

Shall we change the name? @jcbhmr

jcbhmr commented 1 year ago

For the project itself? You can pick whatever name you want! 😊 I couldn't think of anything better than "tinythreadlet" on the spot when I made this thing 🤣

Aslemammad commented 1 year ago

tinylet maybe? it's shorter?

jcbhmr commented 1 year ago

Sounds great! 🚀

jcbhmr commented 1 year ago

Bun doesn't support workers yet. https://github.com/oven-sh/bun/issues/901

jcbhmr commented 1 year ago

@Aslemammad I can't access the Settings menu to add my NPM_TOKEN secret. Would you like to handle that, or how should I go about doing that? I've invited you on npmjs.com to be a "Maintainer" of the package: image

jcbhmr commented 1 year ago

Heyyyy @jimmywarting! I notice you also made something similar to the redlet() function that I have in here. https://github.com/jimmywarting/await-sync

If @Aslemammad is willing (I don't have access to the Settings tab) I think it'd be great to add you as a "collaborator" or whatever it's called (you don't have to though ❤️) since you seem to know a thing-or-two about worker sync SharedArrayBuffer magic. 😊

Aslemammad commented 1 year ago

@jcbhmr I changed your access, try again now.

Aslemammad commented 1 year ago

I'm ok with adding Jimmy to the collaborators, in fact, I'd be happy.

jcbhmr commented 1 year ago

image 👍 Added granular NPM_TOKEN 300 day expiry perms to read/write "tinylet" package only

jimmywarting commented 1 year ago

Hi, yes my await-sync pkg dose similar thing to redlet()

I'm only going to comment on redlet() and give some feedback cuz that's what i kind of have built...


One key differences are that mine dose only allocated a fixed amount of SharedArrayBuffer and dose a little dance back and forth to kind of stream the data over to the other thread (sync) instead of using the grow method. (my tough was to be more memory friendly) so the receiver (caller) allocated a fixed amount of Uint8Array and the worker "writes" to it chunk-by-chunk. Mine also don't assume any specific serialization which is left onto the developer in order to support other stuff.

const buf2text = TextDecoder.prototype.decode.bind(new TextDecoder)
const deserialize = uint8 => JSON.parse(buf2text(uint8))

const get = awaitSync(async url => {
    const res = await fetch(url)
  const text = await res.arrayBuffer()

  return new Uint8Array(text) // Must always return a Uint8Array
}, deserialize) // Optional deserialize function here as 2nd argument, otherwise it returns uint8arr

const json = get('https://httpbin.org/get') // JSON

I found this:

const f = redlet(async (u) => {
  const response = await fetch(u);
  return await response.json();
})

to be redundant to have to parse the response from uint8array -> text -> object -> text -> uint8array in the worker when it can just be passed over in a more direct approach instead. On the plus side it's more ergonomic. on the bad side it's less versatile to support other non-json stuff like a protobuf or file content or any kind of binary.

Make it work with transferable objects This is going to be tuff depending on what you mean by this...

if it's about transferring functions argument then that is a peace of cake. for the return body it will be harder.

the only thing that is going to support being transferable are the functions argument when you send a postMessage and order the worker to do work... they can be transfered over. but in get something that's a transferable object back from a return statement in a sync manner would be to use the NodeJS specific receiveMessageOnPort but that's not going to work outside of NodeJS. the only way to transfer something synchronous over worker threads is to use sharedArrayBuffer as you might have already guessed by now... for this reasons i recommend using cbor-x to be able to (de)serialize it back and forth...