-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
391 lines (360 loc) · 10.3 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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
create.SYNC = 1;
create.ASYNC = 2;
create.QUEUE = 4;
var packageName = "fun-hooks";
var defaults = Object.freeze({
ready: 0
});
var hookableMap = new WeakMap();
function rest(args, skip) {
return Array.prototype.slice.call(args, skip);
}
function runAll(queue) {
var queued;
// eslint-disable-next-line no-cond-assign
while ((queued = queue.shift())) {
queued();
}
}
function create(config) {
var hooks = {};
var postReady = [];
config = Object.assign({}, defaults, config);
function dispatch(arg1, arg2) {
if (typeof arg1 === "function") {
return hookFn.call(null, "sync", arg1, arg2);
} else if (typeof arg1 === "string" && typeof arg2 === "function") {
return hookFn.apply(null, arguments);
} else if (typeof arg1 === "object") {
return hookObj.apply(null, arguments);
}
}
var ready;
if (config.ready) {
dispatch.ready = function() {
ready = true;
runAll(postReady);
};
} else {
ready = true;
}
function hookObj(obj, props, objName) {
var walk = true;
if (typeof props === "undefined") {
props = Object.getOwnPropertyNames(obj).filter(prop => !prop.match(/^_/));
walk = false;
}
var objHooks = {};
var doNotHook = ["constructor"];
do {
props.forEach(function(prop) {
var parts = prop.match(/(?:(sync|async):)?(.+)/);
var type = parts[1] || "sync";
var name = parts[2];
if (
!objHooks[name] &&
typeof obj[name] === "function" &&
!(doNotHook.indexOf(name) !== -1)
) {
var fn = obj[name];
objHooks[name] = obj[name] = hookFn(
type,
fn,
objName ? [objName, name] : undefined
);
}
});
obj = Object.getPrototypeOf(obj);
} while (walk && obj);
return objHooks;
}
/**
* Navigates a string path to return a hookable function. If not found, creates a placeholder for hooks.
* @param {(Array<string> | string)} path
*/
function get(path) {
var parts = Array.isArray(path) ? path : path.split(".");
return parts.reduce(function(memo, part, i) {
var item = memo[part];
var installed = false;
if (item) {
return item;
} else if (i === parts.length - 1) {
if (!ready) {
postReady.push(function() {
if (!installed) {
// eslint-disable-next-line no-console
console.warn(
packageName +
": referenced '" +
path +
"' but it was never created"
);
}
});
}
return (memo[part] = newHookable(function(fn) {
memo[part] = fn;
installed = true;
}));
}
return (memo[part] = {});
}, hooks);
}
function newHookable(onInstall) {
var before = [];
var after = [];
var generateTrap = function() {};
var api = {
before: function(hook, priority) {
return add.call(this, before, "before", hook, priority);
},
after: function(hook, priority) {
return add.call(this, after, "after", hook, priority);
},
getHooks: function(match) {
var hooks = before.concat(after);
if (typeof match === "object") {
hooks = hooks.filter(function(entry) {
return Object.keys(match).every(function(prop) {
return entry[prop] === match[prop];
});
});
}
try {
Object.assign(hooks, {
remove: function() {
hooks.forEach(function(entry) {
entry.remove();
});
return this;
}
});
} catch (e) {
console.error(
"error adding `remove` to array, did you modify Array.prototype?"
);
}
return hooks;
},
removeAll: function() {
return this.getHooks().remove();
}
};
var meta = {
install: function(type, fn, generate) {
this.type = type;
generateTrap = generate;
generate(before, after);
onInstall && onInstall(fn);
}
};
// store meta data related to hookable. use `api.after` since `api` reference is not available on our proxy.
hookableMap.set(api.after, meta);
return api;
function add(store, type, hook, priority) {
var entry = {
hook: hook,
type: type,
priority: priority || 10,
remove: function() {
var index = store.indexOf(entry);
if (index !== -1) {
store.splice(index, 1);
generateTrap(before, after);
}
}
};
store.push(entry);
store.sort(function(a, b) {
return b.priority - a.priority;
});
generateTrap(before, after);
return this;
}
}
function hookFn(type, fn, name) {
// check if function has already been wrapped
var meta = fn.after && hookableMap.get(fn.after);
if (meta) {
if (meta.type !== type) {
throw packageName + ": recreated hookable with different type";
} else {
return fn;
}
}
var hookable = name ? get(name) : newHookable();
var trap;
var handlers = {
get: function(target, prop) {
return hookable[prop] || Reflect.get.apply(Reflect, arguments);
}
};
if (!ready) {
postReady.push(setTrap);
}
var hookedFn = new Proxy(fn, handlers);
hookableMap.get(hookedFn.after).install(type, hookedFn, generateTrap);
return hookedFn;
/* @ifdef EVAL */
function generateTrap(before, after) {
if (before.length || after.length) {
var code;
if (type === "sync") {
var beforeCode =
"r=t.apply(h," + (before.length ? "arguments" : "g") + ")";
var afterCode;
if (after.length) {
afterCode = chainHooks(after, "a", "n(function extract(s){r=s},e)");
}
if (before.length) {
beforeCode = chainHooks(
before,
"b",
"n(function extract(){" + beforeCode + ";" + afterCode + "},e)"
);
afterCode = "";
}
code = [
"var r,e={bail:function(a){r=a}}",
beforeCode,
afterCode,
"return r"
].join(";");
} else if (type === "async") {
code =
"t.apply(h," +
(before.length
? "Array.prototype.slice.call(arguments)" // if we're wrapped in partial, extract arguments
: "g") + // otherwise, we can just use passed in arguments
".concat(" +
chainHooks(after, "a", "z?n(z,e):[]") +
"))";
if (before.length) {
code = "n(function partial(){" + code + "},e)";
}
code = [
"var z",
'typeof g[g.length-1]==="function"&&(z=i.call(g.pop(),null))',
"var e={bail:z}",
chainHooks(before, "b", code)
].join(";");
}
trap = new Function("i,b,a,n,t,h,g", code).bind(
null,
Function.prototype.bind,
before,
after,
Object.assign
);
} else {
trap = undefined;
}
setTrap();
function chainHooks(hooks, name, code) {
for (var i = hooks.length; i-- > 0; ) {
if (i === 0 && !(type === "async" && name === "a")) {
code =
name +
"[" +
i +
"].hook.apply(h,[" +
code +
(name === "b" ? "].concat(g))" : ",r])");
} else {
code = "i.call(" + name + "[" + i + "].hook, h," + code + ")";
if (!(type === "async" && name === "a" && i === 0)) {
code = "n(" + code + ",e)";
}
}
}
return code;
}
}
/* @endif */
/* @ifndef EVAL */
// eslint-disable-next-line no-redeclare
function generateTrap(before, after) {
var order = [];
var targetIndex;
if (before.length || after.length) {
before.forEach(addToOrder);
// placeholder for target function wrapper
targetIndex = order.push(undefined) - 1;
after.forEach(addToOrder);
trap = function(target, thisArg, args) {
var curr = 0;
var result;
var callback =
type === "async" &&
typeof args[args.length - 1] === "function" &&
args.pop();
function bail(value) {
if (type === "sync") {
result = value;
} else if (callback) {
callback.apply(null, arguments);
}
}
function next(value) {
if (order[curr]) {
var args = rest(arguments);
next.bail = bail;
args.unshift(next);
return order[curr++].apply(thisArg, args);
}
if (type === "sync") {
result = value;
} else if (callback) {
callback.apply(null, arguments);
}
}
order[targetIndex] = function() {
var args = rest(arguments, 1);
if (type === "async" && callback) {
delete next.bail;
args.push(next);
}
var result = target.apply(thisArg, args);
if (type === "sync") {
next(result);
}
};
next.apply(null, args);
return result;
};
} else {
trap = undefined;
}
setTrap();
function addToOrder(entry) {
order.push(entry.hook);
}
}
/* @endif */
function setTrap() {
if (
ready ||
(type === "sync" && !(config.ready & create.SYNC)) ||
(type === "async" && !(config.ready & create.ASYNC))
) {
handlers.apply = trap;
} else if (type === "sync" || !(config.ready & create.QUEUE)) {
handlers.apply = function() {
throw packageName + ": hooked function not ready";
};
} else {
handlers.apply = function() {
var args = arguments;
postReady.push(function() {
hookedFn.apply(args[1], args[2]);
});
};
}
}
}
dispatch.get = get;
return dispatch;
}
/* global module */
module.exports = create;