Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Simplified exports.

  • Loading branch information...
commit cfab9e2b10368ce26ded34c0dcb51d6fb58cdaea 1 parent fdc6035
@dshaw authored
Showing with 26 additions and 23 deletions.
  1. +19 −16 lib/mixture.js
  2. +7 −7 test/mixture.test.js
View
35 lib/mixture.js
@@ -17,15 +17,17 @@ var assert = require('assert')
* Exports
*/
-exports.mix = function () { return new Master() }
-exports.Master = Master
+exports.mix = Mix
exports.Task = Task
/**
* Mix Master
*/
-function Master () {
+function Mix (name) {
+ if (!(this instanceof Mix)) return new Mix(name)
+
+ this.name = name
this.ids = 0
this.tasks = {}
this.workers = []
@@ -37,13 +39,13 @@ function Master () {
* Inherit EventEmitter
*/
-util.inherits(Master, EventEmitter)
+util.inherits(Mix, EventEmitter)
/**
- * Initialize Master
+ * Initialize Mix
*/
-Master.prototype.init = function () {
+Mix.prototype.init = function () {
var self = this
this.on('exit', function(e) {
@@ -76,11 +78,11 @@ Master.prototype.init = function () {
* @api public
*/
-Master.prototype.task = function (name, options) {
+Mix.prototype.task = function (name, options) {
if (!this.tasks[name]) {
options || (options = {})
options.master = this
- options.name = name
+ options.name = this.name
this.tasks[name] = new Task(options)
}
return this.tasks[name]
@@ -93,18 +95,19 @@ Master.prototype.task = function (name, options) {
* @param cb
*/
-Master.prototype.eachWorker = function (task, cb) {
- var _workers = workers
+Mix.prototype.eachWorker = function (task, cb) {
+ var workers = null
- if (arguments.length < 2) {
+ if (typeof cb === 'undefined') {
cb = task
+ workers = this.workers
} else {
- _workers = tasks[task].workers
+ workers = this.tasks[task].workers
}
- for (var id in _workers) {
- if (_workers[id]) {
- cb(_workers[id])
+ for (var id in workers) {
+ if (workers[id]) {
+ cb(workers[id])
}
}
}
@@ -117,6 +120,6 @@ Master.prototype.eachWorker = function (task, cb) {
* @param message
*/
-Master.prototype.onWorkerMessage = function (task, worker, message) {
+Mix.prototype.onWorkerMessage = function (task, worker, message) {
console.log(task.name, worker.id, message)
}
View
14 test/mixture.test.js
@@ -1,29 +1,29 @@
var mixture = require('..')
- , mix = mixture.mix()
- , Master = mixture.Master
+ , Mix = mixture.mix
+ , mix = Mix()
, Task = mixture.Task
, tap = require('tap')
, test = tap.test
-console.log(mix, Master, Task, mix instanceof Master)
+console.log(mix, Task, mix instanceof Mix)
test('mixture exports', function (t) {
- t.ok(Master, 'exports Master')
+ t.ok(Mix, 'exports Mix')
t.ok(Task, 'exports Task')
var version = require('../package.json').version
t.equal(mixture.version, version, 'exports version')
- t.isa(mix, Master, 'mix is an instance of Master')
+ t.isa(mix, Mix, 'mix is an instance of Mix')
t.ok(mix.tasks, 'initialized tasks hash')
t.ok(mix.workers, 'initialized workers list')
- var master = new Master()
+ var master = new Mix()
var task = master.task('name')
t.isa(master.task, 'function', 'task method defined')
t.isa(task, Task, 'task returns a task')
t.ok(task.master, 'task has a reference to the master')
- t.isa(task.master, Master, 'task master is an instance of Master')
+ t.isa(task.master, Mix, 'task master is an instance of Master')
master.on('death', function() {
console.log('I died', arguments)
Please sign in to comment.
Something went wrong with that request. Please try again.