Web threading made easy
DynWorker is a WebWorker library which makes it easier to work with workers (haha).
You no longer need to create a separate file for each different worker... the only
extra file you will ever have to load is
DynWorker contains a few utilities to augment your worker. You can easily inject functions into the worker, run arbitrary code, pass messages containing mixed data (everything is JSON-encoded), and access DOM storage (local and session).
Development has stopped and DynWorker hasn't been updated in at least three years, but it still works fine and is used in the wild. Pull requests, bug reports, and other questions are very welcome. Take care and have fun!
// If the library is in the current dir and// is named dynworker.js, this works:var worker = ;// Otherwise you need to specify a filename,// but it needs to be on the same domain.var worker = "/js/lib/dynworker.min.js";// You can have a shortcut with Ender:var worker = new $worker;// You can also modify the default path once and for all:DynWorkerpath"path/to/dynworker.js";var worker = ;// The function is namespaced under $.ns// inside the worker.workerinject"funcName"var result = "Do something awesome here";// In-worker helpers are namespaced under $$receive// Receive messages from the main thread;return result;;// The callback gets back the raw event and the// parsed dataworkerreceivedata; // Display and strike awe;// The #run function wraps the function call in a// $.send() so the return value of the// function is sent up.workerrun"funcName" arg1 arg2;// Hence, these two are equivalent:workerrun"funcName";workereval"$.send($.ns['funcName']());";
$.localStorage API mimics the
window.localStorage API, minus
the array-like interface. Under the hood, all calls are asynchronous, but it
doesn't matter too much. All workers and the main thread use the same DOM
$.sessionStorage API is the same.
Inside the worker:
$localStoragesetItem"key" "data";$localStoragegetItem"key"// Do something with that data;
DynWorker is licensed under this MIT License.