/
step.js
161 lines (143 loc) · 4.84 KB
/
step.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
var Promise = require('./promise')
, clone = require('./utils').clone;
var Step = module.exports = function Step (name, _module) {
this.name = name;
// defineProperty; otherwise,
// clone will overflow when we clone a module
Object.defineProperty(this, 'module', {
value: _module
});
};
Step.prototype = {
/**
* @returns {Promise}
*/
exec: function (seq) {
var accepts = this.accepts
, promises = this.promises
, block = this.block
, _module = this.module
, errorCallback = this.errback || _module._moduleErrback // Configured errback
, self = this;
if (this.debug)
console.log('starting step - ' + this.name);
var args = this._unwrapArgs(seq);
// There is a hidden last argument to every step function that
// is all the data promised by prior steps up to the step's point
// in time. We cannot anticipate everything a developer may want via
// `accepts(...)`. Therefore, just in case, we give the developer
// access to all data promised by prior steps via the last argument -- `seq.values`
args.push(seq.values);
try {
// Apply the step logic
// Add _super access
_module._super = function () {
var step = this.__proto__._steps[self.name];
if (!step) return;
var superArgs = arguments.length ? arguments : args;
step.block.apply(this, superArgs);
};
ret = block.apply(_module, args);
delete _module._super;
} catch (breakTo) {
// Catch any sync breakTo's if any
if (breakTo.isSeq) {
console.log("breaking out to " + breakTo.name);
breakTo.start.apply(breakTo, breakTo.initialArgs);
// TODO Garbage collect the promise chain
return;
} else {
// Else, we have a regular exception
// TODO Scope this fn
errorCallback(breakTo, seq.values);
}
}
if (promises && promises.length &&
'undefined' === typeof ret) {
// TODO Scope this fn
errorCallback(
new Error('Step ' + this.name + ' of `' + _module.name +
'` is promising: ' + promises.join(', ') +
' ; however, the step returns nothing. ' +
'Fix the step by returning the expected values OR ' +
'by returning a Promise that promises said values.')
);
}
// Convert return value into a Promise
// if it's not yet a Promise
ret = (ret instanceof Promise)
? ret
: Array.isArray(ret)
? promises.length === 1
? this.module.Promise([ret])
: this.module.Promise(ret)
: this.module.Promise([ret]);
ret.callback( function () {
if (seq.debug)
console.log('...finished step');
});
var convertErr = _module._convertErr;
if (convertErr) {
var oldErrback = ret.errback;
ret.errback = function (fn, scope) {
var oldFn = fn;
fn = function (err) {
if (err.constructor === Object) {
err = convertErr(err);
} else if ('string' === typeof err) {
err = new Error(err);
}
return oldFn.call(this, err);
};
return oldErrback.call(this, fn, scope);
};
}
// TODO Scope this fn -- i.e., errorCallback?
ret.errback(errorCallback);
ret.callback( function () {
// Store the returned values
// in the sequence's state via seq.values
var returned = arguments
, vals = seq.values;
if (promises !== null) promises.forEach( function (valName, i) {
vals[valName] = returned[i];
});
});
ret.timeback( function () {
ret.fail(new Error('Step ' + self.name + ' of `' + _module.name + '` module timed out.'));
});
var timeoutMillis = this.timeout ||
_module.moduleTimeout();
ret.timeout(timeoutMillis);
return ret;
}
/**
* Unwraps values (from the sequence) based on
* the step's this.accepts spec.
*/
, _unwrapArgs: function (seq) {
return this.accepts.reduce( function (args, accept) {
args.push(seq.values[accept]);
return args;
}, []);
}
, clone: function (name, _module) {
var step = new Step(name, _module);
step.accepts = clone(this.accepts);
step.promises = clone(this.promises);
step.description = this.description;
step.timeout = this.timeout;
step.errback = this.errback;
return step;
}
};
Object.defineProperty(Step.prototype, 'block', {
get: function () {
return this._block || (this._block = this.module[this.name]());
}
});
Object.defineProperty(Step.prototype, 'debug', {
get: function () {
return this.module.everyauth.debug;
}
});