malory v0.0.5
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 = new malory(config)
malory.demand
maloryInstance.demand(demand, workerArguments).then (responseArray) ->
# Iterate over the response array, perhaps coalescing the results
malory.killAllWorkers
maloryInstance.killAllWorkers()
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 = new malory workerConfig
demand = 'bring me a gin and tonic'
workerArguments = {'ginBrand':'tanqueray'}
maloryInstance.demand(demand, workerArguments).then (drinkArray) ->
for drink, i in drinkArray
console.log 'I am having gin and tonic number ' + i
The Web Worker (employee.coffee)
self.addEventListener "message", ((e) ->
# Extract Arguments
demand = e.data.demand
workerArguments = e.data.workerArguments
memoryLimit = workerArguments.memoryLimit
# Decide Which Course of Action to Take
itemFetched = ''
officiallyOutOfMemory = false
switch demand
when 'initialize worker' then officiallyOutOfMemory = Boolean(Math.round(Math.random()))
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
self.postMessage 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