-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
128 lines (107 loc) · 3.29 KB
/
index.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
/*
Run a generator with Episode 7
```javascript
let result = Episode7.run(yourGeneratorFunction);
```
Returns a Promise resolving when the generator is done.
*/
function run(generator, ...args) {
try {
let g = generator(...args);
g.episode7Name = generator.name;
return runUntilResolved(g);
} catch(error) {
return Promise.reject(error);
}
}
/*
Perform a side-effect with Episode 7
```javascript
yield Episode7.call(functionToCall, arg1, arg2)
```
Passes through to JavaScript `Function.prototype.call`.
`call` also supports also invoking object methods, you can provide a `this`
context to the invoked functions using the following form. (Syntax copied
from [redux-saga](http://yelouafi.github.io/redux-saga/docs/basics/DeclarativeEffects.html))
```javascript
yield Episode7.call([obj, obj.method], arg1, arg2)
```
The return value is not passed back to the yield statement. Episode 7
will call `functionToCall` when running and return a Promise resolving
with its return value.
This function's return value is useful in testing as an object
representing the side-effect to perform.
*/
function call(fn, ...args) {
return { fn, args };
}
/*
Internal function to run the generator, waiting on
each side-effect's returned Promise.
Each side-effect function is executed via standard
JavaScript `Function.prototype.call`.
Returns a Promise of the return value from
the generator.
*/
function runUntilResolved(genInstance, nextArg) {
try {
let nextResult = genInstance.next(nextArg);
if (nextResult.done) {
return Promise.resolve(nextResult.value);
} else {
let v = nextResult.value;
let fn = v.fn;
let thisArg;
if (fn instanceof Array) {
thisArg = fn[0];
fn = fn[1];
}
let args = v.args;
if (fn == null || args == null) {
throw new Error('`Episode7.run` requires using `Episode7.call` with every `yield`. '+
'Check all of the `yield` expressions in [Function: '+genInstance.episode7Name+']');
}
// Iterate through the generator, handling
// promises & nested generators.
// Side-effect function is called without `this`.
let sideEffect;
if (isGeneratorFunction(fn)) {
sideEffect = Promise.resolve()
.then(function() {
// Wrapped in `then` to promisify return value
return run(fn, ...args)
})
} else {
sideEffect = Promise.resolve()
.then(function() {
// Wrapped in `then` to promisify return value
return fn.call(thisArg, ...args)
})
}
return sideEffect
.then(function(result) {
return runUntilResolved(genInstance, result);
})
}
} catch(error) {
return Promise.reject(error);
}
}
/*
Internal helpers.
Source: https://github.com/tj/co/blob/717b043371ba057cb7a4a2a4e47120d598116ed7/index.js
*/
function isGenerator(obj) {
return 'function' == typeof obj.next && 'function' == typeof obj.throw;
}
function isGeneratorFunction(obj) {
if (obj == null) return false;
var constructor = obj.constructor;
if (!constructor) return false;
if ('GeneratorFunction' === constructor.name || 'GeneratorFunction' === constructor.displayName) return true;
return isGenerator(constructor.prototype);
}
module.exports = {
run,
call
}