malory
web worker manager
Last updated 6 years ago by steve.quince .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install malory 
SYNC missed versions from official npm registry.

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] (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise#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.

  1. Clone or fork the source, github.com/glg/malory, then

  2. $ npm install

    gets you the dependencies

  3. $ grunt dev

    compiles and then starts a webserver on localhost:8100, and sets up a file watcher

Future Improvements

Current Tags

  • 0.0.5                                ...           latest (6 years ago)

4 Versions

  • 0.0.5                                ...           6 years ago
  • 0.0.4                                ...           6 years ago
  • 0.0.2                                ...           6 years ago
  • 0.0.1                                ...           6 years ago
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 1
Dependencies (9)
Dev Dependencies (8)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |