-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathindex.js
45 lines (37 loc) · 1.16 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
// @ts-check
import { register } from "./lib/register.js";
import { addHook } from "./lib/add.js";
import { removeHook } from "./lib/remove.js";
// bind with array of arguments: https://stackoverflow.com/a/21792913
const bind = Function.bind;
const bindable = bind.bind(bind);
function bindApi(hook, state, name) {
const removeHookRef = bindable(removeHook, null).apply(
null,
name ? [state, name] : [state]
);
hook.api = { remove: removeHookRef };
hook.remove = removeHookRef;
["before", "error", "after", "wrap"].forEach((kind) => {
const args = name ? [state, kind, name] : [state, kind];
hook[kind] = hook.api[kind] = bindable(addHook, null).apply(null, args);
});
}
function Singular() {
const singularHookName = Symbol("Singular");
const singularHookState = {
registry: {},
};
const singularHook = register.bind(null, singularHookState, singularHookName);
bindApi(singularHook, singularHookState, singularHookName);
return singularHook;
}
function Collection() {
const state = {
registry: {},
};
const hook = register.bind(null, state);
bindApi(hook, state);
return hook;
}
export default { Singular, Collection };