UNPKG

@wordpress/hooks

Version:
79 lines (75 loc) 2.63 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _validateNamespace = _interopRequireDefault(require("./validateNamespace")); var _validateHookName = _interopRequireDefault(require("./validateHookName")); /** * Internal dependencies */ /** * Removes the specified callback (or all callbacks) from the hook with a given hookName * and namespace. */ /** * Returns a function which, when invoked, will remove a specified hook or all * hooks by the given name. * * @param hooks Hooks instance. * @param storeKey * @param [removeAll=false] Whether to remove all callbacks for a hookName, * without regard to namespace. Used to create * `removeAll*` functions. * * @return Function that removes hooks. */ function createRemoveHook(hooks, storeKey, removeAll = false) { return function removeHook(hookName, namespace) { const hooksStore = hooks[storeKey]; if (!(0, _validateHookName.default)(hookName)) { return; } if (!removeAll && !(0, _validateNamespace.default)(namespace)) { return; } // Bail if no hooks exist by this name. if (!hooksStore[hookName]) { return 0; } let handlersRemoved = 0; if (removeAll) { handlersRemoved = hooksStore[hookName].handlers.length; hooksStore[hookName] = { runs: hooksStore[hookName].runs, handlers: [] }; } else { // Try to find the specified callback to remove. const handlers = hooksStore[hookName].handlers; for (let i = handlers.length - 1; i >= 0; i--) { if (handlers[i].namespace === namespace) { handlers.splice(i, 1); handlersRemoved++; // This callback may also be part of a hook that is // currently executing. If the callback we're removing // comes after the current callback, there's no problem; // otherwise we need to decrease the execution index of any // other runs by 1 to account for the removed element. hooksStore.__current.forEach(hookInfo => { if (hookInfo.name === hookName && hookInfo.currentIndex >= i) { hookInfo.currentIndex--; } }); } } } if (hookName !== 'hookRemoved') { hooks.doAction('hookRemoved', hookName, namespace); } return handlersRemoved; }; } var _default = exports.default = createRemoveHook; //# sourceMappingURL=createRemoveHook.js.map