malory
malory is a web worker manager, written in CoffeeScript which handles instantiation, messaging, and destruction of a collection of web workers.
Public API
For full code documentation, including private methods and objects, see malory.litcoffee
Constructor
maloryInstance = config
malory.demand
maloryInstancedemanddemandworkerArgumentsthen # Iterate over the response array, perhaps coalescing the results
malory.killAllWorkers
maloryInstancekillAllWorkers
Example
Main Thread
workerConfig = workerUrl: "scripts/employee.js" name: "hrEmployee" initialDemand: "initialize worker" budgetedWorkers: 10 officiallyOutOfMemory: "we are officially out of memory" workerArguments: 'memoryLimit': 700*1024 workerUrl: "scripts/employee.js" name: "fieldEmployee" initialDemand: "initialize worker" budgetedWorkers: 10 officiallyOutOfMemory: "we are officially out of memory" workerArguments: 'memoryLimit': 700*1024 maloryInstance = workerConfig demand = 'bring me a gin and tonic'workerArguments = 'ginBrand':'tanqueray'maloryInstancedemanddemandworkerArgumentsthen for drinki in drinkArray consolelog 'I am having gin and tonic number ' + i
The Web Worker (employee.coffee)
selfaddEventListener "message" # Extract Arguments demand = edatademand workerArguments = edataworkerArguments memoryLimit = workerArgumentsmemoryLimit # Decide Which Course of Action to Take itemFetched = '' officiallyOutOfMemory = false switch demand when 'initialize worker' then officiallyOutOfMemory = BooleanMathroundMathrandom when 'bring me a gin and tonic' then itemFetched = 'gin and tonic' when 'bring me a monte cristo sandwich' then itemFetched = 'monte cristo' # Respond to malory if itemFetched then workerArguments.itemFetched = itemFetched returnMessage = returnMessage.demand = demand returnMessage.officiallyOutOfMemory = officiallyOutOfMemory returnMessage.workerArguments = workerArguments selfpostMessage returnMessage
Requirements
Malory relies on the JavaScript Promise API. We suggest checking browser compatibility before use and use a polyfill if necessary
NPM Install
Get familiar with node.js if you haven't used it before, then
- $ npm install malory
Contribute
This plugin requires Grunt ~0.4.0
If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins.
-
Clone or fork the source, github.com/glg/malory, then
-
$ npm install
gets you the dependencies
-
$ grunt dev
compiles and then starts a webserver on localhost:8100, and sets up a file watcher
Future Improvements
- Shift to using plain callbacks or a cross-browser promise library, like jQuery.deferred
- Support for the catiline and operative libraries