change-string-case/node_modules/before-after-hook/index.js

62 lines
1.7 KiB
JavaScript
Raw Permalink Normal View History

2023-10-26 04:27:07 +08:00
var register = require("./lib/register");
var addHook = require("./lib/add");
var removeHook = require("./lib/remove");
2022-11-04 00:55:07 +08:00
// bind with array of arguments: https://stackoverflow.com/a/21792913
2023-10-26 04:27:07 +08:00
var bind = Function.bind;
var bindable = bind.bind(bind);
function bindApi(hook, state, name) {
var removeHookRef = bindable(removeHook, null).apply(
null,
name ? [state, name] : [state]
);
hook.api = { remove: removeHookRef };
hook.remove = removeHookRef;
["before", "error", "after", "wrap"].forEach(function (kind) {
var args = name ? [state, kind, name] : [state, kind];
hook[kind] = hook.api[kind] = bindable(addHook, null).apply(null, args);
});
2022-11-04 00:55:07 +08:00
}
2023-10-26 04:27:07 +08:00
function HookSingular() {
var singularHookName = "h";
2022-11-04 00:55:07 +08:00
var singularHookState = {
2023-10-26 04:27:07 +08:00
registry: {},
};
var singularHook = register.bind(null, singularHookState, singularHookName);
bindApi(singularHook, singularHookState, singularHookName);
return singularHook;
2022-11-04 00:55:07 +08:00
}
2023-10-26 04:27:07 +08:00
function HookCollection() {
2022-11-04 00:55:07 +08:00
var state = {
2023-10-26 04:27:07 +08:00
registry: {},
};
2022-11-04 00:55:07 +08:00
2023-10-26 04:27:07 +08:00
var hook = register.bind(null, state);
bindApi(hook, state);
2022-11-04 00:55:07 +08:00
2023-10-26 04:27:07 +08:00
return hook;
2022-11-04 00:55:07 +08:00
}
2023-10-26 04:27:07 +08:00
var collectionHookDeprecationMessageDisplayed = false;
function Hook() {
2022-11-04 00:55:07 +08:00
if (!collectionHookDeprecationMessageDisplayed) {
2023-10-26 04:27:07 +08:00
console.warn(
'[before-after-hook]: "Hook()" repurposing warning, use "Hook.Collection()". Read more: https://git.io/upgrade-before-after-hook-to-1.4'
);
collectionHookDeprecationMessageDisplayed = true;
2022-11-04 00:55:07 +08:00
}
2023-10-26 04:27:07 +08:00
return HookCollection();
2022-11-04 00:55:07 +08:00
}
2023-10-26 04:27:07 +08:00
Hook.Singular = HookSingular.bind();
Hook.Collection = HookCollection.bind();
2022-11-04 00:55:07 +08:00
2023-10-26 04:27:07 +08:00
module.exports = Hook;
2022-11-04 00:55:07 +08:00
// expose constructors as a named property for TypeScript
2023-10-26 04:27:07 +08:00
module.exports.Hook = Hook;
module.exports.Singular = Hook.Singular;
module.exports.Collection = Hook.Collection;