diff --git a/README.md b/README.md index ceb01d3..9c6d35c 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ npm install fastfall --save ## Usage ```js -var fall = require('./')() +var fall = require('fastfall')() fall([ function a (cb) { @@ -49,11 +49,11 @@ fall([ }) ``` -You can also set `this` in the functions: +You can also set `this` when you create a fall: ```js var that = { hello: 'world' } -var fall = require('./')(that) +var fall = require('fastfall')(that) fall([ function a (cb) { @@ -73,6 +73,28 @@ fall([ }) ``` +You can also set `this` when you run a task: + +var that = { hello: 'world' } +var fall = require('fastfall')() + +fall(that, [ + function a (cb) { + console.log(this) + console.log('called a') + cb(null, 'a') + }, + function b (a, cb) { + console.log('called b with:', a) + cb(null, 'a', 'b') + }, + function c (a, b, cb) { + console.log('called c with:', a, b) + cb(null, 'a', 'b', 'c') + }], function result (err, a, b, c) { + console.log('result arguments', arguments) + }) + ### Compile a waterfall ```js diff --git a/fall.js b/fall.js index 233b34e..599cc5b 100644 --- a/fall.js +++ b/fall.js @@ -9,7 +9,7 @@ function fastfall (context, template) { context = null } - var head = new Holder(context, release) + var head = new Holder(release) var tail = head return template ? compiled : fall @@ -20,7 +20,7 @@ function fastfall (context, template) { if (holder.next) { head = holder.next } else { - head = new Holder(context, release) + head = new Holder(release) tail = head } @@ -29,11 +29,18 @@ function fastfall (context, template) { return holder } - function fall (list, done) { + function fall () { var current = next() - current.list = list - current.callback = done + if (arguments.length === 3) { + current.context = arguments[0] + current.list = arguments[1] + current.callback = arguments[2] || noop + } else { + current.context = context + current.list = arguments[0] + current.callback = arguments[1] || noop + } current.work() } @@ -57,7 +64,8 @@ function fastfall (context, template) { args[i + 1] = arguments[i] } - current.callback = arguments[i] + current.context = context + current.callback = arguments[i] || noop current.work.apply(null, args) } @@ -65,10 +73,11 @@ function fastfall (context, template) { function noop () {} -function Holder (context, release) { +function Holder (release) { this.list = empty this.callback = noop this.count = 0 + this.context = undefined var that = this @@ -86,12 +95,13 @@ function Holder (context, release) { args[i - 1] = arguments[i] } args[args.length] = work - that.list[that.count++].apply(context, args) + that.list[that.count++].apply(that.context, args) } else { for (i = 0; i < arguments.length; i++) { args[i] = arguments[i] } - that.callback.apply(context, args) + that.callback.apply(that.context, args) + that.context = undefined that.list = empty that.count = 0 release(that) diff --git a/test.js b/test.js index 8888290..f3c8a7c 100644 --- a/test.js +++ b/test.js @@ -100,7 +100,7 @@ test('set this', function (t) { }) test('set this in compiled mode', function (t) { - t.plan(3) + t.plan(4) var that = {} var fall = fastfall(that, [ @@ -113,5 +113,24 @@ test('set this in compiled mode', function (t) { fall(42, function result (err, a, b, c) { t.error(err, 'no error') t.equal(a, 42, 'result function 2nd arg matches') + t.equal(this, that, 'this is set') + }) +}) + +test('set this for a normal fall', function (t) { + t.plan(4) + + var that = {} + var fall = fastfall() + + fall(that, [ + function a (cb) { + t.equal(this, that, 'this is set') + cb(null, 'a') + } + ], function result (err, a) { + t.error(err, 'no error') + t.equal(this, that, 'this is set') + t.equal(a, 'a', 'result function 2nd arg matches') }) })