@wordpress/hooks
Advanced tools
Comparing version 1.2.0 to 1.3.0
@@ -17,3 +17,4 @@ const Benchmark = require( 'benchmark' ); | ||
} ) | ||
// eslint-disable-next-line no-console | ||
.on( 'cycle', ( event ) => console.log( event.target.toString() ) ) | ||
.run( { async: true } ); |
@@ -0,5 +1,6 @@ | ||
import "core-js/modules/es6.function.name"; | ||
import "core-js/modules/web.dom.iterable"; | ||
import validateNamespace from './validateNamespace.js'; | ||
import validateHookName from './validateHookName.js'; | ||
import { doAction } from './'; | ||
/** | ||
@@ -12,71 +13,80 @@ * Returns a function which, when invoked, will add a 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) | ||
*/ | ||
return function addHook(hookName, namespace, callback) { | ||
var priority = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 10; | ||
/** | ||
* 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) | ||
*/ | ||
return function addHook(hookName, namespace, callback) { | ||
var priority = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 10; | ||
if (!validateHookName(hookName)) { | ||
return; | ||
} | ||
if (!validateHookName(hookName)) { | ||
return; | ||
} | ||
if (!validateNamespace(namespace)) { | ||
return; | ||
} | ||
if (!validateNamespace(namespace)) { | ||
return; | ||
} | ||
if ('function' !== typeof callback) { | ||
// eslint-disable-next-line no-console | ||
console.error('The hook callback must be a function.'); | ||
return; | ||
} // Validate numeric priority | ||
if ('function' !== typeof callback) { | ||
console.error('The hook callback must be a function.'); | ||
return; | ||
} | ||
// Validate numeric priority | ||
if ('number' !== typeof priority) { | ||
console.error('If specified, the hook priority must be a number.'); | ||
return; | ||
} | ||
if ('number' !== typeof priority) { | ||
// eslint-disable-next-line no-console | ||
console.error('If specified, the hook priority must be a number.'); | ||
return; | ||
} | ||
var handler = { callback: callback, priority: priority, namespace: namespace }; | ||
var handler = { | ||
callback: callback, | ||
priority: priority, | ||
namespace: namespace | ||
}; | ||
if (hooks[hookName]) { | ||
// Find the correct insert index of the new hook. | ||
var handlers = hooks[hookName].handlers; | ||
var i = 0; | ||
while (i < handlers.length) { | ||
if (handlers[i].priority > priority) { | ||
break; | ||
} | ||
i++; | ||
} | ||
// Insert (or append) the new hook. | ||
handlers.splice(i, 0, handler); | ||
// 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. | ||
(hooks.__current || []).forEach(function (hookInfo) { | ||
if (hookInfo.name === hookName && hookInfo.currentIndex >= i) { | ||
hookInfo.currentIndex++; | ||
} | ||
}); | ||
} else { | ||
// This is the first hook of its type. | ||
hooks[hookName] = { | ||
handlers: [handler], | ||
runs: 0 | ||
}; | ||
} | ||
if (hooks[hookName]) { | ||
// Find the correct insert index of the new hook. | ||
var handlers = hooks[hookName].handlers; | ||
var i = 0; | ||
if (hookName !== 'hookAdded') { | ||
doAction('hookAdded', hookName, namespace, callback, priority); | ||
} | ||
}; | ||
while (i < handlers.length) { | ||
if (handlers[i].priority > priority) { | ||
break; | ||
} | ||
i++; | ||
} // Insert (or append) the new hook. | ||
handlers.splice(i, 0, handler); // 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. | ||
(hooks.__current || []).forEach(function (hookInfo) { | ||
if (hookInfo.name === hookName && hookInfo.currentIndex >= i) { | ||
hookInfo.currentIndex++; | ||
} | ||
}); | ||
} else { | ||
// This is the first hook of its type. | ||
hooks[hookName] = { | ||
handlers: [handler], | ||
runs: 0 | ||
}; | ||
} | ||
if (hookName !== 'hookAdded') { | ||
doAction('hookAdded', hookName, namespace, callback, priority); | ||
} | ||
}; | ||
} | ||
export default createAddHook; |
@@ -0,1 +1,3 @@ | ||
import "core-js/modules/es6.function.name"; | ||
/** | ||
@@ -10,19 +12,19 @@ * Returns a function which, when invoked, will return the name of the | ||
*/ | ||
function createCurrentHook(hooks, returnFirstArg) { | ||
/** | ||
* 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. | ||
*/ | ||
return function currentHook() { | ||
if (!hooks.__current || !hooks.__current.length) { | ||
return 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. | ||
*/ | ||
return function currentHook() { | ||
if (!hooks.__current || !hooks.__current.length) { | ||
return null; | ||
} | ||
return hooks.__current[hooks.__current.length - 1].name; | ||
}; | ||
return hooks.__current[hooks.__current.length - 1].name; | ||
}; | ||
} | ||
export default createCurrentHook; |
import validateHookName from './validateHookName.js'; | ||
/** | ||
@@ -11,20 +10,20 @@ * Returns a function which, when invoked, will return the number of times a | ||
*/ | ||
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. | ||
*/ | ||
return function didHook(hookName) { | ||
/** | ||
* 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. | ||
*/ | ||
return function didHook(hookName) { | ||
if (!validateHookName(hookName)) { | ||
return; | ||
} | ||
if (!validateHookName(hookName)) { | ||
return; | ||
} | ||
return hooks[hookName] && hooks[hookName].runs ? hooks[hookName].runs : 0; | ||
}; | ||
return hooks[hookName] && hooks[hookName].runs ? hooks[hookName].runs : 0; | ||
}; | ||
} | ||
export default createDidHook; |
@@ -0,1 +1,3 @@ | ||
import "core-js/modules/es6.function.name"; | ||
/** | ||
@@ -11,21 +13,21 @@ * Returns a function which, when invoked, will return whether a hook is | ||
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 {bool} Whether the hook is being executed. | ||
*/ | ||
return function doingHook(hookName) { | ||
// If the hookName was not passed, check for any current hook. | ||
if ('undefined' === typeof hookName) { | ||
return 'undefined' !== typeof hooks.__current[0]; | ||
} | ||
/** | ||
* 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. | ||
*/ | ||
return function doingHook(hookName) { | ||
// If the hookName was not passed, check for any current hook. | ||
if ('undefined' === typeof hookName) { | ||
return 'undefined' !== typeof hooks.__current[0]; | ||
} // Return the __current hook. | ||
// Return the __current hook. | ||
return hooks.__current[0] ? hookName === hooks.__current[0].name : false; | ||
}; | ||
return hooks.__current[0] ? hookName === hooks.__current[0].name : false; | ||
}; | ||
} | ||
export default createDoingHook; |
@@ -11,14 +11,14 @@ /** | ||
function createHasHook(hooks) { | ||
/** | ||
* Returns how many handlers are attached for the given hook. | ||
* | ||
* @param {string} hookName The name of the hook to check for. | ||
* | ||
* @return {boolean} Whether there are handlers that are attached to the given hook. | ||
*/ | ||
return function hasHook(hookName) { | ||
return hookName in hooks; | ||
}; | ||
/** | ||
* Returns how many handlers are attached for the given hook. | ||
* | ||
* @param {string} hookName The name of the hook to check for. | ||
* | ||
* @return {boolean} Whether there are handlers that are attached to the given hook. | ||
*/ | ||
return function hasHook(hookName) { | ||
return hookName in hooks; | ||
}; | ||
} | ||
export default createHasHook; |
@@ -1,2 +0,2 @@ | ||
import _Object$create from 'babel-runtime/core-js/object/create'; | ||
import _Object$create from "@babel/runtime/core-js/object/create"; | ||
import createAddHook from './createAddHook'; | ||
@@ -9,3 +9,2 @@ import createRemoveHook from './createRemoveHook'; | ||
import createDidHook from './createDidHook'; | ||
/** | ||
@@ -16,30 +15,32 @@ * Returns an instance of the hooks object. | ||
*/ | ||
function createHooks() { | ||
var actions = _Object$create(null); | ||
var filters = _Object$create(null); | ||
actions.__current = []; | ||
filters.__current = []; | ||
var actions = _Object$create(null); | ||
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 | ||
}; | ||
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 | ||
}; | ||
} | ||
export default createHooks; |
@@ -0,5 +1,6 @@ | ||
import "core-js/modules/es6.function.name"; | ||
import "core-js/modules/web.dom.iterable"; | ||
import validateNamespace from './validateNamespace.js'; | ||
import validateHookName from './validateHookName.js'; | ||
import { doAction } from './'; | ||
/** | ||
@@ -10,72 +11,73 @@ * Returns a function which, when invoked, will remove a specified hook or all | ||
* @param {Object} hooks Stored hooks, keyed by hook name. | ||
* @param {bool} removeAll Whether to remove all callbacks for a hookName, without regard to namespace. Used to create `removeAll*` functions. | ||
* @param {boolean} removeAll Whether to remove all callbacks for a hookName, without regard to namespace. Used to create `removeAll*` functions. | ||
* | ||
* @return {Function} 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. | ||
*/ | ||
return function removeHook(hookName, namespace) { | ||
/** | ||
* 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. | ||
*/ | ||
return function removeHook(hookName, namespace) { | ||
if (!validateHookName(hookName)) { | ||
return; | ||
} | ||
if (!validateHookName(hookName)) { | ||
return; | ||
} | ||
if (!removeAll && !validateNamespace(namespace)) { | ||
return; | ||
} // Bail if no hooks exist by this name | ||
if (!removeAll && !validateNamespace(namespace)) { | ||
return; | ||
} | ||
// Bail if no hooks exist by this name | ||
if (!hooks[hookName]) { | ||
return 0; | ||
} | ||
if (!hooks[hookName]) { | ||
return 0; | ||
} | ||
var handlersRemoved = 0; | ||
var handlersRemoved = 0; | ||
if (removeAll) { | ||
handlersRemoved = hooks[hookName].handlers.length; | ||
hooks[hookName] = { | ||
runs: hooks[hookName].runs, | ||
handlers: [] | ||
}; | ||
} else { | ||
// Try to find the specified callback to remove. | ||
var handlers = hooks[hookName].handlers; | ||
if (removeAll) { | ||
handlersRemoved = hooks[hookName].handlers.length; | ||
hooks[hookName] = { | ||
runs: hooks[hookName].runs, | ||
handlers: [] | ||
}; | ||
} else { | ||
// Try to find the specified callback to remove. | ||
var handlers = hooks[hookName].handlers; | ||
var _loop = function _loop(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. | ||
(hooks.__current || []).forEach(function (hookInfo) { | ||
if (hookInfo.name === hookName && hookInfo.currentIndex >= i) { | ||
hookInfo.currentIndex--; | ||
} | ||
}); | ||
} | ||
}; | ||
var _loop = function _loop(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. | ||
for (var i = handlers.length - 1; i >= 0; i--) { | ||
_loop(i); | ||
} | ||
} | ||
if (hookName !== 'hookRemoved') { | ||
doAction('hookRemoved', hookName, namespace); | ||
} | ||
(hooks.__current || []).forEach(function (hookInfo) { | ||
if (hookInfo.name === hookName && hookInfo.currentIndex >= i) { | ||
hookInfo.currentIndex--; | ||
} | ||
}); | ||
} | ||
}; | ||
return handlersRemoved; | ||
}; | ||
for (var i = handlers.length - 1; i >= 0; i--) { | ||
_loop(i); | ||
} | ||
} | ||
if (hookName !== 'hookRemoved') { | ||
doAction('hookRemoved', hookName, namespace); | ||
} | ||
return handlersRemoved; | ||
}; | ||
} | ||
export default createRemoveHook; |
@@ -1,3 +0,1 @@ | ||
import validateHookName from './validateHookName.js'; | ||
/** | ||
@@ -9,3 +7,3 @@ * Returns a function which, when invoked, will execute all callbacks | ||
* @param {Object} hooks Stored hooks, keyed by hook name. | ||
* @param {?bool} returnFirstArg Whether each hook callback is expected to | ||
* @param {?boolean} returnFirstArg Whether each hook callback is expected to | ||
* return its first argument. | ||
@@ -16,64 +14,62 @@ * | ||
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. | ||
*/ | ||
return function runHooks(hookName) { | ||
var handlers = void 0; | ||
if (!hooks[hookName]) { | ||
hooks[hookName] = { | ||
handlers: [], | ||
runs: 0 | ||
}; | ||
} | ||
/** | ||
* 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. | ||
*/ | ||
return function runHooks(hookName) { | ||
if (!hooks[hookName]) { | ||
hooks[hookName] = { | ||
handlers: [], | ||
runs: 0 | ||
}; | ||
} | ||
hooks[hookName].runs++; | ||
hooks[hookName].runs++; | ||
var handlers = hooks[hookName].handlers; | ||
handlers = hooks[hookName].handlers; | ||
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
args[_key - 1] = arguments[_key]; | ||
} | ||
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
args[_key - 1] = arguments[_key]; | ||
} | ||
if (!handlers || !handlers.length) { | ||
return returnFirstArg ? args[0] : undefined; | ||
} | ||
if (!handlers || !handlers.length) { | ||
return returnFirstArg ? args[0] : undefined; | ||
} | ||
var hookInfo = { | ||
name: hookName, | ||
currentIndex: 0 | ||
}; | ||
var hookInfo = { | ||
name: hookName, | ||
currentIndex: 0 | ||
}; | ||
hooks.__current.push(hookInfo); | ||
hooks.__current.push(hookInfo); | ||
if (!hooks[hookName]) { | ||
hooks[hookName] = { | ||
runs: 0, | ||
handlers: [] | ||
}; | ||
} | ||
if (!hooks[hookName]) { | ||
hooks[hookName] = { | ||
runs: 0, | ||
handlers: [] | ||
}; | ||
} | ||
while (hookInfo.currentIndex < handlers.length) { | ||
var handler = handlers[hookInfo.currentIndex]; | ||
var result = handler.callback.apply(null, args); | ||
while (hookInfo.currentIndex < handlers.length) { | ||
var handler = handlers[hookInfo.currentIndex]; | ||
if (returnFirstArg) { | ||
args[0] = result; | ||
} | ||
var result = handler.callback.apply(null, args); | ||
if (returnFirstArg) { | ||
args[0] = result; | ||
} | ||
hookInfo.currentIndex++; | ||
} | ||
hookInfo.currentIndex++; | ||
} | ||
hooks.__current.pop(); | ||
hooks.__current.pop(); | ||
if (returnFirstArg) { | ||
return args[0]; | ||
} | ||
}; | ||
if (returnFirstArg) { | ||
return args[0]; | ||
} | ||
}; | ||
} | ||
export default createRunHook; |
@@ -8,24 +8,26 @@ /** | ||
* | ||
* @return {bool} Whether the hook name is valid. | ||
* @return {boolean} Whether the hook name is valid. | ||
*/ | ||
function validateHookName(hookName) { | ||
if ('string' !== typeof hookName || '' === hookName) { | ||
// eslint-disable-next-line no-console | ||
console.error('The hook name must be a non-empty string.'); | ||
return false; | ||
} | ||
if ('string' !== typeof hookName || '' === hookName) { | ||
console.error('The hook name must be a non-empty string.'); | ||
return false; | ||
} | ||
if (/^__/.test(hookName)) { | ||
// eslint-disable-next-line no-console | ||
console.error('The hook name cannot begin with `__`.'); | ||
return false; | ||
} | ||
if (/^__/.test(hookName)) { | ||
console.error('The hook name cannot begin with `__`.'); | ||
return false; | ||
} | ||
if (!/^[a-zA-Z][a-zA-Z0-9_.-]*$/.test(hookName)) { | ||
// eslint-disable-next-line no-console | ||
console.error('The hook name can only contain numbers, letters, dashes, periods and underscores.'); | ||
return false; | ||
} | ||
if (!/^[a-zA-Z][a-zA-Z0-9_.-]*$/.test(hookName)) { | ||
console.error('The hook name can only contain numbers, letters, dashes, periods and underscores.'); | ||
return false; | ||
} | ||
return true; | ||
return true; | ||
} | ||
export default validateHookName; |
@@ -7,19 +7,20 @@ /** | ||
* | ||
* @return {bool} Whether the namespace is valid. | ||
* @return {boolean} Whether the namespace is valid. | ||
*/ | ||
function validateNamespace(namespace) { | ||
if ('string' !== typeof namespace || '' === namespace) { | ||
// eslint-disable-next-line no-console | ||
console.error('The namespace must be a non-empty string.'); | ||
return false; | ||
} | ||
if ('string' !== typeof namespace || '' === namespace) { | ||
console.error('The namespace must be a non-empty string.'); | ||
return false; | ||
} | ||
if (!/^[a-zA-Z][a-zA-Z0-9_.\-\/]*$/.test(namespace)) { | ||
// eslint-disable-next-line no-console | ||
console.error('The namespace can only contain numbers, letters, dashes, periods, underscores and slashes.'); | ||
return false; | ||
} | ||
if (!/^[a-zA-Z][a-zA-Z0-9_.\-\/]*$/.test(namespace)) { | ||
console.error('The namespace can only contain numbers, letters, dashes, periods, underscores and slashes.'); | ||
return false; | ||
} | ||
return true; | ||
return true; | ||
} | ||
export default validateNamespace; |
@@ -1,19 +0,20 @@ | ||
'use strict'; | ||
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
value: true | ||
}); | ||
exports.default = void 0; | ||
var _validateNamespace = require('./validateNamespace.js'); | ||
require("core-js/modules/es6.function.name"); | ||
var _validateNamespace2 = _interopRequireDefault(_validateNamespace); | ||
require("core-js/modules/web.dom.iterable"); | ||
var _validateHookName = require('./validateHookName.js'); | ||
var _validateNamespace = _interopRequireDefault(require("./validateNamespace.js")); | ||
var _validateHookName2 = _interopRequireDefault(_validateHookName); | ||
var _validateHookName = _interopRequireDefault(require("./validateHookName.js")); | ||
var _ = require('./'); | ||
var _ = require("./"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
/** | ||
@@ -27,70 +28,79 @@ * Returns a function which, when invoked, will add a 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) | ||
*/ | ||
return function addHook(hookName, namespace, callback) { | ||
var priority = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 10; | ||
/** | ||
* 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) | ||
*/ | ||
return function addHook(hookName, namespace, callback) { | ||
var priority = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 10; | ||
if (!(0, _validateHookName.default)(hookName)) { | ||
return; | ||
} | ||
if (!(0, _validateHookName2.default)(hookName)) { | ||
return; | ||
} | ||
if (!(0, _validateNamespace.default)(namespace)) { | ||
return; | ||
} | ||
if (!(0, _validateNamespace2.default)(namespace)) { | ||
return; | ||
} | ||
if ('function' !== typeof callback) { | ||
// eslint-disable-next-line no-console | ||
console.error('The hook callback must be a function.'); | ||
return; | ||
} // Validate numeric priority | ||
if ('function' !== typeof callback) { | ||
console.error('The hook callback must be a function.'); | ||
return; | ||
} | ||
// Validate numeric priority | ||
if ('number' !== typeof priority) { | ||
console.error('If specified, the hook priority must be a number.'); | ||
return; | ||
} | ||
if ('number' !== typeof priority) { | ||
// eslint-disable-next-line no-console | ||
console.error('If specified, the hook priority must be a number.'); | ||
return; | ||
} | ||
var handler = { callback: callback, priority: priority, namespace: namespace }; | ||
var handler = { | ||
callback: callback, | ||
priority: priority, | ||
namespace: namespace | ||
}; | ||
if (hooks[hookName]) { | ||
// Find the correct insert index of the new hook. | ||
var handlers = hooks[hookName].handlers; | ||
var i = 0; | ||
while (i < handlers.length) { | ||
if (handlers[i].priority > priority) { | ||
break; | ||
} | ||
i++; | ||
} | ||
// Insert (or append) the new hook. | ||
handlers.splice(i, 0, handler); | ||
// 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. | ||
(hooks.__current || []).forEach(function (hookInfo) { | ||
if (hookInfo.name === hookName && hookInfo.currentIndex >= i) { | ||
hookInfo.currentIndex++; | ||
} | ||
}); | ||
} else { | ||
// This is the first hook of its type. | ||
hooks[hookName] = { | ||
handlers: [handler], | ||
runs: 0 | ||
}; | ||
} | ||
if (hooks[hookName]) { | ||
// Find the correct insert index of the new hook. | ||
var handlers = hooks[hookName].handlers; | ||
var i = 0; | ||
if (hookName !== 'hookAdded') { | ||
(0, _.doAction)('hookAdded', hookName, namespace, callback, priority); | ||
} | ||
}; | ||
while (i < handlers.length) { | ||
if (handlers[i].priority > priority) { | ||
break; | ||
} | ||
i++; | ||
} // Insert (or append) the new hook. | ||
handlers.splice(i, 0, handler); // 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. | ||
(hooks.__current || []).forEach(function (hookInfo) { | ||
if (hookInfo.name === hookName && hookInfo.currentIndex >= i) { | ||
hookInfo.currentIndex++; | ||
} | ||
}); | ||
} else { | ||
// This is the first hook of its type. | ||
hooks[hookName] = { | ||
handlers: [handler], | ||
runs: 0 | ||
}; | ||
} | ||
if (hookName !== 'hookAdded') { | ||
(0, _.doAction)('hookAdded', hookName, namespace, callback, priority); | ||
} | ||
}; | ||
} | ||
exports.default = createAddHook; | ||
var _default = createAddHook; | ||
exports.default = _default; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
value: true | ||
}); | ||
exports.default = void 0; | ||
require("core-js/modules/es6.function.name"); | ||
/** | ||
@@ -15,19 +19,20 @@ * Returns a function which, when invoked, will return the name of the | ||
*/ | ||
function createCurrentHook(hooks, returnFirstArg) { | ||
/** | ||
* 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. | ||
*/ | ||
return function currentHook() { | ||
if (!hooks.__current || !hooks.__current.length) { | ||
return 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. | ||
*/ | ||
return function currentHook() { | ||
if (!hooks.__current || !hooks.__current.length) { | ||
return null; | ||
} | ||
return hooks.__current[hooks.__current.length - 1].name; | ||
}; | ||
return hooks.__current[hooks.__current.length - 1].name; | ||
}; | ||
} | ||
exports.default = createCurrentHook; | ||
var _default = createCurrentHook; | ||
exports.default = _default; |
@@ -1,13 +0,12 @@ | ||
'use strict'; | ||
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
value: true | ||
}); | ||
exports.default = void 0; | ||
var _validateHookName = require('./validateHookName.js'); | ||
var _validateHookName = _interopRequireDefault(require("./validateHookName.js")); | ||
var _validateHookName2 = _interopRequireDefault(_validateHookName); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
/** | ||
@@ -22,19 +21,19 @@ * Returns a function which, when invoked, will return the number of times a | ||
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. | ||
*/ | ||
return function didHook(hookName) { | ||
/** | ||
* 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. | ||
*/ | ||
return function didHook(hookName) { | ||
if (!(0, _validateHookName.default)(hookName)) { | ||
return; | ||
} | ||
if (!(0, _validateHookName2.default)(hookName)) { | ||
return; | ||
} | ||
return hooks[hookName] && hooks[hookName].runs ? hooks[hookName].runs : 0; | ||
}; | ||
return hooks[hookName] && hooks[hookName].runs ? hooks[hookName].runs : 0; | ||
}; | ||
} | ||
exports.default = createDidHook; | ||
var _default = createDidHook; | ||
exports.default = _default; |
@@ -1,6 +0,10 @@ | ||
'use strict'; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
value: true | ||
}); | ||
exports.default = void 0; | ||
require("core-js/modules/es6.function.name"); | ||
/** | ||
@@ -16,21 +20,22 @@ * Returns a function which, when invoked, will return whether a hook is | ||
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 {bool} Whether the hook is being executed. | ||
*/ | ||
return function doingHook(hookName) { | ||
// If the hookName was not passed, check for any current hook. | ||
if ('undefined' === typeof hookName) { | ||
return 'undefined' !== typeof hooks.__current[0]; | ||
} | ||
/** | ||
* 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. | ||
*/ | ||
return function doingHook(hookName) { | ||
// If the hookName was not passed, check for any current hook. | ||
if ('undefined' === typeof hookName) { | ||
return 'undefined' !== typeof hooks.__current[0]; | ||
} // Return the __current hook. | ||
// Return the __current hook. | ||
return hooks.__current[0] ? hookName === hooks.__current[0].name : false; | ||
}; | ||
return hooks.__current[0] ? hookName === hooks.__current[0].name : false; | ||
}; | ||
} | ||
exports.default = createDoingHook; | ||
var _default = createDoingHook; | ||
exports.default = _default; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
value: true | ||
}); | ||
exports.default = void 0; | ||
/** | ||
@@ -16,14 +18,15 @@ * Returns a function which, when invoked, will return whether any handlers are | ||
function createHasHook(hooks) { | ||
/** | ||
* Returns how many handlers are attached for the given hook. | ||
* | ||
* @param {string} hookName The name of the hook to check for. | ||
* | ||
* @return {boolean} Whether there are handlers that are attached to the given hook. | ||
*/ | ||
return function hasHook(hookName) { | ||
return hookName in hooks; | ||
}; | ||
/** | ||
* Returns how many handlers are attached for the given hook. | ||
* | ||
* @param {string} hookName The name of the hook to check for. | ||
* | ||
* @return {boolean} Whether there are handlers that are attached to the given hook. | ||
*/ | ||
return function hasHook(hookName) { | ||
return hookName in hooks; | ||
}; | ||
} | ||
exports.default = createHasHook; | ||
var _default = createHasHook; | ||
exports.default = _default; |
@@ -1,41 +0,26 @@ | ||
'use strict'; | ||
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
value: true | ||
}); | ||
exports.default = void 0; | ||
var _create = require('babel-runtime/core-js/object/create'); | ||
var _create = _interopRequireDefault(require("@babel/runtime/core-js/object/create")); | ||
var _create2 = _interopRequireDefault(_create); | ||
var _createAddHook = _interopRequireDefault(require("./createAddHook")); | ||
var _createAddHook = require('./createAddHook'); | ||
var _createRemoveHook = _interopRequireDefault(require("./createRemoveHook")); | ||
var _createAddHook2 = _interopRequireDefault(_createAddHook); | ||
var _createHasHook = _interopRequireDefault(require("./createHasHook")); | ||
var _createRemoveHook = require('./createRemoveHook'); | ||
var _createRunHook = _interopRequireDefault(require("./createRunHook")); | ||
var _createRemoveHook2 = _interopRequireDefault(_createRemoveHook); | ||
var _createCurrentHook = _interopRequireDefault(require("./createCurrentHook")); | ||
var _createHasHook = require('./createHasHook'); | ||
var _createDoingHook = _interopRequireDefault(require("./createDoingHook")); | ||
var _createHasHook2 = _interopRequireDefault(_createHasHook); | ||
var _createDidHook = _interopRequireDefault(require("./createDidHook")); | ||
var _createRunHook = require('./createRunHook'); | ||
var _createRunHook2 = _interopRequireDefault(_createRunHook); | ||
var _createCurrentHook = require('./createCurrentHook'); | ||
var _createCurrentHook2 = _interopRequireDefault(_createCurrentHook); | ||
var _createDoingHook = require('./createDoingHook'); | ||
var _createDoingHook2 = _interopRequireDefault(_createDoingHook); | ||
var _createDidHook = require('./createDidHook'); | ||
var _createDidHook2 = _interopRequireDefault(_createDidHook); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
/** | ||
@@ -47,29 +32,29 @@ * Returns an instance of the hooks object. | ||
function createHooks() { | ||
var actions = (0, _create2.default)(null); | ||
var filters = (0, _create2.default)(null); | ||
actions.__current = []; | ||
filters.__current = []; | ||
return { | ||
addAction: (0, _createAddHook2.default)(actions), | ||
addFilter: (0, _createAddHook2.default)(filters), | ||
removeAction: (0, _createRemoveHook2.default)(actions), | ||
removeFilter: (0, _createRemoveHook2.default)(filters), | ||
hasAction: (0, _createHasHook2.default)(actions), | ||
hasFilter: (0, _createHasHook2.default)(filters), | ||
removeAllActions: (0, _createRemoveHook2.default)(actions, true), | ||
removeAllFilters: (0, _createRemoveHook2.default)(filters, true), | ||
doAction: (0, _createRunHook2.default)(actions), | ||
applyFilters: (0, _createRunHook2.default)(filters, true), | ||
currentAction: (0, _createCurrentHook2.default)(actions), | ||
currentFilter: (0, _createCurrentHook2.default)(filters), | ||
doingAction: (0, _createDoingHook2.default)(actions), | ||
doingFilter: (0, _createDoingHook2.default)(filters), | ||
didAction: (0, _createDidHook2.default)(actions), | ||
didFilter: (0, _createDidHook2.default)(filters), | ||
actions: actions, | ||
filters: filters | ||
}; | ||
var actions = (0, _create.default)(null); | ||
var filters = (0, _create.default)(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 | ||
}; | ||
} | ||
exports.default = createHooks; | ||
var _default = createHooks; | ||
exports.default = _default; |
@@ -1,19 +0,20 @@ | ||
'use strict'; | ||
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
value: true | ||
}); | ||
exports.default = void 0; | ||
var _validateNamespace = require('./validateNamespace.js'); | ||
require("core-js/modules/es6.function.name"); | ||
var _validateNamespace2 = _interopRequireDefault(_validateNamespace); | ||
require("core-js/modules/web.dom.iterable"); | ||
var _validateHookName = require('./validateHookName.js'); | ||
var _validateNamespace = _interopRequireDefault(require("./validateNamespace.js")); | ||
var _validateHookName2 = _interopRequireDefault(_validateHookName); | ||
var _validateHookName = _interopRequireDefault(require("./validateHookName.js")); | ||
var _ = require('./'); | ||
var _ = require("./"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
/** | ||
@@ -24,3 +25,3 @@ * Returns a function which, when invoked, will remove a specified hook or all | ||
* @param {Object} hooks Stored hooks, keyed by hook name. | ||
* @param {bool} removeAll Whether to remove all callbacks for a hookName, without regard to namespace. Used to create `removeAll*` functions. | ||
* @param {boolean} removeAll Whether to remove all callbacks for a hookName, without regard to namespace. Used to create `removeAll*` functions. | ||
* | ||
@@ -30,67 +31,68 @@ * @return {Function} 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. | ||
*/ | ||
return function removeHook(hookName, namespace) { | ||
/** | ||
* 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. | ||
*/ | ||
return function removeHook(hookName, namespace) { | ||
if (!(0, _validateHookName.default)(hookName)) { | ||
return; | ||
} | ||
if (!(0, _validateHookName2.default)(hookName)) { | ||
return; | ||
} | ||
if (!removeAll && !(0, _validateNamespace.default)(namespace)) { | ||
return; | ||
} // Bail if no hooks exist by this name | ||
if (!removeAll && !(0, _validateNamespace2.default)(namespace)) { | ||
return; | ||
} | ||
// Bail if no hooks exist by this name | ||
if (!hooks[hookName]) { | ||
return 0; | ||
} | ||
if (!hooks[hookName]) { | ||
return 0; | ||
} | ||
var handlersRemoved = 0; | ||
var handlersRemoved = 0; | ||
if (removeAll) { | ||
handlersRemoved = hooks[hookName].handlers.length; | ||
hooks[hookName] = { | ||
runs: hooks[hookName].runs, | ||
handlers: [] | ||
}; | ||
} else { | ||
// Try to find the specified callback to remove. | ||
var handlers = hooks[hookName].handlers; | ||
if (removeAll) { | ||
handlersRemoved = hooks[hookName].handlers.length; | ||
hooks[hookName] = { | ||
runs: hooks[hookName].runs, | ||
handlers: [] | ||
}; | ||
} else { | ||
// Try to find the specified callback to remove. | ||
var handlers = hooks[hookName].handlers; | ||
var _loop = function _loop(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. | ||
(hooks.__current || []).forEach(function (hookInfo) { | ||
if (hookInfo.name === hookName && hookInfo.currentIndex >= i) { | ||
hookInfo.currentIndex--; | ||
} | ||
}); | ||
} | ||
}; | ||
var _loop = function _loop(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. | ||
for (var i = handlers.length - 1; i >= 0; i--) { | ||
_loop(i); | ||
} | ||
} | ||
if (hookName !== 'hookRemoved') { | ||
(0, _.doAction)('hookRemoved', hookName, namespace); | ||
} | ||
(hooks.__current || []).forEach(function (hookInfo) { | ||
if (hookInfo.name === hookName && hookInfo.currentIndex >= i) { | ||
hookInfo.currentIndex--; | ||
} | ||
}); | ||
} | ||
}; | ||
return handlersRemoved; | ||
}; | ||
for (var i = handlers.length - 1; i >= 0; i--) { | ||
_loop(i); | ||
} | ||
} | ||
if (hookName !== 'hookRemoved') { | ||
(0, _.doAction)('hookRemoved', hookName, namespace); | ||
} | ||
return handlersRemoved; | ||
}; | ||
} | ||
exports.default = createRemoveHook; | ||
var _default = createRemoveHook; | ||
exports.default = _default; |
@@ -1,13 +0,8 @@ | ||
'use strict'; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
value: true | ||
}); | ||
exports.default = void 0; | ||
var _validateHookName = require('./validateHookName.js'); | ||
var _validateHookName2 = _interopRequireDefault(_validateHookName); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
/** | ||
@@ -19,3 +14,3 @@ * Returns a function which, when invoked, will execute all callbacks | ||
* @param {Object} hooks Stored hooks, keyed by hook name. | ||
* @param {?bool} returnFirstArg Whether each hook callback is expected to | ||
* @param {?boolean} returnFirstArg Whether each hook callback is expected to | ||
* return its first argument. | ||
@@ -26,64 +21,63 @@ * | ||
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. | ||
*/ | ||
return function runHooks(hookName) { | ||
var handlers = void 0; | ||
if (!hooks[hookName]) { | ||
hooks[hookName] = { | ||
handlers: [], | ||
runs: 0 | ||
}; | ||
} | ||
/** | ||
* 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. | ||
*/ | ||
return function runHooks(hookName) { | ||
if (!hooks[hookName]) { | ||
hooks[hookName] = { | ||
handlers: [], | ||
runs: 0 | ||
}; | ||
} | ||
hooks[hookName].runs++; | ||
hooks[hookName].runs++; | ||
var handlers = hooks[hookName].handlers; | ||
handlers = hooks[hookName].handlers; | ||
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
args[_key - 1] = arguments[_key]; | ||
} | ||
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
args[_key - 1] = arguments[_key]; | ||
} | ||
if (!handlers || !handlers.length) { | ||
return returnFirstArg ? args[0] : undefined; | ||
} | ||
if (!handlers || !handlers.length) { | ||
return returnFirstArg ? args[0] : undefined; | ||
} | ||
var hookInfo = { | ||
name: hookName, | ||
currentIndex: 0 | ||
}; | ||
var hookInfo = { | ||
name: hookName, | ||
currentIndex: 0 | ||
}; | ||
hooks.__current.push(hookInfo); | ||
hooks.__current.push(hookInfo); | ||
if (!hooks[hookName]) { | ||
hooks[hookName] = { | ||
runs: 0, | ||
handlers: [] | ||
}; | ||
} | ||
if (!hooks[hookName]) { | ||
hooks[hookName] = { | ||
runs: 0, | ||
handlers: [] | ||
}; | ||
} | ||
while (hookInfo.currentIndex < handlers.length) { | ||
var handler = handlers[hookInfo.currentIndex]; | ||
var result = handler.callback.apply(null, args); | ||
while (hookInfo.currentIndex < handlers.length) { | ||
var handler = handlers[hookInfo.currentIndex]; | ||
if (returnFirstArg) { | ||
args[0] = result; | ||
} | ||
var result = handler.callback.apply(null, args); | ||
if (returnFirstArg) { | ||
args[0] = result; | ||
} | ||
hookInfo.currentIndex++; | ||
} | ||
hookInfo.currentIndex++; | ||
} | ||
hooks.__current.pop(); | ||
hooks.__current.pop(); | ||
if (returnFirstArg) { | ||
return args[0]; | ||
} | ||
}; | ||
if (returnFirstArg) { | ||
return args[0]; | ||
} | ||
}; | ||
} | ||
exports.default = createRunHook; | ||
var _default = createRunHook; | ||
exports.default = _default; |
@@ -1,15 +0,19 @@ | ||
'use strict'; | ||
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
value: true | ||
}); | ||
exports.filters = exports.actions = exports.didFilter = exports.didAction = exports.doingFilter = exports.doingAction = exports.currentFilter = exports.currentAction = exports.applyFilters = exports.doAction = exports.removeAllFilters = exports.removeAllActions = exports.hasFilter = exports.hasAction = exports.removeFilter = exports.removeAction = exports.addFilter = exports.addAction = exports.createHooks = undefined; | ||
Object.defineProperty(exports, "createHooks", { | ||
enumerable: true, | ||
get: function get() { | ||
return _createHooks2.default; | ||
} | ||
}); | ||
exports.filters = exports.actions = exports.didFilter = exports.didAction = exports.doingFilter = exports.doingAction = exports.currentFilter = exports.currentAction = exports.applyFilters = exports.doAction = exports.removeAllFilters = exports.removeAllActions = exports.hasFilter = exports.hasAction = exports.removeFilter = exports.removeAction = exports.addFilter = exports.addAction = void 0; | ||
var _createHooks2 = require('./createHooks'); | ||
var _createHooks2 = _interopRequireDefault(require("./createHooks")); | ||
var _createHooks3 = _interopRequireDefault(_createHooks2); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var _createHooks = (0, _createHooks3.default)(), | ||
var _createHooks = (0, _createHooks2.default)(), | ||
addAction = _createHooks.addAction, | ||
@@ -34,20 +38,19 @@ addFilter = _createHooks.addFilter, | ||
exports.createHooks = _createHooks3.default; | ||
exports.addAction = addAction; | ||
exports.filters = filters; | ||
exports.actions = actions; | ||
exports.didFilter = didFilter; | ||
exports.didAction = didAction; | ||
exports.doingFilter = doingFilter; | ||
exports.doingAction = doingAction; | ||
exports.currentFilter = currentFilter; | ||
exports.currentAction = currentAction; | ||
exports.applyFilters = applyFilters; | ||
exports.doAction = doAction; | ||
exports.removeAllFilters = removeAllFilters; | ||
exports.removeAllActions = removeAllActions; | ||
exports.hasFilter = hasFilter; | ||
exports.hasAction = hasAction; | ||
exports.removeFilter = removeFilter; | ||
exports.removeAction = removeAction; | ||
exports.addFilter = addFilter; | ||
exports.removeAction = removeAction; | ||
exports.removeFilter = removeFilter; | ||
exports.hasAction = hasAction; | ||
exports.hasFilter = hasFilter; | ||
exports.removeAllActions = removeAllActions; | ||
exports.removeAllFilters = removeAllFilters; | ||
exports.doAction = doAction; | ||
exports.applyFilters = applyFilters; | ||
exports.currentAction = currentAction; | ||
exports.currentFilter = currentFilter; | ||
exports.doingAction = doingAction; | ||
exports.doingFilter = doingFilter; | ||
exports.didAction = didAction; | ||
exports.didFilter = didFilter; | ||
exports.actions = actions; | ||
exports.filters = filters; | ||
exports.addAction = addAction; |
@@ -1,6 +0,8 @@ | ||
'use strict'; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
value: true | ||
}); | ||
exports.default = void 0; | ||
/** | ||
@@ -13,24 +15,27 @@ * Validate a hookName string. | ||
* | ||
* @return {bool} Whether the hook name is valid. | ||
* @return {boolean} Whether the hook name is valid. | ||
*/ | ||
function validateHookName(hookName) { | ||
if ('string' !== typeof hookName || '' === hookName) { | ||
// eslint-disable-next-line no-console | ||
console.error('The hook name must be a non-empty string.'); | ||
return false; | ||
} | ||
if ('string' !== typeof hookName || '' === hookName) { | ||
console.error('The hook name must be a non-empty string.'); | ||
return false; | ||
} | ||
if (/^__/.test(hookName)) { | ||
// eslint-disable-next-line no-console | ||
console.error('The hook name cannot begin with `__`.'); | ||
return false; | ||
} | ||
if (/^__/.test(hookName)) { | ||
console.error('The hook name cannot begin with `__`.'); | ||
return false; | ||
} | ||
if (!/^[a-zA-Z][a-zA-Z0-9_.-]*$/.test(hookName)) { | ||
// eslint-disable-next-line no-console | ||
console.error('The hook name can only contain numbers, letters, dashes, periods and underscores.'); | ||
return false; | ||
} | ||
if (!/^[a-zA-Z][a-zA-Z0-9_.-]*$/.test(hookName)) { | ||
console.error('The hook name can only contain numbers, letters, dashes, periods and underscores.'); | ||
return false; | ||
} | ||
return true; | ||
return true; | ||
} | ||
exports.default = validateHookName; | ||
var _default = validateHookName; | ||
exports.default = _default; |
@@ -1,6 +0,8 @@ | ||
'use strict'; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
value: true | ||
}); | ||
exports.default = void 0; | ||
/** | ||
@@ -12,19 +14,21 @@ * Validate a namespace string. | ||
* | ||
* @return {bool} Whether the namespace is valid. | ||
* @return {boolean} Whether the namespace is valid. | ||
*/ | ||
function validateNamespace(namespace) { | ||
if ('string' !== typeof namespace || '' === namespace) { | ||
// eslint-disable-next-line no-console | ||
console.error('The namespace must be a non-empty string.'); | ||
return false; | ||
} | ||
if ('string' !== typeof namespace || '' === namespace) { | ||
console.error('The namespace must be a non-empty string.'); | ||
return false; | ||
} | ||
if (!/^[a-zA-Z][a-zA-Z0-9_.\-\/]*$/.test(namespace)) { | ||
// eslint-disable-next-line no-console | ||
console.error('The namespace can only contain numbers, letters, dashes, periods, underscores and slashes.'); | ||
return false; | ||
} | ||
if (!/^[a-zA-Z][a-zA-Z0-9_.\-\/]*$/.test(namespace)) { | ||
console.error('The namespace can only contain numbers, letters, dashes, periods, underscores and slashes.'); | ||
return false; | ||
} | ||
return true; | ||
return true; | ||
} | ||
exports.default = validateNamespace; | ||
var _default = validateNamespace; | ||
exports.default = _default; |
@@ -0,1 +1,6 @@ | ||
## 1.2.0 (2018-07-12) | ||
- Updated build to work with Babel 7 ([#7832](https://github.com/WordPress/gutenberg/pull/7832)) | ||
- Moved `@WordPress/packages` repository to `@WordPress/gutenberg` ([#7805](https://github.com/WordPress/gutenberg/pull/7805)) | ||
## 1.1.8 (2018-05-08) | ||
@@ -2,0 +7,0 @@ |
{ | ||
"name": "@wordpress/hooks", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "WordPress Hooks library", | ||
"author": "WordPress", | ||
"author": "The WordPress Contributors", | ||
"license": "GPL-2.0-or-later", | ||
"keywords": [ | ||
"wordpress", | ||
"hooks" | ||
], | ||
"homepage": "https://github.com/WordPress/packages/tree/master/packages/hooks/README.md", | ||
"homepage": "https://github.com/WordPress/gutenberg/tree/master/packages/hooks/README.md", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/WordPress/packages.git" | ||
"url": "https://github.com/WordPress/gutenberg.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/WordPress/packages/issues" | ||
"url": "https://github.com/WordPress/gutenberg/issues" | ||
}, | ||
"main": "build/index.js", | ||
"module": "build-module/index.js", | ||
"devDependencies": { | ||
"benchmark": "^2.1.4" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
}, | ||
"gitHead": "8827c049ec802471f51a5cb906d9096ffc1b4e48" | ||
} |
@@ -22,3 +22,2 @@ import validateNamespace from './validateNamespace.js'; | ||
return function addHook( hookName, namespace, callback, priority = 10 ) { | ||
if ( ! validateHookName( hookName ) ) { | ||
@@ -33,2 +32,3 @@ return; | ||
if ( 'function' !== typeof callback ) { | ||
// eslint-disable-next-line no-console | ||
console.error( 'The hook callback must be a function.' ); | ||
@@ -40,2 +40,3 @@ return; | ||
if ( 'number' !== typeof priority ) { | ||
// eslint-disable-next-line no-console | ||
console.error( 'If specified, the hook priority must be a number.' ); | ||
@@ -63,3 +64,3 @@ return; | ||
// any other runs by 1 to account for the added element. | ||
( hooks.__current || [] ).forEach( hookInfo => { | ||
( hooks.__current || [] ).forEach( ( hookInfo ) => { | ||
if ( hookInfo.name === hookName && hookInfo.currentIndex >= i ) { | ||
@@ -66,0 +67,0 @@ hookInfo.currentIndex++; |
@@ -10,3 +10,3 @@ /** | ||
*/ | ||
function createCurrentHook( hooks, returnFirstArg ) { | ||
function createCurrentHook( hooks ) { | ||
/** | ||
@@ -13,0 +13,0 @@ * Returns the name of the currently running hook, or `null` if no hook of |
@@ -20,3 +20,2 @@ import validateHookName from './validateHookName.js'; | ||
return function didHook( hookName ) { | ||
if ( ! validateHookName( hookName ) ) { | ||
@@ -26,5 +25,5 @@ return; | ||
return hooks[ hookName ] && hooks[ hookName ].runs | ||
? hooks[ hookName ].runs | ||
: 0; | ||
return hooks[ hookName ] && hooks[ hookName ].runs ? | ||
hooks[ hookName ].runs : | ||
0; | ||
}; | ||
@@ -31,0 +30,0 @@ } |
@@ -17,3 +17,3 @@ /** | ||
* | ||
* @return {bool} Whether the hook is being executed. | ||
* @return {boolean} Whether the hook is being executed. | ||
*/ | ||
@@ -23,9 +23,9 @@ return function doingHook( hookName ) { | ||
if ( 'undefined' === typeof hookName ) { | ||
return 'undefined' !== typeof hooks.__current[0]; | ||
return 'undefined' !== typeof hooks.__current[ 0 ]; | ||
} | ||
// Return the __current hook. | ||
return hooks.__current[0] | ||
? hookName === hooks.__current[0].name | ||
: false; | ||
return hooks.__current[ 0 ] ? | ||
hookName === hooks.__current[ 0 ].name : | ||
false; | ||
}; | ||
@@ -32,0 +32,0 @@ } |
@@ -21,20 +21,20 @@ import createAddHook from './createAddHook'; | ||
return { | ||
addAction: createAddHook( actions ), | ||
addFilter: createAddHook( filters ), | ||
removeAction: createRemoveHook( actions ), | ||
removeFilter: createRemoveHook( filters ), | ||
hasAction: createHasHook( actions ), | ||
hasFilter: createHasHook( filters ), | ||
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, | ||
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, | ||
}; | ||
@@ -41,0 +41,0 @@ } |
@@ -10,3 +10,3 @@ import validateNamespace from './validateNamespace.js'; | ||
* @param {Object} hooks Stored hooks, keyed by hook name. | ||
* @param {bool} removeAll Whether to remove all callbacks for a hookName, without regard to namespace. Used to create `removeAll*` functions. | ||
* @param {boolean} removeAll Whether to remove all callbacks for a hookName, without regard to namespace. Used to create `removeAll*` functions. | ||
* | ||
@@ -26,3 +26,2 @@ * @return {Function} Function that removes hooks. | ||
return function removeHook( hookName, namespace ) { | ||
if ( ! validateHookName( hookName ) ) { | ||
@@ -63,3 +62,3 @@ return; | ||
// other runs by 1 to account for the removed element. | ||
( hooks.__current || [] ).forEach( hookInfo => { | ||
( hooks.__current || [] ).forEach( ( hookInfo ) => { | ||
if ( hookInfo.name === hookName && hookInfo.currentIndex >= i ) { | ||
@@ -66,0 +65,0 @@ hookInfo.currentIndex--; |
@@ -1,3 +0,1 @@ | ||
import validateHookName from './validateHookName.js'; | ||
/** | ||
@@ -9,3 +7,3 @@ * Returns a function which, when invoked, will execute all callbacks | ||
* @param {Object} hooks Stored hooks, keyed by hook name. | ||
* @param {?bool} returnFirstArg Whether each hook callback is expected to | ||
* @param {?boolean} returnFirstArg Whether each hook callback is expected to | ||
* return its first argument. | ||
@@ -25,3 +23,2 @@ * | ||
return function runHooks( hookName, ...args ) { | ||
let handlers; | ||
if ( ! hooks[ hookName ] ) { | ||
@@ -36,8 +33,8 @@ hooks[ hookName ] = { | ||
handlers = hooks[ hookName ].handlers; | ||
const handlers = hooks[ hookName ].handlers; | ||
if ( ! handlers || ! handlers.length ) { | ||
return returnFirstArg | ||
? args[ 0 ] | ||
: undefined; | ||
return returnFirstArg ? | ||
args[ 0 ] : | ||
undefined; | ||
} | ||
@@ -44,0 +41,0 @@ |
@@ -28,20 +28,20 @@ /* eslint-disable no-console */ | ||
function filter_a( str ) { | ||
function filterA( str ) { | ||
return str + 'a'; | ||
} | ||
function filter_b( str ) { | ||
function filterB( str ) { | ||
return str + 'b'; | ||
} | ||
function filter_c( str ) { | ||
function filterC( str ) { | ||
return str + 'c'; | ||
} | ||
function filter_b_removes_self( str ) { | ||
removeFilter( 'test.filter', 'my_callback_filter_b_removes_self' ); | ||
function filterCRemovesSelf( str ) { | ||
removeFilter( 'test.filter', 'my_callback_filter_c_removes_self' ); | ||
return str + 'b'; | ||
} | ||
function filter_removes_b( str ) { | ||
function filterRemovesB( str ) { | ||
removeFilter( 'test.filter', 'my_callback_filter_b' ); | ||
@@ -51,3 +51,3 @@ return str; | ||
function filter_removes_c( str ) { | ||
function filterRemovesC( str ) { | ||
removeFilter( 'test.filter', 'my_callback_filter_c' ); | ||
@@ -57,11 +57,11 @@ return str; | ||
function action_a() { | ||
function actionA() { | ||
window.actionValue += 'a'; | ||
} | ||
function action_b() { | ||
function actionB() { | ||
window.actionValue += 'b'; | ||
} | ||
function action_c() { | ||
function actionC() { | ||
window.actionValue += 'c'; | ||
@@ -75,3 +75,3 @@ } | ||
// because the internal functions have references to the original objects. | ||
[ actions, filters ].forEach( hooks => { | ||
[ actions, filters ].forEach( ( hooks ) => { | ||
for ( const k in hooks ) { | ||
@@ -98,3 +98,3 @@ if ( '__current' === k ) { | ||
test( 'add and remove a filter', () => { | ||
addFilter( 'test.filter', 'my_callback', filter_a ); | ||
addFilter( 'test.filter', 'my_callback', filterA ); | ||
expect( removeAllFilters( 'test.filter' ) ).toBe( 1 ); | ||
@@ -106,3 +106,3 @@ expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'test' ); | ||
test( 'add a filter and run it', () => { | ||
addFilter( 'test.filter', 'my_callback', filter_a ); | ||
addFilter( 'test.filter', 'my_callback', filterA ); | ||
expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'testa' ); | ||
@@ -112,4 +112,4 @@ } ); | ||
test( 'add 2 filters in a row and run them', () => { | ||
addFilter( 'test.filter', 'my_callback', filter_a ); | ||
addFilter( 'test.filter', 'my_callback', filter_b ); | ||
addFilter( 'test.filter', 'my_callback', filterA ); | ||
addFilter( 'test.filter', 'my_callback', filterB ); | ||
expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'testab' ); | ||
@@ -119,3 +119,3 @@ } ); | ||
test( 'remove a non-existent filter', () => { | ||
expect( removeFilter( 'test.filter', 'my_callback', filter_a ) ).toBe( 0 ); | ||
expect( removeFilter( 'test.filter', 'my_callback', filterA ) ).toBe( 0 ); | ||
expect( removeAllFilters( 'test.filter' ) ).toBe( 0 ); | ||
@@ -159,2 +159,9 @@ } ); | ||
test( 'cannot add filters with namespaces starting with a slash', () => { | ||
addFilter( 'hook_name', '/invalid_name', () => null ); | ||
expect( console ).toHaveErroredWith( | ||
'The namespace can only contain numbers, letters, dashes, periods, underscores and slashes.' | ||
); | ||
} ); | ||
test( 'Can add filters with dashes in namespaces', () => { | ||
@@ -185,7 +192,2 @@ addFilter( 'hook_name', 'with-dashes', () => null ); | ||
test( 'Can add filters with periods in namespaces', () => { | ||
addFilter( 'hook_name', 'ok.action', () => null ); | ||
expect( console ).not.toHaveErrored(); | ||
} ); | ||
test( 'Can add filters with periods in hookName', () => { | ||
@@ -196,9 +198,2 @@ addFilter( 'hook.name', 'action', () => null ); | ||
test( 'cannot add filters with invalid namespaces', () => { | ||
addFilter( 'hook_name', '/invalid_name', () => null ); | ||
expect( console ).toHaveErroredWith( | ||
'The namespace can only contain numbers, letters, dashes, periods, underscores and slashes.' | ||
); | ||
} ); | ||
test( 'cannot add filters with namespace containing backslash', () => { | ||
@@ -233,5 +228,5 @@ addFilter( 'hook_name', 'i\n\v\a\l\i\d\n\a\m\e', () => null ); | ||
test( 'add 3 filters with different priorities and run them', () => { | ||
addFilter( 'test.filter', 'my_callback_filter_a', filter_a ); | ||
addFilter( 'test.filter', 'my_callback_filter_b', filter_b, 2 ); | ||
addFilter( 'test.filter', 'my_callback_filter_c', filter_c, 8 ); | ||
addFilter( 'test.filter', 'my_callback_filter_a', filterA ); | ||
addFilter( 'test.filter', 'my_callback_filter_b', filterB, 2 ); | ||
addFilter( 'test.filter', 'my_callback_filter_c', filterC, 8 ); | ||
expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'testbca' ); | ||
@@ -243,5 +238,5 @@ } ); | ||
[ 1, 2, 3, 4 ].forEach( priority => { | ||
[ 'a', 'b', 'c', 'd' ].forEach( string => { | ||
callbacks[ 'fn_' + priority + string ] = value => { | ||
[ 1, 2, 3, 4 ].forEach( ( priority ) => { | ||
[ 'a', 'b', 'c', 'd' ].forEach( ( string ) => { | ||
callbacks[ 'fn_' + priority + string ] = ( value ) => { | ||
return value.concat( priority + string ); | ||
@@ -291,3 +286,3 @@ }; | ||
test( 'add and remove an action', () => { | ||
addAction( 'test.action', 'my_callback', action_a ); | ||
addAction( 'test.action', 'my_callback', actionA ); | ||
expect( removeAllActions( 'test.action' ) ).toBe( 1 ); | ||
@@ -299,3 +294,3 @@ expect( doAction( 'test.action' ) ).toBe( undefined ); | ||
test( 'add an action and run it', () => { | ||
addAction( 'test.action', 'my_callback', action_a ); | ||
addAction( 'test.action', 'my_callback', actionA ); | ||
doAction( 'test.action' ); | ||
@@ -306,4 +301,4 @@ expect( window.actionValue ).toBe( 'a' ); | ||
test( 'add 2 actions in a row and then run them', () => { | ||
addAction( 'test.action', 'my_callback', action_a ); | ||
addAction( 'test.action', 'my_callback', action_b ); | ||
addAction( 'test.action', 'my_callback', actionA ); | ||
addAction( 'test.action', 'my_callback', actionB ); | ||
doAction( 'test.action' ); | ||
@@ -314,5 +309,5 @@ expect( window.actionValue ).toBe( 'ab' ); | ||
test( 'add 3 actions with different priorities and run them', () => { | ||
addAction( 'test.action', 'my_callback', action_a ); | ||
addAction( 'test.action', 'my_callback', action_b, 2 ); | ||
addAction( 'test.action', 'my_callback', action_c, 8 ); | ||
addAction( 'test.action', 'my_callback', actionA ); | ||
addAction( 'test.action', 'my_callback', actionB, 2 ); | ||
addAction( 'test.action', 'my_callback', actionC, 8 ); | ||
doAction( 'test.action' ); | ||
@@ -346,5 +341,5 @@ expect( window.actionValue ).toBe( 'bca' ); | ||
test( 'add a filter before the one currently executing', () => { | ||
addFilter( 'test.filter', 'my_callback', val => { | ||
addFilter( 'test.filter', 'my_callback', val => val + 'a', 1 ); | ||
return val + 'b'; | ||
addFilter( 'test.filter', 'my_callback', ( outerValue ) => { | ||
addFilter( 'test.filter', 'my_callback', ( innerValue ) => innerValue + 'a', 1 ); | ||
return outerValue + 'b'; | ||
}, 2 ); | ||
@@ -356,5 +351,5 @@ | ||
test( 'add a filter after the one currently executing', () => { | ||
addFilter( 'test.filter', 'my_callback', val => { | ||
addFilter( 'test.filter', 'my_callback', val => val + 'b', 2 ); | ||
return val + 'a'; | ||
addFilter( 'test.filter', 'my_callback', ( outerValue ) => { | ||
addFilter( 'test.filter', 'my_callback', ( innerValue ) => innerValue + 'b', 2 ); | ||
return outerValue + 'a'; | ||
}, 1 ); | ||
@@ -366,5 +361,5 @@ | ||
test( 'add a filter immediately after the one currently executing', () => { | ||
addFilter( 'test.filter', 'my_callback', val => { | ||
addFilter( 'test.filter', 'my_callback', val => val + 'b', 1 ); | ||
return val + 'a'; | ||
addFilter( 'test.filter', 'my_callback', ( outerValue ) => { | ||
addFilter( 'test.filter', 'my_callback', ( innerValue ) => innerValue + 'b', 1 ); | ||
return outerValue + 'a'; | ||
}, 1 ); | ||
@@ -376,5 +371,5 @@ | ||
test( 'remove specific action callback', () => { | ||
addAction( 'test.action', 'my_callback_action_a', action_a ); | ||
addAction( 'test.action', 'my_callback_action_b', action_b, 2 ); | ||
addAction( 'test.action', 'my_callback_action_c', action_c, 8 ); | ||
addAction( 'test.action', 'my_callback_action_a', actionA ); | ||
addAction( 'test.action', 'my_callback_action_b', actionB, 2 ); | ||
addAction( 'test.action', 'my_callback_action_c', actionC, 8 ); | ||
@@ -387,5 +382,5 @@ expect( removeAction( 'test.action', 'my_callback_action_b' ) ).toBe( 1 ); | ||
test( 'remove all action callbacks', () => { | ||
addAction( 'test.action', 'my_callback_action_a', action_a ); | ||
addAction( 'test.action', 'my_callback_action_b', action_b, 2 ); | ||
addAction( 'test.action', 'my_callback_action_c', action_c, 8 ); | ||
addAction( 'test.action', 'my_callback_action_a', actionA ); | ||
addAction( 'test.action', 'my_callback_action_b', actionB, 2 ); | ||
addAction( 'test.action', 'my_callback_action_c', actionC, 8 ); | ||
@@ -398,5 +393,5 @@ expect( removeAllActions( 'test.action' ) ).toBe( 3 ); | ||
test( 'remove specific filter callback', () => { | ||
addFilter( 'test.filter', 'my_callback_filter_a', filter_a ); | ||
addFilter( 'test.filter', 'my_callback_filter_b', filter_b, 2 ); | ||
addFilter( 'test.filter', 'my_callback_filter_c', filter_c, 8 ); | ||
addFilter( 'test.filter', 'my_callback_filter_a', filterA ); | ||
addFilter( 'test.filter', 'my_callback_filter_b', filterB, 2 ); | ||
addFilter( 'test.filter', 'my_callback_filter_c', filterC, 8 ); | ||
@@ -408,6 +403,6 @@ expect( removeFilter( 'test.filter', 'my_callback_filter_b' ) ).toBe( 1 ); | ||
test( 'filter removes a callback that has already executed', () => { | ||
addFilter( 'test.filter', 'my_callback_filter_a', filter_a, 1 ); | ||
addFilter( 'test.filter', 'my_callback_filter_b', filter_b, 3 ); | ||
addFilter( 'test.filter', 'my_callback_filter_c', filter_c, 5 ); | ||
addFilter( 'test.filter', 'my_callback_filter_removes_b', filter_removes_b, 4 ); | ||
addFilter( 'test.filter', 'my_callback_filter_a', filterA, 1 ); | ||
addFilter( 'test.filter', 'my_callback_filter_b', filterB, 3 ); | ||
addFilter( 'test.filter', 'my_callback_filter_c', filterC, 5 ); | ||
addFilter( 'test.filter', 'my_callback_filter_removes_b', filterRemovesB, 4 ); | ||
@@ -418,6 +413,6 @@ expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'testabc' ); | ||
test( 'filter removes a callback that has already executed (same priority)', () => { | ||
addFilter( 'test.filter', 'my_callback_filter_a', filter_a, 1 ); | ||
addFilter( 'test.filter', 'my_callback_filter_b', filter_b, 2 ); | ||
addFilter( 'test.filter', 'my_callback_filter_removes_b', filter_removes_b, 2 ); | ||
addFilter( 'test.filter', 'my_callback_filter_c', filter_c, 4 ); | ||
addFilter( 'test.filter', 'my_callback_filter_a', filterA, 1 ); | ||
addFilter( 'test.filter', 'my_callback_filter_b', filterB, 2 ); | ||
addFilter( 'test.filter', 'my_callback_filter_removes_b', filterRemovesB, 2 ); | ||
addFilter( 'test.filter', 'my_callback_filter_c', filterC, 4 ); | ||
@@ -428,5 +423,5 @@ expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'testabc' ); | ||
test( 'filter removes the current callback', () => { | ||
addFilter( 'test.filter', 'my_callback_filter_a', filter_a, 1 ); | ||
addFilter( 'test.filter', 'my_callback_filter_b_removes_self', filter_b_removes_self, 3 ); | ||
addFilter( 'test.filter', 'my_callback_filter_c', filter_c, 5 ); | ||
addFilter( 'test.filter', 'my_callback_filter_a', filterA, 1 ); | ||
addFilter( 'test.filter', 'my_callback_filter_c_removes_self', filterCRemovesSelf, 3 ); | ||
addFilter( 'test.filter', 'my_callback_filter_c', filterC, 5 ); | ||
@@ -437,6 +432,6 @@ expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'testabc' ); | ||
test( 'filter removes a callback that has not yet executed (last)', () => { | ||
addFilter( 'test.filter', 'my_callback_filter_a', filter_a, 1 ); | ||
addFilter( 'test.filter', 'my_callback_filter_b', filter_b, 3 ); | ||
addFilter( 'test.filter', 'my_callback_filter_c', filter_c, 5 ); | ||
addFilter( 'test.filter', 'my_callback_filter_removes_c', filter_removes_c, 4 ); | ||
addFilter( 'test.filter', 'my_callback_filter_a', filterA, 1 ); | ||
addFilter( 'test.filter', 'my_callback_filter_b', filterB, 3 ); | ||
addFilter( 'test.filter', 'my_callback_filter_c', filterC, 5 ); | ||
addFilter( 'test.filter', 'my_callback_filter_removes_c', filterRemovesC, 4 ); | ||
@@ -447,6 +442,6 @@ expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'testab' ); | ||
test( 'filter removes a callback that has not yet executed (middle)', () => { | ||
addFilter( 'test.filter', 'my_callback_filter_a', filter_a, 1 ); | ||
addFilter( 'test.filter', 'my_callback_filter_b', filter_b, 3 ); | ||
addFilter( 'test.filter', 'my_callback_filter_c', filter_c, 4 ); | ||
addFilter( 'test.filter', 'my_callback_filter_removes_b', filter_removes_b, 2 ); | ||
addFilter( 'test.filter', 'my_callback_filter_a', filterA, 1 ); | ||
addFilter( 'test.filter', 'my_callback_filter_b', filterB, 3 ); | ||
addFilter( 'test.filter', 'my_callback_filter_c', filterC, 4 ); | ||
addFilter( 'test.filter', 'my_callback_filter_removes_b', filterRemovesB, 2 ); | ||
@@ -457,6 +452,6 @@ expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'testac' ); | ||
test( 'filter removes a callback that has not yet executed (same priority)', () => { | ||
addFilter( 'test.filter', 'my_callback_filter_a', filter_a, 1 ); | ||
addFilter( 'test.filter', 'my_callback_filter_removes_b', filter_removes_b, 2 ); | ||
addFilter( 'test.filter', 'my_callback_filter_b', filter_b, 2 ); | ||
addFilter( 'test.filter', 'my_callback_filter_c', filter_c, 4 ); | ||
addFilter( 'test.filter', 'my_callback_filter_a', filterA, 1 ); | ||
addFilter( 'test.filter', 'my_callback_filter_removes_b', filterRemovesB, 2 ); | ||
addFilter( 'test.filter', 'my_callback_filter_b', filterB, 2 ); | ||
addFilter( 'test.filter', 'my_callback_filter_c', filterC, 4 ); | ||
@@ -467,5 +462,5 @@ expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'testac' ); | ||
test( 'remove all filter callbacks', () => { | ||
addFilter( 'test.filter', 'my_callback_filter_a', filter_a ); | ||
addFilter( 'test.filter', 'my_callback_filter_b', filter_b, 2 ); | ||
addFilter( 'test.filter', 'my_callback_filter_c', filter_c, 8 ); | ||
addFilter( 'test.filter', 'my_callback_filter_a', filterA ); | ||
addFilter( 'test.filter', 'my_callback_filter_b', filterB, 2 ); | ||
addFilter( 'test.filter', 'my_callback_filter_c', filterC, 8 ); | ||
@@ -544,3 +539,3 @@ expect( removeAllFilters( 'test.filter' ) ).toBe( 3 ); | ||
addFilter( 'runtest.filter', 'my_callback', arg => { | ||
addFilter( 'runtest.filter', 'my_callback', ( arg ) => { | ||
filterCalls++; | ||
@@ -572,3 +567,3 @@ expect( currentFilter() ).toBe( 'runtest.filter' ); | ||
test( 'recursively calling a filter', () => { | ||
addFilter( 'test.filter', 'my_callback', value => { | ||
addFilter( 'test.filter', 'my_callback', ( value ) => { | ||
if ( value.length === 7 ) { | ||
@@ -584,7 +579,7 @@ return value; | ||
test( 'current filter when multiple filters are running', () => { | ||
addFilter( 'test.filter1', 'my_callback', value => { | ||
addFilter( 'test.filter1', 'my_callback', ( value ) => { | ||
return applyFilters( 'test.filter2', value.concat( currentFilter() ) ); | ||
} ); | ||
addFilter( 'test.filter2', 'my_callback', value => { | ||
addFilter( 'test.filter2', 'my_callback', ( value ) => { | ||
return value.concat( currentFilter() ); | ||
@@ -610,6 +605,6 @@ } ); | ||
addFilter( 'remove_and_add', 'my_callback', val => val + '1', 11 ); | ||
addFilter( 'remove_and_add', 'my_callback', ( val ) => val + '1', 11 ); | ||
addFilter( 'remove_and_add', 'my_callback_recurse', removeRecurseAndAdd2, 12 ); | ||
addFilter( 'remove_and_add', 'my_callback', val => val + '3', 13 ); | ||
addFilter( 'remove_and_add', 'my_callback', val => val + '4', 14 ); | ||
addFilter( 'remove_and_add', 'my_callback', ( val ) => val + '3', 13 ); | ||
addFilter( 'remove_and_add', 'my_callback', ( val ) => val + '4', 14 ); | ||
@@ -681,12 +676,12 @@ expect( applyFilters( 'remove_and_add', '' ) ).toBe( '1-134-234' ); | ||
test( 'adding an action triggers a hookAdded action passing all callback details', () => { | ||
const hook_added_spy = jest.fn(); | ||
const hookAddedSpy = jest.fn(); | ||
setupActionListener( 'hookAdded', hook_added_spy ); | ||
setupActionListener( 'hookAdded', hookAddedSpy ); | ||
addAction( 'testAction', 'my_callback2', action_a, 9 ); | ||
expect( hook_added_spy ).toHaveBeenCalledTimes( 1 ); | ||
expect( hook_added_spy ).toHaveBeenCalledWith( | ||
addAction( 'testAction', 'my_callback2', actionA, 9 ); | ||
expect( hookAddedSpy ).toHaveBeenCalledTimes( 1 ); | ||
expect( hookAddedSpy ).toHaveBeenCalledWith( | ||
'testAction', | ||
'my_callback2', | ||
action_a, | ||
actionA, | ||
9 | ||
@@ -697,12 +692,12 @@ ); | ||
test( 'adding a filter triggers a hookAdded action passing all callback details', () => { | ||
const hook_added_spy = jest.fn(); | ||
const hookAddedSpy = jest.fn(); | ||
setupActionListener( 'hookAdded', hook_added_spy ); | ||
setupActionListener( 'hookAdded', hookAddedSpy ); | ||
addFilter( 'testFilter', 'my_callback3', filter_a, 8 ); | ||
expect( hook_added_spy ).toHaveBeenCalledTimes( 1 ); | ||
expect( hook_added_spy ).toHaveBeenCalledWith( | ||
addFilter( 'testFilter', 'my_callback3', filterA, 8 ); | ||
expect( hookAddedSpy ).toHaveBeenCalledTimes( 1 ); | ||
expect( hookAddedSpy ).toHaveBeenCalledWith( | ||
'testFilter', | ||
'my_callback3', | ||
filter_a, | ||
filterA, | ||
8 | ||
@@ -713,11 +708,11 @@ ); | ||
test( 'removing an action triggers a hookRemoved action passing all callback details', () => { | ||
const hook_removed_spy = jest.fn(); | ||
const hookRemovedSpy = jest.fn(); | ||
setupActionListener( 'hookRemoved', hook_removed_spy ); | ||
setupActionListener( 'hookRemoved', hookRemovedSpy ); | ||
addAction( 'testAction', 'my_callback2', action_a, 9 ); | ||
addAction( 'testAction', 'my_callback2', actionA, 9 ); | ||
removeAction( 'testAction', 'my_callback2' ); | ||
expect( hook_removed_spy ).toHaveBeenCalledTimes( 1 ); | ||
expect( hook_removed_spy ).toHaveBeenCalledWith( | ||
expect( hookRemovedSpy ).toHaveBeenCalledTimes( 1 ); | ||
expect( hookRemovedSpy ).toHaveBeenCalledWith( | ||
'testAction', | ||
@@ -729,11 +724,11 @@ 'my_callback2' | ||
test( 'removing a filter triggers a hookRemoved action passing all callback details', () => { | ||
const hook_removed_spy = jest.fn(); | ||
const hookRemovedSpy = jest.fn(); | ||
setupActionListener( 'hookRemoved', hook_removed_spy ); | ||
setupActionListener( 'hookRemoved', hookRemovedSpy ); | ||
addFilter( 'testFilter', 'my_callback3', filter_a, 8 ); | ||
addFilter( 'testFilter', 'my_callback3', filterA, 8 ); | ||
removeFilter( 'testFilter', 'my_callback3' ); | ||
expect( hook_removed_spy ).toHaveBeenCalledTimes( 1 ); | ||
expect( hook_removed_spy ).toHaveBeenCalledWith( | ||
expect( hookRemovedSpy ).toHaveBeenCalledTimes( 1 ); | ||
expect( hookRemovedSpy ).toHaveBeenCalledWith( | ||
'testFilter', | ||
@@ -740,0 +735,0 @@ 'my_callback3' |
@@ -8,7 +8,7 @@ /** | ||
* | ||
* @return {bool} Whether the hook name is valid. | ||
* @return {boolean} Whether the hook name is valid. | ||
*/ | ||
function validateHookName( hookName ) { | ||
if ( 'string' !== typeof hookName || '' === hookName ) { | ||
// eslint-disable-next-line no-console | ||
console.error( 'The hook name must be a non-empty string.' ); | ||
@@ -19,2 +19,3 @@ return false; | ||
if ( /^__/.test( hookName ) ) { | ||
// eslint-disable-next-line no-console | ||
console.error( 'The hook name cannot begin with `__`.' ); | ||
@@ -25,2 +26,3 @@ return false; | ||
if ( ! /^[a-zA-Z][a-zA-Z0-9_.-]*$/.test( hookName ) ) { | ||
// eslint-disable-next-line no-console | ||
console.error( 'The hook name can only contain numbers, letters, dashes, periods and underscores.' ); | ||
@@ -27,0 +29,0 @@ return false; |
@@ -7,7 +7,7 @@ /** | ||
* | ||
* @return {bool} Whether the namespace is valid. | ||
* @return {boolean} Whether the namespace is valid. | ||
*/ | ||
function validateNamespace( namespace ) { | ||
if ( 'string' !== typeof namespace || '' === namespace ) { | ||
// eslint-disable-next-line no-console | ||
console.error( 'The namespace must be a non-empty string.' ); | ||
@@ -18,2 +18,3 @@ return false; | ||
if ( ! /^[a-zA-Z][a-zA-Z0-9_.\-\/]*$/.test( namespace ) ) { | ||
// eslint-disable-next-line no-console | ||
console.error( 'The namespace can only contain numbers, letters, dashes, periods, underscores and slashes.' ); | ||
@@ -20,0 +21,0 @@ return false; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
72833
1921
0
1