A tiny library for multithreading in browser. The library aims to make the work with worker in browsers easy.
- keep application state in a shared worker and have one source of thruth
- keep api calls, espcially via websocket in shared worker
- keep data normalization in another thread
- separate the ui thread from any heavy computation logic
- build a multiwindow in borwser progressive web applications with single state
- keep saving to local database and syncing everything later to a cloud away from main thread
const task = { module: "console", method: "log", data: { message: "Hello World!" }}
const result = await thread.run(task);
const event = { name: "message", data: { message: "Hello World!" }}
thread.forward(event);
thread.on('name', listener);
It takes the module and method names from the name of the event (aka path), executes the method, awaits and returns the result (trigers the listener)
thread.sub('/name', listener);
Eventually this should look something like goroutines
.
Where you call a method on a module/object/instance as it was in the main thread namesapse.
const result = await module.compute(somedata)
Where somedata
could be a huge object.
But as an idea that still needs some research and performance mesures and as an alternative to using SharedArrayBuffer
.
The shared worker would get some kind of id
or query
to be able to get the data directly from the local data base (eg IndexedDB) to avoid structural cloning between threads.