Skip to content

Commit

Permalink
Added this to the basic fall function.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina committed Jun 17, 2015
1 parent 9b746fa commit cfc829b
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 13 deletions.
28 changes: 25 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ npm install fastfall --save
## Usage

```js
var fall = require('./')()
var fall = require('fastfall')()

fall([
function a (cb) {
Expand All @@ -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) {
Expand All @@ -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
Expand Down
28 changes: 19 additions & 9 deletions fall.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand All @@ -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()
}
Expand All @@ -57,18 +64,20 @@ 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)
}
}

function noop () {}

function Holder (context, release) {
function Holder (release) {
this.list = empty
this.callback = noop
this.count = 0
this.context = undefined

var that = this

Expand All @@ -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)
Expand Down
21 changes: 20 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, [
Expand All @@ -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')
})
})

0 comments on commit cfc829b

Please sign in to comment.