Socket
Socket
Sign inDemoInstall

@wordpress/hooks

Package Overview
Dependencies
Maintainers
16
Versions
162
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@wordpress/hooks - npm Package Compare versions

Comparing version 2.10.0 to 2.11.0

build-types/createAddHook.d.ts

40

build-module/createAddHook.js

@@ -6,22 +6,26 @@ /**

import validateHookName from './validateHookName.js';
import { doAction } from './';
/**
* @callback AddHook
*
* Adds the hook to the appropriate hooks container.
*
* @param {string} hookName Name of hook to add
* @param {string} namespace The unique namespace identifying the callback in the form `vendor/plugin/function`.
* @param {import('.').Callback} callback Function to call when the hook is run
* @param {number} [priority=10] Priority of this hook
*/
/**
* Returns a function which, when invoked, will add a hook.
*
* @param {Object} hooks Stored hooks, keyed by hook name.
* @param {import('.').Hooks} hooks Hooks instance.
* @param {import('.').StoreKey} storeKey
*
* @return {Function} Function that adds a new hook.
* @return {AddHook} Function that adds a new hook.
*/
function createAddHook(hooks) {
/**
* Adds the hook to the appropriate hooks container.
*
* @param {string} hookName Name of hook to add
* @param {string} namespace The unique namespace identifying the callback in the form `vendor/plugin/function`.
* @param {Function} callback Function to call when the hook is run
* @param {?number} priority Priority of this hook (default=10)
*/
function createAddHook(hooks, storeKey) {
return function addHook(hookName, namespace, callback) {
var priority = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 10;
var hooksStore = hooks[storeKey];

@@ -55,5 +59,7 @@ if (!validateHookName(hookName)) {

if (hooks[hookName]) {
if (hooksStore[hookName]) {
// Find the correct insert index of the new hook.
var handlers = hooks[hookName].handlers;
var handlers = hooksStore[hookName].handlers;
/** @type {number} */
var i;

@@ -79,3 +85,3 @@

(hooks.__current || []).forEach(function (hookInfo) {
hooksStore.__current.forEach(function (hookInfo) {
if (hookInfo.name === hookName && hookInfo.currentIndex >= i) {

@@ -87,3 +93,3 @@ hookInfo.currentIndex++;

// This is the first hook of its type.
hooks[hookName] = {
hooksStore[hookName] = {
handlers: [handler],

@@ -95,3 +101,3 @@ runs: 0

if (hookName !== 'hookAdded') {
doAction('hookAdded', hookName, namespace, callback, priority);
hooks.doAction('hookAdded', hookName, namespace, callback, priority);
}

@@ -98,0 +104,0 @@ };

@@ -6,20 +6,13 @@ /**

*
* @param {Object} hooks Stored hooks, keyed by hook name.
* @param {import('.').Hooks} hooks Hooks instance.
* @param {import('.').StoreKey} storeKey
*
* @return {Function} Function that returns the current hook.
* @return {() => string | null} Function that returns the current hook name or null.
*/
function createCurrentHook(hooks) {
/**
* Returns the name of the currently running hook, or `null` if no hook of
* the given type is currently running.
*
* @return {?string} The name of the currently running hook, or
* `null` if no hook is currently running.
*/
function createCurrentHook(hooks, storeKey) {
return function currentHook() {
if (!hooks.__current || !hooks.__current.length) {
return null;
}
var _hooksStore$__current, _hooksStore$__current2;
return hooks.__current[hooks.__current.length - 1].name;
var hooksStore = hooks[storeKey];
return (_hooksStore$__current = (_hooksStore$__current2 = hooksStore.__current[hooksStore.__current.length - 1]) === null || _hooksStore$__current2 === void 0 ? void 0 : _hooksStore$__current2.name) !== null && _hooksStore$__current !== void 0 ? _hooksStore$__current : null;
};

@@ -26,0 +19,0 @@ }

@@ -6,19 +6,25 @@ /**

/**
* @callback DidHook
*
* Returns the number of times an action has been fired.
*
* @param {string} hookName The hook name to check.
*
* @return {number | undefined} The number of times the hook has run.
*/
/**
* Returns a function which, when invoked, will return the number of times a
* hook has been called.
*
* @param {Object} hooks Stored hooks, keyed by hook name.
* @param {import('.').Hooks} hooks Hooks instance.
* @param {import('.').StoreKey} storeKey
*
* @return {Function} Function that returns a hook's call count.
* @return {DidHook} Function that returns a hook's call count.
*/
function createDidHook(hooks) {
/**
* Returns the number of times an action has been fired.
*
* @param {string} hookName The hook name to check.
*
* @return {number} The number of times the hook has run.
*/
function createDidHook(hooks, storeKey) {
return function didHook(hookName) {
var hooksStore = hooks[storeKey];
if (!validateHookName(hookName)) {

@@ -28,3 +34,3 @@ return;

return hooks[hookName] && hooks[hookName].runs ? hooks[hookName].runs : 0;
return hooksStore[hookName] && hooksStore[hookName].runs ? hooksStore[hookName].runs : 0;
};

@@ -31,0 +37,0 @@ }

/**
* @callback DoingHook
* Returns whether a hook is currently being executed.
*
* @param {string} [hookName] The name of the hook to check for. If
* omitted, will check for any hook being executed.
*
* @return {boolean} Whether the hook is being executed.
*/
/**
* Returns a function which, when invoked, will return whether a hook is
* currently being executed.
*
* @param {Object} hooks Stored hooks, keyed by hook name.
* @param {import('.').Hooks} hooks Hooks instance.
* @param {import('.').StoreKey} storeKey
*
* @return {Function} Function that returns whether a hook is currently
* being executed.
* @return {DoingHook} Function that returns whether a hook is currently
* being executed.
*/
function createDoingHook(hooks) {
/**
* Returns whether a hook is currently being executed.
*
* @param {?string} hookName The name of the hook to check for. If
* omitted, will check for any hook being executed.
*
* @return {boolean} Whether the hook is being executed.
*/
function createDoingHook(hooks, storeKey) {
return function doingHook(hookName) {
// If the hookName was not passed, check for any current hook.
var hooksStore = hooks[storeKey]; // If the hookName was not passed, check for any current hook.
if ('undefined' === typeof hookName) {
return 'undefined' !== typeof hooks.__current[0];
return 'undefined' !== typeof hooksStore.__current[0];
} // Return the __current hook.
return hooks.__current[0] ? hookName === hooks.__current[0].name : false;
return hooksStore.__current[0] ? hookName === hooksStore.__current[0].name : false;
};

@@ -28,0 +32,0 @@ }

/**
* @callback HasHook
*
* Returns whether any handlers are attached for the given hookName and optional namespace.
*
* @param {string} hookName The name of the hook to check for.
* @param {string} [namespace] Optional. The unique namespace identifying the callback
* in the form `vendor/plugin/function`.
*
* @return {boolean} Whether there are handlers that are attached to the given hook.
*/
/**
* Returns a function which, when invoked, will return whether any handlers are
* attached to a particular hook.
*
* @param {Object} hooks Stored hooks, keyed by hook name.
* @param {import('.').Hooks} hooks Hooks instance.
* @param {import('.').StoreKey} storeKey
*
* @return {Function} Function that returns whether any handlers are
* attached to a particular hook and optional namespace.
* @return {HasHook} Function that returns whether any handlers are
* attached to a particular hook and optional namespace.
*/
function createHasHook(hooks) {
/**
* Returns whether any handlers are attached for the given hookName and optional namespace.
*
* @param {string} hookName The name of the hook to check for.
* @param {?string} namespace Optional. The unique namespace identifying the callback
* in the form `vendor/plugin/function`.
*
* @return {boolean} Whether there are handlers that are attached to the given hook.
*/
function createHasHook(hooks, storeKey) {
return function hasHook(hookName, namespace) {
// Use the namespace if provided.
var hooksStore = hooks[storeKey]; // Use the namespace if provided.
if ('undefined' !== typeof namespace) {
return hookName in hooks && hooks[hookName].handlers.some(function (hook) {
return hookName in hooksStore && hooksStore[hookName].handlers.some(function (hook) {
return hook.namespace === namespace;

@@ -28,3 +33,3 @@ });

return hookName in hooks;
return hookName in hooksStore;
};

@@ -31,0 +36,0 @@ }

@@ -0,1 +1,3 @@

import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
/**

@@ -12,32 +14,46 @@ * Internal dependencies

/**
* Internal class for constructing hooks. Use `createHooks()` function
*
* Note, it is necessary to expose this class to make its type public.
*
* @private
*/
export var _Hooks = function _Hooks() {
_classCallCheck(this, _Hooks);
/** @type {import('.').Store} actions */
this.actions = Object.create(null);
this.actions.__current = [];
/** @type {import('.').Store} filters */
this.filters = Object.create(null);
this.filters.__current = [];
this.addAction = createAddHook(this, 'actions');
this.addFilter = createAddHook(this, 'filters');
this.removeAction = createRemoveHook(this, 'actions');
this.removeFilter = createRemoveHook(this, 'filters');
this.hasAction = createHasHook(this, 'actions');
this.hasFilter = createHasHook(this, 'filters');
this.removeAllActions = createRemoveHook(this, 'actions', true);
this.removeAllFilters = createRemoveHook(this, 'filters', true);
this.doAction = createRunHook(this, 'actions');
this.applyFilters = createRunHook(this, 'filters', true);
this.currentAction = createCurrentHook(this, 'actions');
this.currentFilter = createCurrentHook(this, 'filters');
this.doingAction = createDoingHook(this, 'actions');
this.doingFilter = createDoingHook(this, 'filters');
this.didAction = createDidHook(this, 'actions');
this.didFilter = createDidHook(this, 'filters');
};
/** @typedef {_Hooks} Hooks */
/**
* Returns an instance of the hooks object.
*
* @return {Object} Object that contains all hooks.
* @return {Hooks} A Hooks instance.
*/
function createHooks() {
var actions = Object.create(null);
var filters = Object.create(null);
actions.__current = [];
filters.__current = [];
return {
addAction: createAddHook(actions),
addFilter: createAddHook(filters),
removeAction: createRemoveHook(actions),
removeFilter: createRemoveHook(filters),
hasAction: createHasHook(actions),
hasFilter: createHasHook(filters),
removeAllActions: createRemoveHook(actions, true),
removeAllFilters: createRemoveHook(filters, true),
doAction: createRunHook(actions),
applyFilters: createRunHook(filters, true),
currentAction: createCurrentHook(actions),
currentFilter: createCurrentHook(filters),
doingAction: createDoingHook(actions),
doingFilter: createDoingHook(filters),
didAction: createDidHook(actions),
didFilter: createDidHook(filters),
actions: actions,
filters: filters
};
return new _Hooks();
}

@@ -44,0 +60,0 @@

@@ -6,24 +6,32 @@ /**

import validateHookName from './validateHookName.js';
import { doAction } from './';
/**
* @callback RemoveHook
* Removes the specified callback (or all callbacks) from the hook with a given hookName
* and namespace.
*
* @param {string} hookName The name of the hook to modify.
* @param {string} namespace The unique namespace identifying the callback in the
* form `vendor/plugin/function`.
*
* @return {number | undefined} The number of callbacks removed.
*/
/**
* Returns a function which, when invoked, will remove a specified hook or all
* hooks by the given name.
*
* @param {Object} hooks Stored hooks, keyed by hook name.
* @param {boolean} removeAll Whether to remove all callbacks for a hookName, without regard to namespace. Used to create `removeAll*` functions.
* @param {import('.').Hooks} hooks Hooks instance.
* @param {import('.').StoreKey} storeKey
* @param {boolean} [removeAll=false] Whether to remove all callbacks for a hookName,
* without regard to namespace. Used to create
* `removeAll*` functions.
*
* @return {Function} Function that removes hooks.
* @return {RemoveHook} Function that removes hooks.
*/
function createRemoveHook(hooks, removeAll) {
/**
* Removes the specified callback (or all callbacks) from the hook with a
* given hookName and namespace.
*
* @param {string} hookName The name of the hook to modify.
* @param {string} namespace The unique namespace identifying the callback in the form `vendor/plugin/function`.
*
* @return {number} The number of callbacks removed.
*/
function createRemoveHook(hooks, storeKey) {
var removeAll = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
return function removeHook(hookName, namespace) {
var hooksStore = hooks[storeKey];
if (!validateHookName(hookName)) {

@@ -38,3 +46,3 @@ return;

if (!hooks[hookName]) {
if (!hooksStore[hookName]) {
return 0;

@@ -46,5 +54,5 @@ }

if (removeAll) {
handlersRemoved = hooks[hookName].handlers.length;
hooks[hookName] = {
runs: hooks[hookName].runs,
handlersRemoved = hooksStore[hookName].handlers.length;
hooksStore[hookName] = {
runs: hooksStore[hookName].runs,
handlers: []

@@ -54,3 +62,3 @@ };

// Try to find the specified callback to remove.
var handlers = hooks[hookName].handlers;
var handlers = hooksStore[hookName].handlers;

@@ -66,3 +74,3 @@ var _loop = function _loop(i) {

(hooks.__current || []).forEach(function (hookInfo) {
hooksStore.__current.forEach(function (hookInfo) {
if (hookInfo.name === hookName && hookInfo.currentIndex >= i) {

@@ -81,3 +89,3 @@ hookInfo.currentIndex--;

if (hookName !== 'hookRemoved') {
doAction('hookRemoved', hookName, namespace);
hooks.doAction('hookRemoved', hookName, namespace);
}

@@ -84,0 +92,0 @@

@@ -8,20 +8,16 @@ import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";

*
* @param {Object} hooks Stored hooks, keyed by hook name.
* @param {?boolean} returnFirstArg Whether each hook callback is expected to
* return its first argument.
* @param {import('.').Hooks} hooks Hooks instance.
* @param {import('.').StoreKey} storeKey
* @param {boolean} [returnFirstArg=false] Whether each hook callback is expected to
* return its first argument.
*
* @return {Function} Function that runs hook callbacks.
* @return {(hookName:string, ...args: unknown[]) => unknown} Function that runs hook callbacks.
*/
function createRunHook(hooks, returnFirstArg) {
/**
* Runs all callbacks for the specified hook.
*
* @param {string} hookName The name of the hook to run.
* @param {...*} args Arguments to pass to the hook callbacks.
*
* @return {*} Return value of runner, if applicable.
*/
function createRunHook(hooks, storeKey) {
var returnFirstArg = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
return function runHooks(hookName) {
if (!hooks[hookName]) {
hooks[hookName] = {
var hooksStore = hooks[storeKey];
if (!hooksStore[hookName]) {
hooksStore[hookName] = {
handlers: [],

@@ -32,9 +28,9 @@ runs: 0

hooks[hookName].runs++;
var handlers = hooks[hookName].handlers; // The following code is stripped from production builds.
hooksStore[hookName].runs++;
var handlers = hooksStore[hookName].handlers; // The following code is stripped from production builds.
if ('production' !== process.env.NODE_ENV) {
// Handle any 'all' hooks registered.
if ('hookAdded' !== hookName && hooks.all) {
handlers.push.apply(handlers, _toConsumableArray(hooks.all.handlers));
if ('hookAdded' !== hookName && hooksStore.all) {
handlers.push.apply(handlers, _toConsumableArray(hooksStore.all.handlers));
}

@@ -56,3 +52,3 @@ }

hooks.__current.push(hookInfo);
hooksStore.__current.push(hookInfo);

@@ -70,3 +66,3 @@ while (hookInfo.currentIndex < handlers.length) {

hooks.__current.pop();
hooksStore.__current.pop();

@@ -73,0 +69,0 @@ if (returnFirstArg) {

@@ -5,3 +5,35 @@ /**

import createHooks from './createHooks';
/** @typedef {(...args: any[])=>any} Callback */
/**
* @typedef Handler
* @property {Callback} callback The callback
* @property {string} namespace The namespace
* @property {number} priority The namespace
*/
/**
* @typedef Hook
* @property {Handler[]} handlers Array of handlers
* @property {number} runs Run counter
*/
/**
* @typedef Current
* @property {string} name Hook name
* @property {number} currentIndex The index
*/
/**
* @typedef {Record<string, Hook> & {__current: Current[]}} Store
*/
/**
* @typedef {'actions' | 'filters'} StoreKey
*/
/**
* @typedef {import('./createHooks').Hooks} Hooks
*/
var _createHooks = createHooks(),

@@ -8,0 +40,0 @@ addAction = _createHooks.addAction,

@@ -14,4 +14,2 @@ "use strict";

var _ = require("./");
/**

@@ -22,19 +20,24 @@ * Internal dependencies

/**
* @callback AddHook
*
* Adds the hook to the appropriate hooks container.
*
* @param {string} hookName Name of hook to add
* @param {string} namespace The unique namespace identifying the callback in the form `vendor/plugin/function`.
* @param {import('.').Callback} callback Function to call when the hook is run
* @param {number} [priority=10] Priority of this hook
*/
/**
* Returns a function which, when invoked, will add a hook.
*
* @param {Object} hooks Stored hooks, keyed by hook name.
* @param {import('.').Hooks} hooks Hooks instance.
* @param {import('.').StoreKey} storeKey
*
* @return {Function} Function that adds a new hook.
* @return {AddHook} Function that adds a new hook.
*/
function createAddHook(hooks) {
/**
* Adds the hook to the appropriate hooks container.
*
* @param {string} hookName Name of hook to add
* @param {string} namespace The unique namespace identifying the callback in the form `vendor/plugin/function`.
* @param {Function} callback Function to call when the hook is run
* @param {?number} priority Priority of this hook (default=10)
*/
function createAddHook(hooks, storeKey) {
return function addHook(hookName, namespace, callback) {
var priority = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 10;
var hooksStore = hooks[storeKey];

@@ -68,5 +71,7 @@ if (!(0, _validateHookName.default)(hookName)) {

if (hooks[hookName]) {
if (hooksStore[hookName]) {
// Find the correct insert index of the new hook.
var handlers = hooks[hookName].handlers;
var handlers = hooksStore[hookName].handlers;
/** @type {number} */
var i;

@@ -92,3 +97,3 @@

(hooks.__current || []).forEach(function (hookInfo) {
hooksStore.__current.forEach(function (hookInfo) {
if (hookInfo.name === hookName && hookInfo.currentIndex >= i) {

@@ -100,3 +105,3 @@ hookInfo.currentIndex++;

// This is the first hook of its type.
hooks[hookName] = {
hooksStore[hookName] = {
handlers: [handler],

@@ -108,3 +113,3 @@ runs: 0

if (hookName !== 'hookAdded') {
(0, _.doAction)('hookAdded', hookName, namespace, callback, priority);
hooks.doAction('hookAdded', hookName, namespace, callback, priority);
}

@@ -111,0 +116,0 @@ };

@@ -13,20 +13,13 @@ "use strict";

*
* @param {Object} hooks Stored hooks, keyed by hook name.
* @param {import('.').Hooks} hooks Hooks instance.
* @param {import('.').StoreKey} storeKey
*
* @return {Function} Function that returns the current hook.
* @return {() => string | null} Function that returns the current hook name or null.
*/
function createCurrentHook(hooks) {
/**
* Returns the name of the currently running hook, or `null` if no hook of
* the given type is currently running.
*
* @return {?string} The name of the currently running hook, or
* `null` if no hook is currently running.
*/
function createCurrentHook(hooks, storeKey) {
return function currentHook() {
if (!hooks.__current || !hooks.__current.length) {
return null;
}
var _hooksStore$__current, _hooksStore$__current2;
return hooks.__current[hooks.__current.length - 1].name;
var hooksStore = hooks[storeKey];
return (_hooksStore$__current = (_hooksStore$__current2 = hooksStore.__current[hooksStore.__current.length - 1]) === null || _hooksStore$__current2 === void 0 ? void 0 : _hooksStore$__current2.name) !== null && _hooksStore$__current !== void 0 ? _hooksStore$__current : null;
};

@@ -33,0 +26,0 @@ }

@@ -17,18 +17,24 @@ "use strict";

/**
* @callback DidHook
*
* Returns the number of times an action has been fired.
*
* @param {string} hookName The hook name to check.
*
* @return {number | undefined} The number of times the hook has run.
*/
/**
* Returns a function which, when invoked, will return the number of times a
* hook has been called.
*
* @param {Object} hooks Stored hooks, keyed by hook name.
* @param {import('.').Hooks} hooks Hooks instance.
* @param {import('.').StoreKey} storeKey
*
* @return {Function} Function that returns a hook's call count.
* @return {DidHook} Function that returns a hook's call count.
*/
function createDidHook(hooks) {
/**
* Returns the number of times an action has been fired.
*
* @param {string} hookName The hook name to check.
*
* @return {number} The number of times the hook has run.
*/
function createDidHook(hooks, storeKey) {
return function didHook(hookName) {
var hooksStore = hooks[storeKey];
if (!(0, _validateHookName.default)(hookName)) {

@@ -38,3 +44,3 @@ return;

return hooks[hookName] && hooks[hookName].runs ? hooks[hookName].runs : 0;
return hooksStore[hookName] && hooksStore[hookName].runs ? hooksStore[hookName].runs : 0;
};

@@ -41,0 +47,0 @@ }

@@ -9,27 +9,31 @@ "use strict";

/**
* @callback DoingHook
* Returns whether a hook is currently being executed.
*
* @param {string} [hookName] The name of the hook to check for. If
* omitted, will check for any hook being executed.
*
* @return {boolean} Whether the hook is being executed.
*/
/**
* Returns a function which, when invoked, will return whether a hook is
* currently being executed.
*
* @param {Object} hooks Stored hooks, keyed by hook name.
* @param {import('.').Hooks} hooks Hooks instance.
* @param {import('.').StoreKey} storeKey
*
* @return {Function} Function that returns whether a hook is currently
* being executed.
* @return {DoingHook} Function that returns whether a hook is currently
* being executed.
*/
function createDoingHook(hooks) {
/**
* Returns whether a hook is currently being executed.
*
* @param {?string} hookName The name of the hook to check for. If
* omitted, will check for any hook being executed.
*
* @return {boolean} Whether the hook is being executed.
*/
function createDoingHook(hooks, storeKey) {
return function doingHook(hookName) {
// If the hookName was not passed, check for any current hook.
var hooksStore = hooks[storeKey]; // If the hookName was not passed, check for any current hook.
if ('undefined' === typeof hookName) {
return 'undefined' !== typeof hooks.__current[0];
return 'undefined' !== typeof hooksStore.__current[0];
} // Return the __current hook.
return hooks.__current[0] ? hookName === hooks.__current[0].name : false;
return hooksStore.__current[0] ? hookName === hooksStore.__current[0].name : false;
};

@@ -36,0 +40,0 @@ }

@@ -9,24 +9,29 @@ "use strict";

/**
* @callback HasHook
*
* Returns whether any handlers are attached for the given hookName and optional namespace.
*
* @param {string} hookName The name of the hook to check for.
* @param {string} [namespace] Optional. The unique namespace identifying the callback
* in the form `vendor/plugin/function`.
*
* @return {boolean} Whether there are handlers that are attached to the given hook.
*/
/**
* Returns a function which, when invoked, will return whether any handlers are
* attached to a particular hook.
*
* @param {Object} hooks Stored hooks, keyed by hook name.
* @param {import('.').Hooks} hooks Hooks instance.
* @param {import('.').StoreKey} storeKey
*
* @return {Function} Function that returns whether any handlers are
* attached to a particular hook and optional namespace.
* @return {HasHook} Function that returns whether any handlers are
* attached to a particular hook and optional namespace.
*/
function createHasHook(hooks) {
/**
* Returns whether any handlers are attached for the given hookName and optional namespace.
*
* @param {string} hookName The name of the hook to check for.
* @param {?string} namespace Optional. The unique namespace identifying the callback
* in the form `vendor/plugin/function`.
*
* @return {boolean} Whether there are handlers that are attached to the given hook.
*/
function createHasHook(hooks, storeKey) {
return function hasHook(hookName, namespace) {
// Use the namespace if provided.
var hooksStore = hooks[storeKey]; // Use the namespace if provided.
if ('undefined' !== typeof namespace) {
return hookName in hooks && hooks[hookName].handlers.some(function (hook) {
return hookName in hooksStore && hooksStore[hookName].handlers.some(function (hook) {
return hook.namespace === namespace;

@@ -36,3 +41,3 @@ });

return hookName in hooks;
return hookName in hooksStore;
};

@@ -39,0 +44,0 @@ }

@@ -8,4 +8,6 @@ "use strict";

});
exports.default = void 0;
exports.default = exports._Hooks = void 0;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createAddHook = _interopRequireDefault(require("./createAddHook"));

@@ -30,31 +32,48 @@

/**
* Internal class for constructing hooks. Use `createHooks()` function
*
* Note, it is necessary to expose this class to make its type public.
*
* @private
*/
var _Hooks = function _Hooks() {
(0, _classCallCheck2.default)(this, _Hooks);
/** @type {import('.').Store} actions */
this.actions = Object.create(null);
this.actions.__current = [];
/** @type {import('.').Store} filters */
this.filters = Object.create(null);
this.filters.__current = [];
this.addAction = (0, _createAddHook.default)(this, 'actions');
this.addFilter = (0, _createAddHook.default)(this, 'filters');
this.removeAction = (0, _createRemoveHook.default)(this, 'actions');
this.removeFilter = (0, _createRemoveHook.default)(this, 'filters');
this.hasAction = (0, _createHasHook.default)(this, 'actions');
this.hasFilter = (0, _createHasHook.default)(this, 'filters');
this.removeAllActions = (0, _createRemoveHook.default)(this, 'actions', true);
this.removeAllFilters = (0, _createRemoveHook.default)(this, 'filters', true);
this.doAction = (0, _createRunHook.default)(this, 'actions');
this.applyFilters = (0, _createRunHook.default)(this, 'filters', true);
this.currentAction = (0, _createCurrentHook.default)(this, 'actions');
this.currentFilter = (0, _createCurrentHook.default)(this, 'filters');
this.doingAction = (0, _createDoingHook.default)(this, 'actions');
this.doingFilter = (0, _createDoingHook.default)(this, 'filters');
this.didAction = (0, _createDidHook.default)(this, 'actions');
this.didFilter = (0, _createDidHook.default)(this, 'filters');
};
/** @typedef {_Hooks} Hooks */
/**
* Returns an instance of the hooks object.
*
* @return {Object} Object that contains all hooks.
* @return {Hooks} A Hooks instance.
*/
exports._Hooks = _Hooks;
function createHooks() {
var actions = Object.create(null);
var filters = Object.create(null);
actions.__current = [];
filters.__current = [];
return {
addAction: (0, _createAddHook.default)(actions),
addFilter: (0, _createAddHook.default)(filters),
removeAction: (0, _createRemoveHook.default)(actions),
removeFilter: (0, _createRemoveHook.default)(filters),
hasAction: (0, _createHasHook.default)(actions),
hasFilter: (0, _createHasHook.default)(filters),
removeAllActions: (0, _createRemoveHook.default)(actions, true),
removeAllFilters: (0, _createRemoveHook.default)(filters, true),
doAction: (0, _createRunHook.default)(actions),
applyFilters: (0, _createRunHook.default)(filters, true),
currentAction: (0, _createCurrentHook.default)(actions),
currentFilter: (0, _createCurrentHook.default)(filters),
doingAction: (0, _createDoingHook.default)(actions),
doingFilter: (0, _createDoingHook.default)(filters),
didAction: (0, _createDidHook.default)(actions),
didFilter: (0, _createDidHook.default)(filters),
actions: actions,
filters: filters
};
return new _Hooks();
}

@@ -61,0 +80,0 @@

@@ -14,4 +14,2 @@ "use strict";

var _ = require("./");
/**

@@ -22,21 +20,30 @@ * Internal dependencies

/**
* @callback RemoveHook
* Removes the specified callback (or all callbacks) from the hook with a given hookName
* and namespace.
*
* @param {string} hookName The name of the hook to modify.
* @param {string} namespace The unique namespace identifying the callback in the
* form `vendor/plugin/function`.
*
* @return {number | undefined} The number of callbacks removed.
*/
/**
* Returns a function which, when invoked, will remove a specified hook or all
* hooks by the given name.
*
* @param {Object} hooks Stored hooks, keyed by hook name.
* @param {boolean} removeAll Whether to remove all callbacks for a hookName, without regard to namespace. Used to create `removeAll*` functions.
* @param {import('.').Hooks} hooks Hooks instance.
* @param {import('.').StoreKey} storeKey
* @param {boolean} [removeAll=false] Whether to remove all callbacks for a hookName,
* without regard to namespace. Used to create
* `removeAll*` functions.
*
* @return {Function} Function that removes hooks.
* @return {RemoveHook} Function that removes hooks.
*/
function createRemoveHook(hooks, removeAll) {
/**
* Removes the specified callback (or all callbacks) from the hook with a
* given hookName and namespace.
*
* @param {string} hookName The name of the hook to modify.
* @param {string} namespace The unique namespace identifying the callback in the form `vendor/plugin/function`.
*
* @return {number} The number of callbacks removed.
*/
function createRemoveHook(hooks, storeKey) {
var removeAll = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
return function removeHook(hookName, namespace) {
var hooksStore = hooks[storeKey];
if (!(0, _validateHookName.default)(hookName)) {

@@ -51,3 +58,3 @@ return;

if (!hooks[hookName]) {
if (!hooksStore[hookName]) {
return 0;

@@ -59,5 +66,5 @@ }

if (removeAll) {
handlersRemoved = hooks[hookName].handlers.length;
hooks[hookName] = {
runs: hooks[hookName].runs,
handlersRemoved = hooksStore[hookName].handlers.length;
hooksStore[hookName] = {
runs: hooksStore[hookName].runs,
handlers: []

@@ -67,3 +74,3 @@ };

// Try to find the specified callback to remove.
var handlers = hooks[hookName].handlers;
var handlers = hooksStore[hookName].handlers;

@@ -79,3 +86,3 @@ var _loop = function _loop(i) {

(hooks.__current || []).forEach(function (hookInfo) {
hooksStore.__current.forEach(function (hookInfo) {
if (hookInfo.name === hookName && hookInfo.currentIndex >= i) {

@@ -94,3 +101,3 @@ hookInfo.currentIndex--;

if (hookName !== 'hookRemoved') {
(0, _.doAction)('hookRemoved', hookName, namespace);
hooks.doAction('hookRemoved', hookName, namespace);
}

@@ -97,0 +104,0 @@

@@ -17,20 +17,16 @@ "use strict";

*
* @param {Object} hooks Stored hooks, keyed by hook name.
* @param {?boolean} returnFirstArg Whether each hook callback is expected to
* return its first argument.
* @param {import('.').Hooks} hooks Hooks instance.
* @param {import('.').StoreKey} storeKey
* @param {boolean} [returnFirstArg=false] Whether each hook callback is expected to
* return its first argument.
*
* @return {Function} Function that runs hook callbacks.
* @return {(hookName:string, ...args: unknown[]) => unknown} Function that runs hook callbacks.
*/
function createRunHook(hooks, returnFirstArg) {
/**
* Runs all callbacks for the specified hook.
*
* @param {string} hookName The name of the hook to run.
* @param {...*} args Arguments to pass to the hook callbacks.
*
* @return {*} Return value of runner, if applicable.
*/
function createRunHook(hooks, storeKey) {
var returnFirstArg = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
return function runHooks(hookName) {
if (!hooks[hookName]) {
hooks[hookName] = {
var hooksStore = hooks[storeKey];
if (!hooksStore[hookName]) {
hooksStore[hookName] = {
handlers: [],

@@ -41,9 +37,9 @@ runs: 0

hooks[hookName].runs++;
var handlers = hooks[hookName].handlers; // The following code is stripped from production builds.
hooksStore[hookName].runs++;
var handlers = hooksStore[hookName].handlers; // The following code is stripped from production builds.
if ('production' !== process.env.NODE_ENV) {
// Handle any 'all' hooks registered.
if ('hookAdded' !== hookName && hooks.all) {
handlers.push.apply(handlers, (0, _toConsumableArray2.default)(hooks.all.handlers));
if ('hookAdded' !== hookName && hooksStore.all) {
handlers.push.apply(handlers, (0, _toConsumableArray2.default)(hooksStore.all.handlers));
}

@@ -65,3 +61,3 @@ }

hooks.__current.push(hookInfo);
hooksStore.__current.push(hookInfo);

@@ -79,3 +75,3 @@ while (hookInfo.currentIndex < handlers.length) {

hooks.__current.pop();
hooksStore.__current.pop();

@@ -82,0 +78,0 @@ if (returnFirstArg) {

@@ -21,2 +21,35 @@ "use strict";

*/
/** @typedef {(...args: any[])=>any} Callback */
/**
* @typedef Handler
* @property {Callback} callback The callback
* @property {string} namespace The namespace
* @property {number} priority The namespace
*/
/**
* @typedef Hook
* @property {Handler[]} handlers Array of handlers
* @property {number} runs Run counter
*/
/**
* @typedef Current
* @property {string} name Hook name
* @property {number} currentIndex The index
*/
/**
* @typedef {Record<string, Hook> & {__current: Current[]}} Store
*/
/**
* @typedef {'actions' | 'filters'} StoreKey
*/
/**
* @typedef {import('./createHooks').Hooks} Hooks
*/
var _createHooks = (0, _createHooks2.default)(),

@@ -23,0 +56,0 @@ addAction = _createHooks.addAction,

@@ -5,2 +5,12 @@ <!-- Learn how to maintain this file at https://github.com/WordPress/gutenberg/tree/master/packages#maintaining-changelogs. -->

## 2.11.0 (2020-12-17)
### New Feature
- Include TypeScript type declarations ([#26430](https://github.com/WordPress/gutenberg/pull/26430))
### Bug Fix
- Fix: Use own instance's `doAction` method for built-in `hookAdded` and `hookRemoved` hooks ([#26498](https://github.com/WordPress/gutenberg/pull/26498))
## 2.6.0 (2019-08-29)

@@ -7,0 +17,0 @@

{
"name": "@wordpress/hooks",
"version": "2.10.0",
"version": "2.11.0",
"description": "WordPress hooks library.",

@@ -24,4 +24,5 @@ "author": "The WordPress Contributors",

"react-native": "src/index",
"types": "build-types",
"dependencies": {
"@babel/runtime": "^7.11.2"
"@babel/runtime": "^7.12.5"
},

@@ -31,3 +32,3 @@ "publishConfig": {

},
"gitHead": "cbcd167ffb9b67f87a79df71d95aa48dc4db1a64"
"gitHead": "0f57de12b3c47128b216629b08e5c1657e1ee329"
}

@@ -6,21 +6,26 @@ /**

import validateHookName from './validateHookName.js';
import { doAction } from './';
/**
* @callback AddHook
*
* Adds the hook to the appropriate hooks container.
*
* @param {string} hookName Name of hook to add
* @param {string} namespace The unique namespace identifying the callback in the form `vendor/plugin/function`.
* @param {import('.').Callback} callback Function to call when the hook is run
* @param {number} [priority=10] Priority of this hook
*/
/**
* Returns a function which, when invoked, will add a hook.
*
* @param {Object} hooks Stored hooks, keyed by hook name.
* @param {import('.').Hooks} hooks Hooks instance.
* @param {import('.').StoreKey} storeKey
*
* @return {Function} Function that adds a new hook.
* @return {AddHook} Function that adds a new hook.
*/
function createAddHook( hooks ) {
/**
* Adds the hook to the appropriate hooks container.
*
* @param {string} hookName Name of hook to add
* @param {string} namespace The unique namespace identifying the callback in the form `vendor/plugin/function`.
* @param {Function} callback Function to call when the hook is run
* @param {?number} priority Priority of this hook (default=10)
*/
function createAddHook( hooks, storeKey ) {
return function addHook( hookName, namespace, callback, priority = 10 ) {
const hooksStore = hooks[ storeKey ];
if ( ! validateHookName( hookName ) ) {

@@ -51,6 +56,7 @@ return;

if ( hooks[ hookName ] ) {
if ( hooksStore[ hookName ] ) {
// Find the correct insert index of the new hook.
const handlers = hooks[ hookName ].handlers;
const handlers = hooksStore[ hookName ].handlers;
/** @type {number} */
let i;

@@ -75,3 +81,3 @@ for ( i = handlers.length; i > 0; i-- ) {

// any other runs by 1 to account for the added element.
( hooks.__current || [] ).forEach( ( hookInfo ) => {
hooksStore.__current.forEach( ( hookInfo ) => {
if (

@@ -86,3 +92,3 @@ hookInfo.name === hookName &&

// This is the first hook of its type.
hooks[ hookName ] = {
hooksStore[ hookName ] = {
handlers: [ handler ],

@@ -94,3 +100,9 @@ runs: 0,

if ( hookName !== 'hookAdded' ) {
doAction( 'hookAdded', hookName, namespace, callback, priority );
hooks.doAction(
'hookAdded',
hookName,
namespace,
callback,
priority
);
}

@@ -97,0 +109,0 @@ };

@@ -6,20 +6,15 @@ /**

*
* @param {Object} hooks Stored hooks, keyed by hook name.
* @param {import('.').Hooks} hooks Hooks instance.
* @param {import('.').StoreKey} storeKey
*
* @return {Function} Function that returns the current hook.
* @return {() => string | null} Function that returns the current hook name or null.
*/
function createCurrentHook( hooks ) {
/**
* Returns the name of the currently running hook, or `null` if no hook of
* the given type is currently running.
*
* @return {?string} The name of the currently running hook, or
* `null` if no hook is currently running.
*/
function createCurrentHook( hooks, storeKey ) {
return function currentHook() {
if ( ! hooks.__current || ! hooks.__current.length ) {
return null;
}
const hooksStore = hooks[ storeKey ];
return hooks.__current[ hooks.__current.length - 1 ].name;
return (
hooksStore.__current[ hooksStore.__current.length - 1 ]?.name ??
null
);
};

@@ -26,0 +21,0 @@ }

@@ -7,18 +7,24 @@ /**

/**
* @callback DidHook
*
* Returns the number of times an action has been fired.
*
* @param {string} hookName The hook name to check.
*
* @return {number | undefined} The number of times the hook has run.
*/
/**
* Returns a function which, when invoked, will return the number of times a
* hook has been called.
*
* @param {Object} hooks Stored hooks, keyed by hook name.
* @param {import('.').Hooks} hooks Hooks instance.
* @param {import('.').StoreKey} storeKey
*
* @return {Function} Function that returns a hook's call count.
* @return {DidHook} Function that returns a hook's call count.
*/
function createDidHook( hooks ) {
/**
* Returns the number of times an action has been fired.
*
* @param {string} hookName The hook name to check.
*
* @return {number} The number of times the hook has run.
*/
function createDidHook( hooks, storeKey ) {
return function didHook( hookName ) {
const hooksStore = hooks[ storeKey ];
if ( ! validateHookName( hookName ) ) {

@@ -28,4 +34,4 @@ return;

return hooks[ hookName ] && hooks[ hookName ].runs
? hooks[ hookName ].runs
return hooksStore[ hookName ] && hooksStore[ hookName ].runs
? hooksStore[ hookName ].runs
: 0;

@@ -32,0 +38,0 @@ };

/**
* @callback DoingHook
* Returns whether a hook is currently being executed.
*
* @param {string} [hookName] The name of the hook to check for. If
* omitted, will check for any hook being executed.
*
* @return {boolean} Whether the hook is being executed.
*/
/**
* Returns a function which, when invoked, will return whether a hook is
* currently being executed.
*
* @param {Object} hooks Stored hooks, keyed by hook name.
* @param {import('.').Hooks} hooks Hooks instance.
* @param {import('.').StoreKey} storeKey
*
* @return {Function} Function that returns whether a hook is currently
* being executed.
* @return {DoingHook} Function that returns whether a hook is currently
* being executed.
*/
function createDoingHook( hooks ) {
/**
* Returns whether a hook is currently being executed.
*
* @param {?string} hookName The name of the hook to check for. If
* omitted, will check for any hook being executed.
*
* @return {boolean} Whether the hook is being executed.
*/
function createDoingHook( hooks, storeKey ) {
return function doingHook( hookName ) {
const hooksStore = hooks[ storeKey ];
// If the hookName was not passed, check for any current hook.
if ( 'undefined' === typeof hookName ) {
return 'undefined' !== typeof hooks.__current[ 0 ];
return 'undefined' !== typeof hooksStore.__current[ 0 ];
}
// Return the __current hook.
return hooks.__current[ 0 ]
? hookName === hooks.__current[ 0 ].name
return hooksStore.__current[ 0 ]
? hookName === hooksStore.__current[ 0 ].name
: false;

@@ -29,0 +34,0 @@ };

/**
* @callback HasHook
*
* Returns whether any handlers are attached for the given hookName and optional namespace.
*
* @param {string} hookName The name of the hook to check for.
* @param {string} [namespace] Optional. The unique namespace identifying the callback
* in the form `vendor/plugin/function`.
*
* @return {boolean} Whether there are handlers that are attached to the given hook.
*/
/**
* Returns a function which, when invoked, will return whether any handlers are
* attached to a particular hook.
*
* @param {Object} hooks Stored hooks, keyed by hook name.
* @param {import('.').Hooks} hooks Hooks instance.
* @param {import('.').StoreKey} storeKey
*
* @return {Function} Function that returns whether any handlers are
* attached to a particular hook and optional namespace.
* @return {HasHook} Function that returns whether any handlers are
* attached to a particular hook and optional namespace.
*/
function createHasHook( hooks ) {
/**
* Returns whether any handlers are attached for the given hookName and optional namespace.
*
* @param {string} hookName The name of the hook to check for.
* @param {?string} namespace Optional. The unique namespace identifying the callback
* in the form `vendor/plugin/function`.
*
* @return {boolean} Whether there are handlers that are attached to the given hook.
*/
function createHasHook( hooks, storeKey ) {
return function hasHook( hookName, namespace ) {
const hooksStore = hooks[ storeKey ];
// Use the namespace if provided.
if ( 'undefined' !== typeof namespace ) {
return (
hookName in hooks &&
hooks[ hookName ].handlers.some(
hookName in hooksStore &&
hooksStore[ hookName ].handlers.some(
( hook ) => hook.namespace === namespace

@@ -31,3 +36,3 @@ )

return hookName in hooks;
return hookName in hooksStore;
};

@@ -34,0 +39,0 @@ }

@@ -13,34 +13,48 @@ /**

/**
* Internal class for constructing hooks. Use `createHooks()` function
*
* Note, it is necessary to expose this class to make its type public.
*
* @private
*/
export class _Hooks {
constructor() {
/** @type {import('.').Store} actions */
this.actions = Object.create( null );
this.actions.__current = [];
/** @type {import('.').Store} filters */
this.filters = Object.create( null );
this.filters.__current = [];
this.addAction = createAddHook( this, 'actions' );
this.addFilter = createAddHook( this, 'filters' );
this.removeAction = createRemoveHook( this, 'actions' );
this.removeFilter = createRemoveHook( this, 'filters' );
this.hasAction = createHasHook( this, 'actions' );
this.hasFilter = createHasHook( this, 'filters' );
this.removeAllActions = createRemoveHook( this, 'actions', true );
this.removeAllFilters = createRemoveHook( this, 'filters', true );
this.doAction = createRunHook( this, 'actions' );
this.applyFilters = createRunHook( this, 'filters', true );
this.currentAction = createCurrentHook( this, 'actions' );
this.currentFilter = createCurrentHook( this, 'filters' );
this.doingAction = createDoingHook( this, 'actions' );
this.doingFilter = createDoingHook( this, 'filters' );
this.didAction = createDidHook( this, 'actions' );
this.didFilter = createDidHook( this, 'filters' );
}
}
/** @typedef {_Hooks} Hooks */
/**
* Returns an instance of the hooks object.
*
* @return {Object} Object that contains all hooks.
* @return {Hooks} A Hooks instance.
*/
function createHooks() {
const actions = Object.create( null );
const filters = Object.create( null );
actions.__current = [];
filters.__current = [];
return {
addAction: createAddHook( actions ),
addFilter: createAddHook( filters ),
removeAction: createRemoveHook( actions ),
removeFilter: createRemoveHook( filters ),
hasAction: createHasHook( actions ),
hasFilter: createHasHook( filters ),
removeAllActions: createRemoveHook( actions, true ),
removeAllFilters: createRemoveHook( filters, true ),
doAction: createRunHook( actions ),
applyFilters: createRunHook( filters, true ),
currentAction: createCurrentHook( actions ),
currentFilter: createCurrentHook( filters ),
doingAction: createDoingHook( actions ),
doingFilter: createDoingHook( filters ),
didAction: createDidHook( actions ),
didFilter: createDidHook( filters ),
actions,
filters,
};
return new _Hooks();
}
export default createHooks;

@@ -6,24 +6,31 @@ /**

import validateHookName from './validateHookName.js';
import { doAction } from './';
/**
* @callback RemoveHook
* Removes the specified callback (or all callbacks) from the hook with a given hookName
* and namespace.
*
* @param {string} hookName The name of the hook to modify.
* @param {string} namespace The unique namespace identifying the callback in the
* form `vendor/plugin/function`.
*
* @return {number | undefined} The number of callbacks removed.
*/
/**
* Returns a function which, when invoked, will remove a specified hook or all
* hooks by the given name.
*
* @param {Object} hooks Stored hooks, keyed by hook name.
* @param {boolean} removeAll Whether to remove all callbacks for a hookName, without regard to namespace. Used to create `removeAll*` functions.
* @param {import('.').Hooks} hooks Hooks instance.
* @param {import('.').StoreKey} storeKey
* @param {boolean} [removeAll=false] Whether to remove all callbacks for a hookName,
* without regard to namespace. Used to create
* `removeAll*` functions.
*
* @return {Function} Function that removes hooks.
* @return {RemoveHook} Function that removes hooks.
*/
function createRemoveHook( hooks, removeAll ) {
/**
* Removes the specified callback (or all callbacks) from the hook with a
* given hookName and namespace.
*
* @param {string} hookName The name of the hook to modify.
* @param {string} namespace The unique namespace identifying the callback in the form `vendor/plugin/function`.
*
* @return {number} The number of callbacks removed.
*/
function createRemoveHook( hooks, storeKey, removeAll = false ) {
return function removeHook( hookName, namespace ) {
const hooksStore = hooks[ storeKey ];
if ( ! validateHookName( hookName ) ) {

@@ -38,3 +45,3 @@ return;

// Bail if no hooks exist by this name
if ( ! hooks[ hookName ] ) {
if ( ! hooksStore[ hookName ] ) {
return 0;

@@ -46,5 +53,5 @@ }

if ( removeAll ) {
handlersRemoved = hooks[ hookName ].handlers.length;
hooks[ hookName ] = {
runs: hooks[ hookName ].runs,
handlersRemoved = hooksStore[ hookName ].handlers.length;
hooksStore[ hookName ] = {
runs: hooksStore[ hookName ].runs,
handlers: [],

@@ -54,3 +61,3 @@ };

// Try to find the specified callback to remove.
const handlers = hooks[ hookName ].handlers;
const handlers = hooksStore[ hookName ].handlers;
for ( let i = handlers.length - 1; i >= 0; i-- ) {

@@ -65,3 +72,3 @@ if ( handlers[ i ].namespace === namespace ) {

// other runs by 1 to account for the removed element.
( hooks.__current || [] ).forEach( ( hookInfo ) => {
hooksStore.__current.forEach( ( hookInfo ) => {
if (

@@ -77,4 +84,5 @@ hookInfo.name === hookName &&

}
if ( hookName !== 'hookRemoved' ) {
doAction( 'hookRemoved', hookName, namespace );
hooks.doAction( 'hookRemoved', hookName, namespace );
}

@@ -81,0 +89,0 @@

@@ -6,20 +6,15 @@ /**

*
* @param {Object} hooks Stored hooks, keyed by hook name.
* @param {?boolean} returnFirstArg Whether each hook callback is expected to
* return its first argument.
* @param {import('.').Hooks} hooks Hooks instance.
* @param {import('.').StoreKey} storeKey
* @param {boolean} [returnFirstArg=false] Whether each hook callback is expected to
* return its first argument.
*
* @return {Function} Function that runs hook callbacks.
* @return {(hookName:string, ...args: unknown[]) => unknown} Function that runs hook callbacks.
*/
function createRunHook( hooks, returnFirstArg ) {
/**
* Runs all callbacks for the specified hook.
*
* @param {string} hookName The name of the hook to run.
* @param {...*} args Arguments to pass to the hook callbacks.
*
* @return {*} Return value of runner, if applicable.
*/
function createRunHook( hooks, storeKey, returnFirstArg = false ) {
return function runHooks( hookName, ...args ) {
if ( ! hooks[ hookName ] ) {
hooks[ hookName ] = {
const hooksStore = hooks[ storeKey ];
if ( ! hooksStore[ hookName ] ) {
hooksStore[ hookName ] = {
handlers: [],

@@ -30,5 +25,5 @@ runs: 0,

hooks[ hookName ].runs++;
hooksStore[ hookName ].runs++;
const handlers = hooks[ hookName ].handlers;
const handlers = hooksStore[ hookName ].handlers;

@@ -38,4 +33,4 @@ // The following code is stripped from production builds.

// Handle any 'all' hooks registered.
if ( 'hookAdded' !== hookName && hooks.all ) {
handlers.push( ...hooks.all.handlers );
if ( 'hookAdded' !== hookName && hooksStore.all ) {
handlers.push( ...hooksStore.all.handlers );
}

@@ -53,3 +48,3 @@ }

hooks.__current.push( hookInfo );
hooksStore.__current.push( hookInfo );

@@ -67,3 +62,3 @@ while ( hookInfo.currentIndex < handlers.length ) {

hooks.__current.pop();
hooksStore.__current.pop();

@@ -70,0 +65,0 @@ if ( returnFirstArg ) {

@@ -6,2 +6,35 @@ /**

/** @typedef {(...args: any[])=>any} Callback */
/**
* @typedef Handler
* @property {Callback} callback The callback
* @property {string} namespace The namespace
* @property {number} priority The namespace
*/
/**
* @typedef Hook
* @property {Handler[]} handlers Array of handlers
* @property {number} runs Run counter
*/
/**
* @typedef Current
* @property {string} name Hook name
* @property {number} currentIndex The index
*/
/**
* @typedef {Record<string, Hook> & {__current: Current[]}} Store
*/
/**
* @typedef {'actions' | 'filters'} StoreKey
*/
/**
* @typedef {import('./createHooks').Hooks} Hooks
*/
const {

@@ -8,0 +41,0 @@ addAction,

@@ -745,2 +745,19 @@ /**

);
// Private instance.
const hooksPrivateInstance = createHooks();
removeAction( 'hookAdded', 'my_callback' );
hookAddedSpy.mockClear();
hooksPrivateInstance.addAction( 'hookAdded', 'my_callback', hookAddedSpy );
hooksPrivateInstance.addAction( 'testAction', 'my_callback2', actionA, 9 );
expect( hookAddedSpy ).toHaveBeenCalledTimes( 1 );
expect( hookAddedSpy ).toHaveBeenCalledWith(
'testAction',
'my_callback2',
actionA,
9
);
} );

@@ -761,2 +778,19 @@

);
// Private instance.
const hooksPrivateInstance = createHooks();
removeAction( 'hookAdded', 'my_callback' );
hookAddedSpy.mockClear();
hooksPrivateInstance.addAction( 'hookAdded', 'my_callback', hookAddedSpy );
hooksPrivateInstance.addFilter( 'testFilter', 'my_callback3', filterA, 8 );
expect( hookAddedSpy ).toHaveBeenCalledTimes( 1 );
expect( hookAddedSpy ).toHaveBeenCalledWith(
'testFilter',
'my_callback3',
filterA,
8
);
} );

@@ -777,2 +811,23 @@

);
// Private instance.
const hooksPrivateInstance = createHooks();
removeAction( 'hookRemoved', 'my_callback' );
hookRemovedSpy.mockClear();
hooksPrivateInstance.addAction(
'hookRemoved',
'my_callback',
hookRemovedSpy
);
hooksPrivateInstance.addAction( 'testAction', 'my_callback2', actionA, 9 );
hooksPrivateInstance.removeAction( 'testAction', 'my_callback2' );
expect( hookRemovedSpy ).toHaveBeenCalledTimes( 1 );
expect( hookRemovedSpy ).toHaveBeenCalledWith(
'testAction',
'my_callback2'
);
} );

@@ -793,2 +848,23 @@

);
// Private instance.
const hooksPrivateInstance = createHooks();
removeAction( 'hookRemoved', 'my_callback' );
hookRemovedSpy.mockClear();
hooksPrivateInstance.addAction(
'hookRemoved',
'my_callback',
hookRemovedSpy
);
hooksPrivateInstance.addFilter( 'testFilter', 'my_callback3', filterA, 8 );
hooksPrivateInstance.removeFilter( 'testFilter', 'my_callback3' );
expect( hookRemovedSpy ).toHaveBeenCalledTimes( 1 );
expect( hookRemovedSpy ).toHaveBeenCalledWith(
'testFilter',
'my_callback3'
);
} );

@@ -795,0 +871,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc