-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathasync_optimizer.js
112 lines (95 loc) · 4.49 KB
/
async_optimizer.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
// This is the synchronous version of the optimizer; which the Async one should be based on.
import { isDeterministic } from './compiler.js'
import { map } from './async_iterators.js'
import { isSync, Sync } from './constants.js'
import declareSync from './utilities/declareSync.js'
import { coerceArray } from './utilities/coerceArray.js'
/**
* Turns an expression like { '+': [1, 2] } into a function that can be called with data.
* @param {*} logic
* @param {*} engine
* @param {string} methodName
* @param {any[]} above
* @returns A method that can be called to execute the logic.
*/
function getMethod (logic, engine, methodName, above) {
const method = engine.methods[methodName]
const called = method.asyncMethod ? method.asyncMethod : method.method ? method.method : method
if (method.lazy) {
if (typeof method[Sync] === 'function' && method[Sync](logic, { engine })) {
const called = method.method ? method.method : method
return declareSync((data, abv) => called(logic[methodName], data, abv || above, engine.fallback), true)
}
const args = logic[methodName]
return (data, abv) => called(args, data, abv || above, engine)
}
let args = logic[methodName]
if ((!args || typeof args !== 'object') && !method.optimizeUnary) args = [args]
if (Array.isArray(args)) {
const optimizedArgs = args.map(l => optimize(l, engine, above))
if (isSync(optimizedArgs) && (method.method || method[Sync])) {
const called = method.method ? method.method : method
return declareSync((data, abv) => {
const evaluatedArgs = optimizedArgs.map(l => typeof l === 'function' ? l(data, abv) : l)
return called(evaluatedArgs, data, abv || above, engine.fallback)
}, true)
}
return async (data, abv) => {
const evaluatedArgs = await map(optimizedArgs, l => typeof l === 'function' ? l(data, abv) : l)
return called(evaluatedArgs, data, abv || above, engine)
}
} else {
const optimizedArgs = optimize(args, engine, above)
if (isSync(optimizedArgs) && (method.method || method[Sync])) {
const called = method.method ? method.method : method
if (method.optimizeUnary) return declareSync((data, abv) => called(typeof optimizedArgs === 'function' ? optimizedArgs(data, abv) : optimizedArgs, data, abv || above, engine.fallback), true)
return declareSync((data, abv) => called(coerceArray(typeof optimizedArgs === 'function' ? optimizedArgs(data, abv) : optimizedArgs), data, abv || above, engine), true)
}
if (method.optimizeUnary) return async (data, abv) => called(typeof optimizedArgs === 'function' ? await optimizedArgs(data, abv) : optimizedArgs, data, abv || above, engine)
return async (data, abv) => called(coerceArray(typeof optimizedArgs === 'function' ? await optimizedArgs(data, abv) : optimizedArgs), data, abv || above, engine)
}
}
/**
* Processes the logic for the engine once so that it doesn't need to be traversed again.
* @param {*} logic
* @param {*} engine
* @param {any[]} above
* @returns A function that optimizes the logic for the engine in advance.
*/
export function optimize (logic, engine, above = []) {
engine.fallback.allowFunctions = engine.allowFunctions
if (Array.isArray(logic)) {
const arr = logic.map(l => optimize(l, engine, above))
if (isSync(arr)) return declareSync((data, abv) => arr.map(l => typeof l === 'function' ? l(data, abv) : l), true)
return async (data, abv) => map(arr, l => typeof l === 'function' ? l(data, abv) : l)
};
if (logic && typeof logic === 'object') {
const keys = Object.keys(logic)
const methodName = keys[0]
const isData = engine.isData(logic, methodName)
if (isData) return () => logic
// If we have a deterministic function, we can just return the result of the evaluation,
// basically inlining the operation.
const deterministic = !engine.disableInline && isDeterministic(logic, engine, { engine })
if (methodName in engine.methods) {
const result = getMethod(logic, engine, methodName, above)
if (deterministic) {
let computed
if (isSync(result)) {
return declareSync(() => {
if (!computed) computed = result()
return computed
}, true)
}
// For async, it's a little less straightforward since it could be a promise,
// so we'll make it a closure.
return async () => {
if (!computed) computed = await result()
return computed
}
}
return result
}
}
return logic
}