Skip to content

Commit 30a2f05

Browse files
committed
Support custom runner
1 parent a286a0e commit 30a2f05

File tree

6 files changed

+137
-21
lines changed

6 files changed

+137
-21
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,5 +242,19 @@ Type: `Boolean`
242242

243243
Default: `true`
244244

245+
##### run
246+
Specify a runner function to run each callback.
247+
248+
Type: `Function`, `Object`
249+
250+
Default: `null`
251+
252+
If `Function`, it receives a callback followed by a list of arguments,
253+
and should return a promise to fetch the results (`Array`).
254+
255+
If `Object`, it is passed to
256+
[`Runner of run-callback`](https://github.com/zoubin/run-callback#runner--runrunner)
257+
to create a runner function.
258+
245259
## [Changelog](changelog.md)
246260

lib/runner.js

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,48 @@ function Runner(opts) {
99
opts = opts || {}
1010
this.input = opts.input !== false
1111
this.output = opts.output !== false
12+
if (typeof opts.run === 'function') {
13+
this._run = opts.run
14+
} else {
15+
this._runner = runCallback.Runner(opts.run)
16+
}
1217
}
1318

1419
Runner.prototype.normalizeArgs = function(args) {
15-
return this.input && defined(args) ? args : []
20+
if (!this.input || !defined(args)) return []
21+
return Array.isArray(args) ? args : [args]
22+
}
23+
24+
Runner.prototype.normalizeResult = function(res) {
25+
return this.output ? res : []
1626
}
1727

1828
Runner.prototype.run = function(cb, args) {
19-
args = this.normalizeArgs(args)
20-
return runCallback.apply(null, [cb].concat(args))
29+
args = [cb].concat(args)
30+
return Promise.resolve(this).then(function (runner) {
31+
return runner._run.apply(runner, args)
32+
})
33+
}
34+
35+
Runner.prototype._run = function(cb) {
36+
return this._runner.thunkify(cb)
37+
.apply(this._runner, slice(arguments, 1))
2138
}
2239

2340
Runner.prototype.sequence = function(cbs, args, i) {
24-
i = ~~i
41+
if (!Array.isArray(cbs)) cbs = [cbs]
42+
args = this.normalizeArgs(args)
2543

26-
// NOTE: tasks can be pushed to `cbs` dynamically
44+
i = ~~i
2745
var cb = cbs[i]
46+
47+
// **NOTE**: tasks can be pushed to `cbs` dynamically
48+
if (i < cbs.length) ++i
49+
2850
var ret
2951
if (typeof cb === 'function') {
30-
++i
3152
ret = this.run(cb, args)
3253
} else if (Array.isArray(cb)) {
33-
++i
3454
ret = this.parallel(cb, args)
3555
} else {
3656
ret = Promise.resolve(args)
@@ -41,12 +61,14 @@ Runner.prototype.sequence = function(cbs, args, i) {
4161
if (i < cbs.length) {
4262
return self.sequence(cbs, res, i)
4363
}
44-
if (self.output) return res
64+
return self.normalizeResult(res)
4565
})
4666
}
4767

4868
Runner.prototype.parallel = function(cbs, args) {
49-
var self = this
69+
if (!Array.isArray(cbs)) cbs = [cbs]
70+
args = this.normalizeArgs(args)
71+
5072
return Promise.all(cbs.map(function (cb) {
5173
if (typeof cb === 'function') {
5274
return this.run(cb, args)
@@ -56,9 +78,7 @@ Runner.prototype.parallel = function(cbs, args) {
5678
}
5779
return Promise.resolve(args)
5880
}, this))
59-
.then(function (res) {
60-
if (self.output) return res
61-
})
81+
.then(this.normalizeResult.bind(this))
6282
}
6383

6484
Runner.prototype.thunkify = function() {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
},
2727
"homepage": "https://github.com/zoubin/callback-sequence#readme",
2828
"dependencies": {
29-
"run-callback": "^3.0.0"
29+
"run-callback": "^3.1.0"
3030
},
3131
"devDependencies": {
3232
"eslint": "^1.10.1",

test/custom-runner.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
var test = require('tap').test
2+
var Runner = require('..').Runner
3+
4+
test('custom run function', function(t) {
5+
var ctx = {}
6+
var runner = Runner({
7+
run: function (cb) {
8+
return [cb.apply(ctx, Array.prototype.slice.call(arguments, 1))]
9+
},
10+
})
11+
12+
return runner.sequence([
13+
function () {
14+
t.equal(this, ctx)
15+
return 1
16+
},
17+
function (a) {
18+
t.equal(a, 1)
19+
t.equal(this, ctx)
20+
return 2
21+
},
22+
]).then(function (res) {
23+
t.same(res, [2])
24+
})
25+
})
26+
27+
test('custom runner option', function(t) {
28+
var runner = Runner({
29+
run: { async: false },
30+
})
31+
32+
return runner.sequence([
33+
function () {
34+
t.equal(arguments.length, 0)
35+
return 1
36+
},
37+
function (a) {
38+
t.equal(a, 1)
39+
t.equal(arguments.length, 1)
40+
return Promise.resolve(2)
41+
},
42+
]).then(function (res) {
43+
t.same(res, [2])
44+
})
45+
})
46+

test/input.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,19 @@ test('disabled', function(t) {
8484
], [1, 2])
8585
})
8686

87+
test('single function', function(tt) {
88+
tt.test('sequence', function(t) {
89+
return run(function (next) {
90+
t.equal(arguments.length, 1)
91+
next()
92+
})
93+
})
94+
tt.test('parallel', function(t) {
95+
return parallel(function (next) {
96+
t.equal(arguments.length, 1)
97+
next()
98+
})
99+
})
100+
tt.end()
101+
})
102+

test/output.js

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,35 @@ test('thunkify', function(t) {
140140
})
141141
})
142142

143-
test('disabled', function(t) {
143+
test('disabled', function(tt) {
144144
var scheduler = Runner({ output: false })
145-
return scheduler.sequence([
146-
function (next) {
147-
next(null, 1, 2)
148-
},
149-
])
150-
.then(function (res) {
151-
t.equal(typeof res, 'undefined')
145+
tt.test('sequence', function(t) {
146+
return scheduler.sequence([
147+
function (next) {
148+
next(null, 1)
149+
},
150+
function (a, next) {
151+
t.equal(a, 1)
152+
next(null, 1, 2)
153+
},
154+
])
155+
.then(function (res) {
156+
t.same(res, [])
157+
})
158+
})
159+
tt.test('parallel', function(t) {
160+
return scheduler.parallel([
161+
function (next) {
162+
next(null, 1)
163+
},
164+
function (next) {
165+
next(null, 1, 2)
166+
},
167+
])
168+
.then(function (res) {
169+
t.same(res, [])
170+
})
152171
})
172+
tt.end()
153173
})
154174

0 commit comments

Comments
 (0)