Socket
Socket
Sign inDemoInstall

@wordpress/hooks

Package Overview
Dependencies
Maintainers
25
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 3.39.0 to 3.40.0

23

build-module/createAddHook.js

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

import validateHookName from './validateHookName.js';
/**

@@ -26,15 +27,11 @@ * @callback AddHook

*/
function createAddHook(hooks, storeKey) {
return function addHook(hookName, namespace, callback, priority = 10) {
const hooksStore = hooks[storeKey];
if (!validateHookName(hookName)) {
return;
}
if (!validateNamespace(namespace)) {
return;
}
if ('function' !== typeof callback) {

@@ -44,5 +41,5 @@ // eslint-disable-next-line no-console

return;
} // Validate numeric priority
}
// Validate numeric priority
if ('number' !== typeof priority) {

@@ -53,3 +50,2 @@ // eslint-disable-next-line no-console

}
const handler = {

@@ -60,10 +56,8 @@ callback,

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

@@ -74,3 +68,2 @@ if (priority >= handlers[i - 1].priority) {

}
if (i === handlers.length) {

@@ -82,8 +75,8 @@ // If append, operate via direct assignment.

handlers.splice(i, 0, handler);
} // We may also be currently executing this hook. If the callback
}
// We may also be currently executing this hook. If the callback
// we're adding would come after the current callback, there's no
// problem; otherwise we need to increase the execution index of
// any other runs by 1 to account for the added element.
hooksStore.__current.forEach(hookInfo => {

@@ -101,3 +94,2 @@ if (hookInfo.name === hookName && hookInfo.currentIndex >= i) {

}
if (hookName !== 'hookAdded') {

@@ -108,4 +100,3 @@ hooks.doAction('hookAdded', hookName, namespace, callback, priority);

}
export default createAddHook;
//# sourceMappingURL=createAddHook.js.map

@@ -14,3 +14,2 @@ /**

var _hooksStore$__current;
const hooksStore = hooks[storeKey];

@@ -20,4 +19,3 @@ return (_hooksStore$__current = hooksStore.__current[hooksStore.__current.length - 1]?.name) !== null && _hooksStore$__current !== void 0 ? _hooksStore$__current : null;

}
export default createCurrentHook;
//# sourceMappingURL=createCurrentHook.js.map

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

import validateHookName from './validateHookName.js';
/**

@@ -25,16 +26,12 @@ * @callback DidHook

*/
function createDidHook(hooks, storeKey) {
return function didHook(hookName) {
const hooksStore = hooks[storeKey];
if (!validateHookName(hookName)) {
return;
}
return hooksStore[hookName] && hooksStore[hookName].runs ? hooksStore[hookName].runs : 0;
};
}
export default createDidHook;
//# sourceMappingURL=createDidHook.js.map

@@ -23,14 +23,14 @@ /**

return function doingHook(hookName) {
const hooksStore = hooks[storeKey]; // If the hookName was not passed, check for any current hook.
const hooksStore = hooks[storeKey];
// If the hookName was not passed, check for any current hook.
if ('undefined' === typeof hookName) {
return 'undefined' !== typeof hooksStore.__current[0];
} // Return the __current hook.
}
// Return the __current hook.
return hooksStore.__current[0] ? hookName === hooksStore.__current[0].name : false;
};
}
export default createDoingHook;
//# sourceMappingURL=createDoingHook.js.map

@@ -12,3 +12,2 @@ /**

*/
/**

@@ -26,13 +25,12 @@ * Returns a function which, when invoked, will return whether any handlers are

return function hasHook(hookName, namespace) {
const hooksStore = hooks[storeKey]; // Use the namespace if provided.
const hooksStore = hooks[storeKey];
// Use the namespace if provided.
if ('undefined' !== typeof namespace) {
return hookName in hooksStore && hooksStore[hookName].handlers.some(hook => hook.namespace === namespace);
}
return hookName in hooksStore;
};
}
export default createHasHook;
//# sourceMappingURL=createHasHook.js.map

@@ -11,2 +11,3 @@ /**

import createDidHook from './createDidHook';
/**

@@ -19,3 +20,2 @@ * Internal class for constructing hooks. Use `createHooks()` function

*/
export class _Hooks {

@@ -26,4 +26,4 @@ constructor() {

this.actions.__current = [];
/** @type {import('.').Store} filters */
this.filters = Object.create(null);

@@ -48,4 +48,4 @@ this.filters.__current = [];

}
}
}
/** @typedef {_Hooks} Hooks */

@@ -58,8 +58,6 @@

*/
function createHooks() {
return new _Hooks();
}
export default createHooks;
//# sourceMappingURL=createHooks.js.map

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

import validateHookName from './validateHookName.js';
/**

@@ -31,22 +32,17 @@ * @callback RemoveHook

*/
function createRemoveHook(hooks, storeKey, removeAll = false) {
return function removeHook(hookName, namespace) {
const hooksStore = hooks[storeKey];
if (!validateHookName(hookName)) {
return;
}
if (!removeAll && !validateNamespace(namespace)) {
return;
} // Bail if no hooks exist by this name.
}
// Bail if no hooks exist by this name.
if (!hooksStore[hookName]) {
return 0;
}
let handlersRemoved = 0;
if (removeAll) {

@@ -61,7 +57,7 @@ handlersRemoved = hooksStore[hookName].handlers.length;

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
handlersRemoved++;
// This callback may also be part of a hook that is
// currently executing. If the callback we're removing

@@ -71,3 +67,2 @@ // comes after the current callback, there's no problem;

// other runs by 1 to account for the removed element.
hooksStore.__current.forEach(hookInfo => {

@@ -81,12 +76,9 @@ if (hookInfo.name === hookName && hookInfo.currentIndex >= i) {

}
if (hookName !== 'hookRemoved') {
hooks.doAction('hookRemoved', hookName, namespace);
}
return handlersRemoved;
};
}
export default createRemoveHook;
//# sourceMappingURL=createRemoveHook.js.map

@@ -11,3 +11,3 @@ /**

*
* @return {(hookName:string, ...args: unknown[]) => unknown} Function that runs hook callbacks.
* @return {(hookName:string, ...args: unknown[]) => undefined|unknown} Function that runs hook callbacks.
*/

@@ -17,3 +17,2 @@ function createRunHook(hooks, storeKey, returnFirstArg = false) {

const hooksStore = hooks[storeKey];
if (!hooksStore[hookName]) {

@@ -25,6 +24,6 @@ hooksStore[hookName] = {

}
hooksStore[hookName].runs++;
const handlers = hooksStore[hookName].handlers; // The following code is stripped from production builds.
const handlers = hooksStore[hookName].handlers;
// The following code is stripped from production builds.
if ('production' !== process.env.NODE_ENV) {

@@ -36,7 +35,5 @@ // Handle any 'all' hooks registered.

}
if (!handlers || !handlers.length) {
return returnFirstArg ? args[0] : undefined;
}
const hookInfo = {

@@ -46,25 +43,19 @@ name: hookName,

};
hooksStore.__current.push(hookInfo);
while (hookInfo.currentIndex < handlers.length) {
const handler = handlers[hookInfo.currentIndex];
const result = handler.callback.apply(null, args);
if (returnFirstArg) {
args[0] = result;
}
hookInfo.currentIndex++;
}
hooksStore.__current.pop();
if (returnFirstArg) {
return args[0];
}
return undefined;
};
}
export default createRunHook;
//# sourceMappingURL=createRunHook.js.map

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

import createHooks from './createHooks';
/** @typedef {(...args: any[])=>any} Callback */

@@ -7,0 +8,0 @@

@@ -16,3 +16,2 @@ /**

}
if (/^__/.test(hookName)) {

@@ -23,3 +22,2 @@ // eslint-disable-next-line no-console

}
if (!/^[a-zA-Z][a-zA-Z0-9_.-]*$/.test(hookName)) {

@@ -30,7 +28,5 @@ // eslint-disable-next-line no-console

}
return true;
}
export default validateHookName;
//# sourceMappingURL=validateHookName.js.map

@@ -15,3 +15,2 @@ /**

}
if (!/^[a-zA-Z][a-zA-Z0-9_.\-\/]*$/.test(namespace)) {

@@ -22,7 +21,5 @@ // eslint-disable-next-line no-console

}
return true;
}
export default validateNamespace;
//# sourceMappingURL=validateNamespace.js.map

@@ -12,5 +12,5 @@ export default createRunHook;

*
* @return {(hookName:string, ...args: unknown[]) => unknown} Function that runs hook callbacks.
* @return {(hookName:string, ...args: unknown[]) => undefined|unknown} Function that runs hook callbacks.
*/
declare function createRunHook(hooks: import('.').Hooks, storeKey: import('.').StoreKey, returnFirstArg?: boolean | undefined): (hookName: string, ...args: unknown[]) => unknown;
declare function createRunHook(hooks: import('.').Hooks, storeKey: import('.').StoreKey, returnFirstArg?: boolean | undefined): (hookName: string, ...args: unknown[]) => undefined | unknown;
//# sourceMappingURL=createRunHook.d.ts.map

@@ -68,3 +68,3 @@ /** @typedef {(...args: any[])=>any} Callback */

export type Hooks = import('./createHooks').Hooks;
import createHooks from "./createHooks";
import createHooks from './createHooks';
export const addAction: import("./createAddHook").AddHook;

@@ -71,0 +71,0 @@ export const addFilter: import("./createAddHook").AddHook;

"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {

@@ -9,7 +8,4 @@ value: true

exports.default = void 0;
var _validateNamespace = _interopRequireDefault(require("./validateNamespace.js"));
var _validateHookName = _interopRequireDefault(require("./validateHookName.js"));
/**

@@ -41,11 +37,8 @@ * Internal dependencies

const hooksStore = hooks[storeKey];
if (!(0, _validateHookName.default)(hookName)) {
return;
}
if (!(0, _validateNamespace.default)(namespace)) {
return;
}
if ('function' !== typeof callback) {

@@ -55,5 +48,5 @@ // eslint-disable-next-line no-console

return;
} // Validate numeric priority
}
// Validate numeric priority
if ('number' !== typeof priority) {

@@ -64,3 +57,2 @@ // eslint-disable-next-line no-console

}
const handler = {

@@ -71,10 +63,8 @@ callback,

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

@@ -85,3 +75,2 @@ if (priority >= handlers[i - 1].priority) {

}
if (i === handlers.length) {

@@ -93,8 +82,8 @@ // If append, operate via direct assignment.

handlers.splice(i, 0, handler);
} // We may also be currently executing this hook. If the callback
}
// We may also be currently executing this hook. If the callback
// we're adding would come after the current callback, there's no
// problem; otherwise we need to increase the execution index of
// any other runs by 1 to account for the added element.
hooksStore.__current.forEach(hookInfo => {

@@ -112,3 +101,2 @@ if (hookInfo.name === hookName && hookInfo.currentIndex >= i) {

}
if (hookName !== 'hookAdded') {

@@ -119,5 +107,4 @@ hooks.doAction('hookAdded', hookName, namespace, callback, priority);

}
var _default = createAddHook;
exports.default = _default;
//# sourceMappingURL=createAddHook.js.map

@@ -7,3 +7,2 @@ "use strict";

exports.default = void 0;
/**

@@ -22,3 +21,2 @@ * Returns a function which, when invoked, will return the name of the

var _hooksStore$__current;
const hooksStore = hooks[storeKey];

@@ -28,5 +26,4 @@ return (_hooksStore$__current = hooksStore.__current[hooksStore.__current.length - 1]?.name) !== null && _hooksStore$__current !== void 0 ? _hooksStore$__current : null;

}
var _default = createCurrentHook;
exports.default = _default;
//# sourceMappingURL=createCurrentHook.js.map
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {

@@ -9,5 +8,3 @@ value: true

exports.default = void 0;
var _validateHookName = _interopRequireDefault(require("./validateHookName.js"));
/**

@@ -39,13 +36,10 @@ * Internal dependencies

const hooksStore = hooks[storeKey];
if (!(0, _validateHookName.default)(hookName)) {
return;
}
return hooksStore[hookName] && hooksStore[hookName].runs ? hooksStore[hookName].runs : 0;
};
}
var _default = createDidHook;
exports.default = _default;
//# sourceMappingURL=createDidHook.js.map

@@ -7,3 +7,2 @@ "use strict";

exports.default = void 0;
/**

@@ -31,15 +30,15 @@ * @callback DoingHook

return function doingHook(hookName) {
const hooksStore = hooks[storeKey]; // If the hookName was not passed, check for any current hook.
const hooksStore = hooks[storeKey];
// If the hookName was not passed, check for any current hook.
if ('undefined' === typeof hookName) {
return 'undefined' !== typeof hooksStore.__current[0];
} // Return the __current hook.
}
// Return the __current hook.
return hooksStore.__current[0] ? hookName === hooksStore.__current[0].name : false;
};
}
var _default = createDoingHook;
exports.default = _default;
//# sourceMappingURL=createDoingHook.js.map

@@ -7,3 +7,2 @@ "use strict";

exports.default = void 0;
/**

@@ -20,3 +19,2 @@ * @callback HasHook

*/
/**

@@ -34,14 +32,13 @@ * Returns a function which, when invoked, will return whether any handlers are

return function hasHook(hookName, namespace) {
const hooksStore = hooks[storeKey]; // Use the namespace if provided.
const hooksStore = hooks[storeKey];
// Use the namespace if provided.
if ('undefined' !== typeof namespace) {
return hookName in hooksStore && hooksStore[hookName].handlers.some(hook => hook.namespace === namespace);
}
return hookName in hooksStore;
};
}
var _default = createHasHook;
exports.default = _default;
//# sourceMappingURL=createHasHook.js.map
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {

@@ -9,17 +8,9 @@ value: true

exports.default = exports._Hooks = void 0;
var _createAddHook = _interopRequireDefault(require("./createAddHook"));
var _createRemoveHook = _interopRequireDefault(require("./createRemoveHook"));
var _createHasHook = _interopRequireDefault(require("./createHasHook"));
var _createRunHook = _interopRequireDefault(require("./createRunHook"));
var _createCurrentHook = _interopRequireDefault(require("./createCurrentHook"));
var _createDoingHook = _interopRequireDefault(require("./createDoingHook"));
var _createDidHook = _interopRequireDefault(require("./createDidHook"));
/**

@@ -41,4 +32,4 @@ * Internal dependencies

this.actions.__current = [];
/** @type {import('.').Store} filters */
this.filters = Object.create(null);

@@ -63,4 +54,4 @@ this.filters.__current = [];

}
}
}
/** @typedef {_Hooks} Hooks */

@@ -73,12 +64,8 @@

*/
exports._Hooks = _Hooks;
function createHooks() {
return new _Hooks();
}
var _default = createHooks;
exports.default = _default;
//# sourceMappingURL=createHooks.js.map
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {

@@ -9,7 +8,4 @@ value: true

exports.default = void 0;
var _validateNamespace = _interopRequireDefault(require("./validateNamespace.js"));
var _validateHookName = _interopRequireDefault(require("./validateHookName.js"));
/**

@@ -46,18 +42,14 @@ * Internal dependencies

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.
}
// Bail if no hooks exist by this name.
if (!hooksStore[hookName]) {
return 0;
}
let handlersRemoved = 0;
if (removeAll) {

@@ -72,7 +64,7 @@ handlersRemoved = hooksStore[hookName].handlers.length;

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
handlersRemoved++;
// This callback may also be part of a hook that is
// currently executing. If the callback we're removing

@@ -82,3 +74,2 @@ // comes after the current callback, there's no problem;

// other runs by 1 to account for the removed element.
hooksStore.__current.forEach(hookInfo => {

@@ -92,13 +83,10 @@ if (hookInfo.name === hookName && hookInfo.currentIndex >= i) {

}
if (hookName !== 'hookRemoved') {
hooks.doAction('hookRemoved', hookName, namespace);
}
return handlersRemoved;
};
}
var _default = createRemoveHook;
exports.default = _default;
//# sourceMappingURL=createRemoveHook.js.map

@@ -7,3 +7,2 @@ "use strict";

exports.default = void 0;
/**

@@ -19,3 +18,3 @@ * Returns a function which, when invoked, will execute all callbacks

*
* @return {(hookName:string, ...args: unknown[]) => unknown} Function that runs hook callbacks.
* @return {(hookName:string, ...args: unknown[]) => undefined|unknown} Function that runs hook callbacks.
*/

@@ -25,3 +24,2 @@ function createRunHook(hooks, storeKey, returnFirstArg = false) {

const hooksStore = hooks[storeKey];
if (!hooksStore[hookName]) {

@@ -33,6 +31,6 @@ hooksStore[hookName] = {

}
hooksStore[hookName].runs++;
const handlers = hooksStore[hookName].handlers; // The following code is stripped from production builds.
const handlers = hooksStore[hookName].handlers;
// The following code is stripped from production builds.
if ('production' !== process.env.NODE_ENV) {

@@ -44,7 +42,5 @@ // Handle any 'all' hooks registered.

}
if (!handlers || !handlers.length) {
return returnFirstArg ? args[0] : undefined;
}
const hookInfo = {

@@ -54,26 +50,20 @@ name: hookName,

};
hooksStore.__current.push(hookInfo);
while (hookInfo.currentIndex < handlers.length) {
const handler = handlers[hookInfo.currentIndex];
const result = handler.callback.apply(null, args);
if (returnFirstArg) {
args[0] = result;
}
hookInfo.currentIndex++;
}
hooksStore.__current.pop();
if (returnFirstArg) {
return args[0];
}
return undefined;
};
}
var _default = createRunHook;
exports.default = _default;
//# sourceMappingURL=createRunHook.js.map
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {

@@ -16,5 +15,3 @@ value: true

exports.removeFilter = exports.removeAllFilters = exports.removeAllActions = exports.removeAction = exports.hasFilter = exports.hasAction = exports.filters = exports.doingFilter = exports.doingAction = exports.doAction = exports.didFilter = exports.didAction = exports.defaultHooks = exports.currentFilter = exports.currentAction = void 0;
var _createHooks = _interopRequireDefault(require("./createHooks"));
/**

@@ -56,2 +53,3 @@ * Internal dependencies

*/
const defaultHooks = (0, _createHooks.default)();

@@ -58,0 +56,0 @@ exports.defaultHooks = defaultHooks;

@@ -7,3 +7,2 @@ "use strict";

exports.default = void 0;
/**

@@ -24,3 +23,2 @@ * Validate a hookName string.

}
if (/^__/.test(hookName)) {

@@ -31,3 +29,2 @@ // eslint-disable-next-line no-console

}
if (!/^[a-zA-Z][a-zA-Z0-9_.-]*$/.test(hookName)) {

@@ -38,8 +35,6 @@ // eslint-disable-next-line no-console

}
return true;
}
var _default = validateHookName;
exports.default = _default;
//# sourceMappingURL=validateHookName.js.map

@@ -7,3 +7,2 @@ "use strict";

exports.default = void 0;
/**

@@ -23,3 +22,2 @@ * Validate a namespace string.

}
if (!/^[a-zA-Z][a-zA-Z0-9_.\-\/]*$/.test(namespace)) {

@@ -30,8 +28,6 @@ // eslint-disable-next-line no-console

}
return true;
}
var _default = validateNamespace;
exports.default = _default;
//# sourceMappingURL=validateNamespace.js.map

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

## 3.40.0 (2023-08-16)
## 3.39.0 (2023-08-10)

@@ -7,0 +9,0 @@

{
"name": "@wordpress/hooks",
"version": "3.39.0",
"version": "3.40.0",
"description": "WordPress hooks library.",

@@ -34,3 +34,3 @@ "author": "The WordPress Contributors",

},
"gitHead": "b898cf1dc8e70841d1647ea0994ac6278acc18a7"
"gitHead": "78a288d55b83a713b2f7d98d5a855c0771a2afc6"
}

@@ -11,3 +11,3 @@ /**

*
* @return {(hookName:string, ...args: unknown[]) => unknown} Function that runs hook callbacks.
* @return {(hookName:string, ...args: unknown[]) => undefined|unknown} Function that runs hook callbacks.
*/

@@ -64,2 +64,4 @@ function createRunHook( hooks, storeKey, returnFirstArg = false ) {

}
return undefined;
};

@@ -66,0 +68,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

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