-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcreate.js
38 lines (30 loc) · 913 Bytes
/
create.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
import builtinControls from './controls/builtin'
import is from './utils/is'
const create = (userControls = []) => {
const controls = [...userControls, ...builtinControls]
const runtime = (input, success = () => {}, error = () => {}) => {
const iterate = gen => {
const yieldValue = isError => ret => {
try {
const { value, done } = isError ? gen.throw(ret) : gen.next(ret)
if (done) return success(value)
next(value)
} catch (e) {
return error(e)
}
}
const next = ret => {
controls.some(control => control(ret, next, runtime, yieldValue(false), yieldValue(true)))
}
yieldValue(false)()
}
const iterator = is.iterator(input)
? input
: function* () {
return yield input
}()
iterate(iterator, success, error)
}
return runtime
}
export default create;