workerbee

busy bees are srs bees

npm install workerbee
12 downloads in the last week
24 downloads in the last month

workerbee build status

Overview

workerbee helps you create executable objects that also provide direct access to their internal data members. they're highly useful for things like generators, iterators, or counters. busy bees are srs bees.

var workerbee = require("workerbee");

// counter bee
var counter = workerbee(0, function(){
  return this.value += 1;
});

counter();      // 1
counter();      // 2
counter();      // 3
counter.value;  // 3


// override bee
var greeter = workerbee(["welcome", "ようこそ", "word is bond"], function(name) {
  return this.value.shift() + ", " + name;
});

greeter("bob");      // welcome, bob
greeter("naomi");    // ようこそ, naomi
greeter("mastercj"); // word is bond, mastercj


// timer bee
var timer = workerbee(Date.now(), function() {
  var now = Date.now();
  this.delay = now - this.value;
  this.value = now;
  return this.delay;
});

setTimeout(function() {
  timer() + " ms";        // 1009 ms
  timer.delay;            // 1009
}, 1000);


// bee harmony
var roller = workerbee(function(max) {
  return Math.round(Math.random() * (max-1)) + 1;
});

var d6  = workerbee({die: roller}, function() {
  return this.die(6);
});

var d20 = workerbee({die: roller}, function() {
  return this.die(20);
});

var roll = workerbee(function() {
  var dice = [].slice.apply(arguments);
  return this.result = dice.map(function(d){ return d(); });
});

roll(d6, d6);           // [3, 4]
roll(d20, d20);         // [19, 8]
roll(d6, d6, d20, d20); // [5, 2, 15, 14]
roll.result;            // [5, 2, 15, 14]

API

workerbee

var bee = workerbee([context], fn);

Arguments

  • context - optional; can be one of the following types:

    • object - define any data members as you like
    • string - equivalent to {value: "myString"}
    • number - equivalent to {value: 123}
    • Array - equivalent to {value: ["hello", "world", 123]}
  • fn - a function that gets called when the worker bee is executed. All arguments sent to the workerbee are passed to this function.


npm loves you