-
Notifications
You must be signed in to change notification settings - Fork 0
/
chain.js
108 lines (88 loc) · 2.07 KB
/
chain.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
'use strict'
var Zig = require('zig')
var _ = require('lodash')
var Jsonic = require('jsonic')
function Chain () {}
Chain.preload = function () {
this.decorate('start', start)
}
module.exports = Chain
function start (errhandler) {
var sd = this.delegate()
var options = sd.options()
options.zig = options.zig || {}
function make_fn (self, origargs) {
var args = sd.util.parsepattern(self, origargs, 'fn:f?')
var actargs = _.extend(
{},
args.moreobjargs ? args.moreobjargs : {},
args.objargs ? args.objargs : {},
args.strargs ? Jsonic(args.strargs) : {}
)
var fn
if (args.fn) {
fn = function (data, done) {
return args.fn.call(self, data, done)
}
}
else {
fn = function (data, done) {
if (args.strargs) {
var $ = data // eslint-disable-line
_.each(actargs, function (v, k) {
if (_.isString(v) && v.indexOf('$.') === 0) {
actargs[k] = eval(v) // eslint-disable-line
}
})
}
self.act(actargs, done)
return true
}
fn.nm = args.strargs
}
return fn
}
var dzig = Zig({
timeout: options.zig.timeout || options.timeout,
trace: options.zig.trace
})
dzig.start(function () {
var self = this
dzig.end(function () {
if (errhandler) errhandler.apply(self, arguments)
})
})
sd.end = function (cb) {
var self = this
dzig.end(function () {
if (cb) return cb.apply(self, arguments)
if (errhandler) return errhandler.apply(self, arguments)
})
return self
}
sd.wait = function () {
dzig.wait(make_fn(this, arguments))
return this
}
sd.step = function () {
dzig.step(make_fn(this, arguments))
return this
}
sd.run = function () {
dzig.run(make_fn(this, arguments))
return this
}
sd.if = function (cond) {
dzig.if(cond)
return this
}
sd.endif = function () {
dzig.endif()
return this
}
sd.fire = function () {
dzig.step(make_fn(this, arguments))
return this
}
return sd
}