/
index.ts
52 lines (46 loc) · 1.65 KB
/
index.ts
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
export function createBuffer<T>(rootName = 'root') {
const calls = new Map<string, any[]>();
const dynHandler = {
get: (target, prop) => {
if (!target[prop]) {
const mock = Object.assign(
(...args) => calls.set(mock.__getKey(), args),
{
__getKey: () => `${target.__getKey()}.${prop}`
}
);
target[prop] = new Proxy(mock, dynHandler);
}
return target[prop];
}
};
return {
calls,
root: new Proxy({__getKey: () => rootName}, dynHandler) as T,
invokeFor: async (obj: Object, options : InvocationOptions = {}) => {
for (const k of calls.keys()) {
const lastProperty = k.lastIndexOf('.');
const ctxStr = k.substr(0, lastProperty);
const ctxGetter = new Function(rootName, `return ${ctxStr}`);
const ctx = ctxGetter(obj);
const fnName = k.substr(lastProperty + 1);
try {
const res = ctx[fnName].apply(ctx, calls.get(k));
if (options.sync && res instanceof Promise) await res;
}
catch (e) {
if (!options.errorHandler) {
throw e;
}
else {
options.errorHandler(k, calls.get(k), e);
}
}
}
}
};
}
export interface InvocationOptions {
sync? : boolean;
errorHandler? : (callName : string, args : any[], e : any) => void;
}