@wordpress/hooks 3.2.1 → 3.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +2 -0
- package/LICENSE.md +1 -1
- package/README.md +7 -1
- package/build/createAddHook.js +2 -1
- package/build/createAddHook.js.map +1 -1
- package/build/createRemoveHook.js +2 -1
- package/build/createRemoveHook.js.map +1 -1
- package/build/createRunHook.js +7 -2
- package/build/createRunHook.js.map +1 -1
- package/build/index.js +2 -1
- package/build/index.js.map +1 -1
- package/build-module/createAddHook.js +2 -1
- package/build-module/createAddHook.js.map +1 -1
- package/build-module/createRemoveHook.js +2 -1
- package/build-module/createRemoveHook.js.map +1 -1
- package/build-module/createRunHook.js +7 -2
- package/build-module/createRunHook.js.map +1 -1
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
package/LICENSE.md
CHANGED
package/README.md
CHANGED
|
@@ -63,4 +63,10 @@ Whenever an action or filter is added or removed, a matching `hookAdded` or `hoo
|
|
|
63
63
|
|
|
64
64
|
In non-minified builds developers can register a filter or action that will be called on _all_ hooks, for example: `addAction( 'all', 'namespace', callbackFunction );`. Useful for debugging, the code supporting the `all` hook is stripped from the production code for performance reasons.
|
|
65
65
|
|
|
66
|
-
|
|
66
|
+
## Contributing to this package
|
|
67
|
+
|
|
68
|
+
This is an individual package that's part of the Gutenberg project. The project is organized as a monorepo. It's made up of multiple self-contained software packages, each with a specific purpose. The packages in this monorepo are published to [npm](https://www.npmjs.com/) and used by [WordPress](https://make.wordpress.org/core/) as well as other software projects.
|
|
69
|
+
|
|
70
|
+
To find out more about contributing to this package or Gutenberg as a whole, please read the project's main [contributor guide](https://github.com/WordPress/gutenberg/tree/HEAD/CONTRIBUTING.md).
|
|
71
|
+
|
|
72
|
+
<br /><br /><p align="center"><img src="https://s.w.org/style/images/codeispoetry.png?1" alt="Code is Poetry." /></p>
|
package/build/createAddHook.js
CHANGED
|
@@ -35,7 +35,8 @@ var _validateHookName = _interopRequireDefault(require("./validateHookName.js"))
|
|
|
35
35
|
* @return {AddHook} Function that adds a new hook.
|
|
36
36
|
*/
|
|
37
37
|
function createAddHook(hooks, storeKey) {
|
|
38
|
-
return function addHook(hookName, namespace, callback
|
|
38
|
+
return function addHook(hookName, namespace, callback) {
|
|
39
|
+
let priority = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 10;
|
|
39
40
|
const hooksStore = hooks[storeKey];
|
|
40
41
|
|
|
41
42
|
if (!(0, _validateHookName.default)(hookName)) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/hooks/src/createAddHook.js"],"names":["createAddHook","hooks","storeKey","addHook","hookName","namespace","callback","priority","hooksStore","console","error","handler","handlers","i","length","splice","__current","forEach","hookInfo","name","currentIndex","runs","doAction"],"mappings":";;;;;;;;;AAGA;;AACA;;AAJA;AACA;AACA;;AAIA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAASA,aAAT,CAAwBC,KAAxB,EAA+BC,QAA/B,EAA0C;AACzC,SAAO,SAASC,OAAT,CAAkBC,QAAlB,EAA4BC,SAA5B,EAAuCC,QAAvC,
|
|
1
|
+
{"version":3,"sources":["@wordpress/hooks/src/createAddHook.js"],"names":["createAddHook","hooks","storeKey","addHook","hookName","namespace","callback","priority","hooksStore","console","error","handler","handlers","i","length","splice","__current","forEach","hookInfo","name","currentIndex","runs","doAction"],"mappings":";;;;;;;;;AAGA;;AACA;;AAJA;AACA;AACA;;AAIA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAASA,aAAT,CAAwBC,KAAxB,EAA+BC,QAA/B,EAA0C;AACzC,SAAO,SAASC,OAAT,CAAkBC,QAAlB,EAA4BC,SAA5B,EAAuCC,QAAvC,EAAiE;AAAA,QAAhBC,QAAgB,uEAAL,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AQ,CAACE,YAAT,IAAyBP,CAF1B,EAGE;AACDK,UAAAA,QAAQ,CAACE,YAAT;AACA;AACD,OAPD;AAQA,KAhCD,MAgCO;AACN;AACAZ,MAAAA,UAAU,CAAEJ,QAAF,CAAV,GAAyB;AACxBQ,QAAAA,QAAQ,EAAE,CAAED,OAAF,CADc;AAExBU,QAAAA,IAAI,EAAE;AAFkB,OAAzB;AAIA;;AAED,QAAKjB,QAAQ,KAAK,WAAlB,EAAgC;AAC/BH,MAAAA,KAAK,CAACqB,QAAN,CACC,WADD,EAEClB,QAFD,EAGCC,SAHD,EAICC,QAJD,EAKCC,QALD;AAOA;AACD,GA7ED;AA8EA;;eAEcP,a","sourcesContent":["/**\n * Internal dependencies\n */\nimport validateNamespace from './validateNamespace.js';\nimport validateHookName from './validateHookName.js';\n\n/**\n * @callback AddHook\n *\n * Adds the hook to the appropriate hooks container.\n *\n * @param {string} hookName Name of hook to add\n * @param {string} namespace The unique namespace identifying the callback in the form `vendor/plugin/function`.\n * @param {import('.').Callback} callback Function to call when the hook is run\n * @param {number} [priority=10] Priority of this hook\n */\n\n/**\n * Returns a function which, when invoked, will add a hook.\n *\n * @param {import('.').Hooks} hooks Hooks instance.\n * @param {import('.').StoreKey} storeKey\n *\n * @return {AddHook} Function that adds a new hook.\n */\nfunction createAddHook( hooks, storeKey ) {\n\treturn function addHook( hookName, namespace, callback, priority = 10 ) {\n\t\tconst hooksStore = hooks[ storeKey ];\n\n\t\tif ( ! validateHookName( hookName ) ) {\n\t\t\treturn;\n\t\t}\n\n\t\tif ( ! validateNamespace( namespace ) ) {\n\t\t\treturn;\n\t\t}\n\n\t\tif ( 'function' !== typeof callback ) {\n\t\t\t// eslint-disable-next-line no-console\n\t\t\tconsole.error( 'The hook callback must be a function.' );\n\t\t\treturn;\n\t\t}\n\n\t\t// Validate numeric priority\n\t\tif ( 'number' !== typeof priority ) {\n\t\t\t// eslint-disable-next-line no-console\n\t\t\tconsole.error(\n\t\t\t\t'If specified, the hook priority must be a number.'\n\t\t\t);\n\t\t\treturn;\n\t\t}\n\n\t\tconst handler = { callback, priority, namespace };\n\n\t\tif ( hooksStore[ hookName ] ) {\n\t\t\t// Find the correct insert index of the new hook.\n\t\t\tconst handlers = hooksStore[ hookName ].handlers;\n\n\t\t\t/** @type {number} */\n\t\t\tlet i;\n\t\t\tfor ( i = handlers.length; i > 0; i-- ) {\n\t\t\t\tif ( priority >= handlers[ i - 1 ].priority ) {\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif ( i === handlers.length ) {\n\t\t\t\t// If append, operate via direct assignment.\n\t\t\t\thandlers[ i ] = handler;\n\t\t\t} else {\n\t\t\t\t// Otherwise, insert before index via splice.\n\t\t\t\thandlers.splice( i, 0, handler );\n\t\t\t}\n\n\t\t\t// We may also be currently executing this hook. If the callback\n\t\t\t// we're adding would come after the current callback, there's no\n\t\t\t// problem; otherwise we need to increase the execution index of\n\t\t\t// any other runs by 1 to account for the added element.\n\t\t\thooksStore.__current.forEach( ( hookInfo ) => {\n\t\t\t\tif (\n\t\t\t\t\thookInfo.name === hookName &&\n\t\t\t\t\thookInfo.currentIndex >= i\n\t\t\t\t) {\n\t\t\t\t\thookInfo.currentIndex++;\n\t\t\t\t}\n\t\t\t} );\n\t\t} else {\n\t\t\t// This is the first hook of its type.\n\t\t\thooksStore[ hookName ] = {\n\t\t\t\thandlers: [ handler ],\n\t\t\t\truns: 0,\n\t\t\t};\n\t\t}\n\n\t\tif ( hookName !== 'hookAdded' ) {\n\t\t\thooks.doAction(\n\t\t\t\t'hookAdded',\n\t\t\t\thookName,\n\t\t\t\tnamespace,\n\t\t\t\tcallback,\n\t\t\t\tpriority\n\t\t\t);\n\t\t}\n\t};\n}\n\nexport default createAddHook;\n"]}
|
|
@@ -39,7 +39,8 @@ var _validateHookName = _interopRequireDefault(require("./validateHookName.js"))
|
|
|
39
39
|
*
|
|
40
40
|
* @return {RemoveHook} Function that removes hooks.
|
|
41
41
|
*/
|
|
42
|
-
function createRemoveHook(hooks, storeKey
|
|
42
|
+
function createRemoveHook(hooks, storeKey) {
|
|
43
|
+
let removeAll = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
|
|
43
44
|
return function removeHook(hookName, namespace) {
|
|
44
45
|
const hooksStore = hooks[storeKey];
|
|
45
46
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/hooks/src/createRemoveHook.js"],"names":["createRemoveHook","hooks","storeKey","removeAll","removeHook","hookName","namespace","hooksStore","handlersRemoved","handlers","length","runs","i","splice","__current","forEach","hookInfo","name","currentIndex","doAction"],"mappings":";;;;;;;;;AAGA;;AACA;;AAJA;AACA;AACA;;AAIA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAASA,gBAAT,CAA2BC,KAA3B,EAAkCC,QAAlC,
|
|
1
|
+
{"version":3,"sources":["@wordpress/hooks/src/createRemoveHook.js"],"names":["createRemoveHook","hooks","storeKey","removeAll","removeHook","hookName","namespace","hooksStore","handlersRemoved","handlers","length","runs","i","splice","__current","forEach","hookInfo","name","currentIndex","doAction"],"mappings":";;;;;;;;;AAGA;;AACA;;AAJA;AACA;AACA;;AAIA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAASA,gBAAT,CAA2BC,KAA3B,EAAkCC,QAAlC,EAAgE;AAAA,MAApBC,SAAoB,uEAAR,KAAQ;AAC/D,SAAO,SAASC,UAAT,CAAqBC,QAArB,EAA+BC,SAA/B,EAA2C;AACjD,UAAMC,UAAU,GAAGN,KAAK,CAAEC,QAAF,CAAxB;;AAEA,QAAK,CAAE,+BAAkBG,QAAlB,CAAP,EAAsC;AACrC;AACA;;AAED,QAAK,CAAEF,SAAF,IAAe,CAAE,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;eAEcR,gB","sourcesContent":["/**\n * Internal dependencies\n */\nimport validateNamespace from './validateNamespace.js';\nimport validateHookName from './validateHookName.js';\n\n/**\n * @callback RemoveHook\n * Removes the specified callback (or all callbacks) from the hook with a given hookName\n * and namespace.\n *\n * @param {string} hookName The name of the hook to modify.\n * @param {string} namespace The unique namespace identifying the callback in the\n * form `vendor/plugin/function`.\n *\n * @return {number | undefined} The number of callbacks removed.\n */\n\n/**\n * Returns a function which, when invoked, will remove a specified hook or all\n * hooks by the given name.\n *\n * @param {import('.').Hooks} hooks Hooks instance.\n * @param {import('.').StoreKey} storeKey\n * @param {boolean} [removeAll=false] Whether to remove all callbacks for a hookName,\n * without regard to namespace. Used to create\n * `removeAll*` functions.\n *\n * @return {RemoveHook} Function that removes hooks.\n */\nfunction createRemoveHook( hooks, storeKey, removeAll = false ) {\n\treturn function removeHook( hookName, namespace ) {\n\t\tconst hooksStore = hooks[ storeKey ];\n\n\t\tif ( ! validateHookName( hookName ) ) {\n\t\t\treturn;\n\t\t}\n\n\t\tif ( ! removeAll && ! validateNamespace( namespace ) ) {\n\t\t\treturn;\n\t\t}\n\n\t\t// Bail if no hooks exist by this name\n\t\tif ( ! hooksStore[ hookName ] ) {\n\t\t\treturn 0;\n\t\t}\n\n\t\tlet handlersRemoved = 0;\n\n\t\tif ( removeAll ) {\n\t\t\thandlersRemoved = hooksStore[ hookName ].handlers.length;\n\t\t\thooksStore[ hookName ] = {\n\t\t\t\truns: hooksStore[ hookName ].runs,\n\t\t\t\thandlers: [],\n\t\t\t};\n\t\t} else {\n\t\t\t// Try to find the specified callback to remove.\n\t\t\tconst handlers = hooksStore[ hookName ].handlers;\n\t\t\tfor ( let i = handlers.length - 1; i >= 0; i-- ) {\n\t\t\t\tif ( handlers[ i ].namespace === namespace ) {\n\t\t\t\t\thandlers.splice( i, 1 );\n\t\t\t\t\thandlersRemoved++;\n\t\t\t\t\t// This callback may also be part of a hook that is\n\t\t\t\t\t// currently executing. If the callback we're removing\n\t\t\t\t\t// comes after the current callback, there's no problem;\n\t\t\t\t\t// otherwise we need to decrease the execution index of any\n\t\t\t\t\t// other runs by 1 to account for the removed element.\n\t\t\t\t\thooksStore.__current.forEach( ( hookInfo ) => {\n\t\t\t\t\t\tif (\n\t\t\t\t\t\t\thookInfo.name === hookName &&\n\t\t\t\t\t\t\thookInfo.currentIndex >= i\n\t\t\t\t\t\t) {\n\t\t\t\t\t\t\thookInfo.currentIndex--;\n\t\t\t\t\t\t}\n\t\t\t\t\t} );\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif ( hookName !== 'hookRemoved' ) {\n\t\t\thooks.doAction( 'hookRemoved', hookName, namespace );\n\t\t}\n\n\t\treturn handlersRemoved;\n\t};\n}\n\nexport default createRemoveHook;\n"]}
|
package/build/createRunHook.js
CHANGED
|
@@ -17,8 +17,9 @@ exports.default = void 0;
|
|
|
17
17
|
*
|
|
18
18
|
* @return {(hookName:string, ...args: unknown[]) => unknown} Function that runs hook callbacks.
|
|
19
19
|
*/
|
|
20
|
-
function createRunHook(hooks, storeKey
|
|
21
|
-
|
|
20
|
+
function createRunHook(hooks, storeKey) {
|
|
21
|
+
let returnFirstArg = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
|
|
22
|
+
return function runHooks(hookName) {
|
|
22
23
|
const hooksStore = hooks[storeKey];
|
|
23
24
|
|
|
24
25
|
if (!hooksStore[hookName]) {
|
|
@@ -38,6 +39,10 @@ function createRunHook(hooks, storeKey, returnFirstArg = false) {
|
|
|
38
39
|
}
|
|
39
40
|
}
|
|
40
41
|
|
|
42
|
+
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
|
43
|
+
args[_key - 1] = arguments[_key];
|
|
44
|
+
}
|
|
45
|
+
|
|
41
46
|
if (!handlers || !handlers.length) {
|
|
42
47
|
return returnFirstArg ? args[0] : undefined;
|
|
43
48
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/hooks/src/createRunHook.js"],"names":["createRunHook","hooks","storeKey","returnFirstArg","runHooks","hookName","
|
|
1
|
+
{"version":3,"sources":["@wordpress/hooks/src/createRunHook.js"],"names":["createRunHook","hooks","storeKey","returnFirstArg","runHooks","hookName","hooksStore","handlers","runs","process","env","NODE_ENV","all","push","args","length","undefined","hookInfo","name","currentIndex","__current","handler","result","callback","apply","pop"],"mappings":";;;;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAASA,aAAT,CAAwBC,KAAxB,EAA+BC,QAA/B,EAAkE;AAAA,MAAzBC,cAAyB,uEAAR,KAAQ;AACjE,SAAO,SAASC,QAAT,CAAmBC,QAAnB,EAAuC;AAC7C,UAAMC,UAAU,GAAGL,KAAK,CAAEC,QAAF,CAAxB;;AAEA,QAAK,CAAEI,UAAU,CAAED,QAAF,CAAjB,EAAgC;AAC/BC,MAAAA,UAAU,CAAED,QAAF,CAAV,GAAyB;AACxBE,QAAAA,QAAQ,EAAE,EADc;AAExBC,QAAAA,IAAI,EAAE;AAFkB,OAAzB;AAIA;;AAEDF,IAAAA,UAAU,CAAED,QAAF,CAAV,CAAuBG,IAAvB;AAEA,UAAMD,QAAQ,GAAGD,UAAU,CAAED,QAAF,CAAV,CAAuBE,QAAxC,CAZ6C,CAc7C;;AACA,QAAK,iBAAiBE,OAAO,CAACC,GAAR,CAAYC,QAAlC,EAA6C;AAC5C;AACA,UAAK,gBAAgBN,QAAhB,IAA4BC,UAAU,CAACM,GAA5C,EAAkD;AACjDL,QAAAA,QAAQ,CAACM,IAAT,CAAe,GAAGP,UAAU,CAACM,GAAX,CAAeL,QAAjC;AACA;AACD;;AApB4C,sCAAPO,IAAO;AAAPA,MAAAA,IAAO;AAAA;;AAsB7C,QAAK,CAAEP,QAAF,IAAc,CAAEA,QAAQ,CAACQ,MAA9B,EAAuC;AACtC,aAAOZ,cAAc,GAAGW,IAAI,CAAE,CAAF,CAAP,GAAeE,SAApC;AACA;;AAED,UAAMC,QAAQ,GAAG;AAChBC,MAAAA,IAAI,EAAEb,QADU;AAEhBc,MAAAA,YAAY,EAAE;AAFE,KAAjB;;AAKAb,IAAAA,UAAU,CAACc,SAAX,CAAqBP,IAArB,CAA2BI,QAA3B;;AAEA,WAAQA,QAAQ,CAACE,YAAT,GAAwBZ,QAAQ,CAACQ,MAAzC,EAAkD;AACjD,YAAMM,OAAO,GAAGd,QAAQ,CAAEU,QAAQ,CAACE,YAAX,CAAxB;AAEA,YAAMG,MAAM,GAAGD,OAAO,CAACE,QAAR,CAAiBC,KAAjB,CAAwB,IAAxB,EAA8BV,IAA9B,CAAf;;AACA,UAAKX,cAAL,EAAsB;AACrBW,QAAAA,IAAI,CAAE,CAAF,CAAJ,GAAYQ,MAAZ;AACA;;AAEDL,MAAAA,QAAQ,CAACE,YAAT;AACA;;AAEDb,IAAAA,UAAU,CAACc,SAAX,CAAqBK,GAArB;;AAEA,QAAKtB,cAAL,EAAsB;AACrB,aAAOW,IAAI,CAAE,CAAF,CAAX;AACA;AACD,GAjDD;AAkDA;;eAEcd,a","sourcesContent":["/**\n * Returns a function which, when invoked, will execute all callbacks\n * registered to a hook of the specified type, optionally returning the final\n * value of the call chain.\n *\n * @param {import('.').Hooks} hooks Hooks instance.\n * @param {import('.').StoreKey} storeKey\n * @param {boolean} [returnFirstArg=false] Whether each hook callback is expected to\n * return its first argument.\n *\n * @return {(hookName:string, ...args: unknown[]) => unknown} Function that runs hook callbacks.\n */\nfunction createRunHook( hooks, storeKey, returnFirstArg = false ) {\n\treturn function runHooks( hookName, ...args ) {\n\t\tconst hooksStore = hooks[ storeKey ];\n\n\t\tif ( ! hooksStore[ hookName ] ) {\n\t\t\thooksStore[ hookName ] = {\n\t\t\t\thandlers: [],\n\t\t\t\truns: 0,\n\t\t\t};\n\t\t}\n\n\t\thooksStore[ hookName ].runs++;\n\n\t\tconst handlers = hooksStore[ hookName ].handlers;\n\n\t\t// The following code is stripped from production builds.\n\t\tif ( 'production' !== process.env.NODE_ENV ) {\n\t\t\t// Handle any 'all' hooks registered.\n\t\t\tif ( 'hookAdded' !== hookName && hooksStore.all ) {\n\t\t\t\thandlers.push( ...hooksStore.all.handlers );\n\t\t\t}\n\t\t}\n\n\t\tif ( ! handlers || ! handlers.length ) {\n\t\t\treturn returnFirstArg ? args[ 0 ] : undefined;\n\t\t}\n\n\t\tconst hookInfo = {\n\t\t\tname: hookName,\n\t\t\tcurrentIndex: 0,\n\t\t};\n\n\t\thooksStore.__current.push( hookInfo );\n\n\t\twhile ( hookInfo.currentIndex < handlers.length ) {\n\t\t\tconst handler = handlers[ hookInfo.currentIndex ];\n\n\t\t\tconst result = handler.callback.apply( null, args );\n\t\t\tif ( returnFirstArg ) {\n\t\t\t\targs[ 0 ] = result;\n\t\t\t}\n\n\t\t\thookInfo.currentIndex++;\n\t\t}\n\n\t\thooksStore.__current.pop();\n\n\t\tif ( returnFirstArg ) {\n\t\t\treturn args[ 0 ];\n\t\t}\n\t};\n}\n\nexport default createRunHook;\n"]}
|
package/build/index.js
CHANGED
|
@@ -5,13 +5,14 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", {
|
|
6
6
|
value: true
|
|
7
7
|
});
|
|
8
|
+
exports.applyFilters = exports.addFilter = exports.addAction = exports.actions = void 0;
|
|
8
9
|
Object.defineProperty(exports, "createHooks", {
|
|
9
10
|
enumerable: true,
|
|
10
11
|
get: function () {
|
|
11
12
|
return _createHooks.default;
|
|
12
13
|
}
|
|
13
14
|
});
|
|
14
|
-
exports.
|
|
15
|
+
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;
|
|
15
16
|
|
|
16
17
|
var _createHooks = _interopRequireDefault(require("./createHooks"));
|
|
17
18
|
|
package/build/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/hooks/src/index.js"],"names":["defaultHooks","addAction","addFilter","removeAction","removeFilter","hasAction","hasFilter","removeAllActions","removeAllFilters","doAction","applyFilters","currentAction","currentFilter","doingAction","doingFilter","didAction","didFilter","actions","filters"],"mappings":"
|
|
1
|
+
{"version":3,"sources":["@wordpress/hooks/src/index.js"],"names":["defaultHooks","addAction","addFilter","removeAction","removeFilter","hasAction","hasFilter","removeAllActions","removeAllFilters","doAction","applyFilters","currentAction","currentFilter","doingAction","doingFilter","didAction","didFilter","actions","filters"],"mappings":";;;;;;;;;;;;;;;;AAGA;;AAHA;AACA;AACA;;AAGA;;AAEA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;AACA;AAEO,MAAMA,YAAY,GAAG,2BAArB;;AAEP,MAAM;AACLC,EAAAA,SADK;AAELC,EAAAA,SAFK;AAGLC,EAAAA,YAHK;AAILC,EAAAA,YAJK;AAKLC,EAAAA,SALK;AAMLC,EAAAA,SANK;AAOLC,EAAAA,gBAPK;AAQLC,EAAAA,gBARK;AASLC,EAAAA,QATK;AAULC,EAAAA,YAVK;AAWLC,EAAAA,aAXK;AAYLC,EAAAA,aAZK;AAaLC,EAAAA,WAbK;AAcLC,EAAAA,WAdK;AAeLC,EAAAA,SAfK;AAgBLC,EAAAA,SAhBK;AAiBLC,EAAAA,OAjBK;AAkBLC,EAAAA;AAlBK,IAmBFlB,YAnBJ","sourcesContent":["/**\n * Internal dependencies\n */\nimport createHooks from './createHooks';\n\n/** @typedef {(...args: any[])=>any} Callback */\n\n/**\n * @typedef Handler\n * @property {Callback} callback The callback\n * @property {string} namespace The namespace\n * @property {number} priority The namespace\n */\n\n/**\n * @typedef Hook\n * @property {Handler[]} handlers Array of handlers\n * @property {number} runs Run counter\n */\n\n/**\n * @typedef Current\n * @property {string} name Hook name\n * @property {number} currentIndex The index\n */\n\n/**\n * @typedef {Record<string, Hook> & {__current: Current[]}} Store\n */\n\n/**\n * @typedef {'actions' | 'filters'} StoreKey\n */\n\n/**\n * @typedef {import('./createHooks').Hooks} Hooks\n */\n\nexport const defaultHooks = createHooks();\n\nconst {\n\taddAction,\n\taddFilter,\n\tremoveAction,\n\tremoveFilter,\n\thasAction,\n\thasFilter,\n\tremoveAllActions,\n\tremoveAllFilters,\n\tdoAction,\n\tapplyFilters,\n\tcurrentAction,\n\tcurrentFilter,\n\tdoingAction,\n\tdoingFilter,\n\tdidAction,\n\tdidFilter,\n\tactions,\n\tfilters,\n} = defaultHooks;\n\nexport {\n\tcreateHooks,\n\taddAction,\n\taddFilter,\n\tremoveAction,\n\tremoveFilter,\n\thasAction,\n\thasFilter,\n\tremoveAllActions,\n\tremoveAllFilters,\n\tdoAction,\n\tapplyFilters,\n\tcurrentAction,\n\tcurrentFilter,\n\tdoingAction,\n\tdoingFilter,\n\tdidAction,\n\tdidFilter,\n\tactions,\n\tfilters,\n};\n"]}
|
|
@@ -24,7 +24,8 @@ import validateHookName from './validateHookName.js';
|
|
|
24
24
|
*/
|
|
25
25
|
|
|
26
26
|
function createAddHook(hooks, storeKey) {
|
|
27
|
-
return function addHook(hookName, namespace, callback
|
|
27
|
+
return function addHook(hookName, namespace, callback) {
|
|
28
|
+
let priority = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 10;
|
|
28
29
|
const hooksStore = hooks[storeKey];
|
|
29
30
|
|
|
30
31
|
if (!validateHookName(hookName)) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/hooks/src/createAddHook.js"],"names":["validateNamespace","validateHookName","createAddHook","hooks","storeKey","addHook","hookName","namespace","callback","priority","hooksStore","console","error","handler","handlers","i","length","splice","__current","forEach","hookInfo","name","currentIndex","runs","doAction"],"mappings":"AAAA;AACA;AACA;AACA,OAAOA,iBAAP,MAA8B,wBAA9B;AACA,OAAOC,gBAAP,MAA6B,uBAA7B;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,SAASC,aAAT,CAAwBC,KAAxB,EAA+BC,QAA/B,EAA0C;AACzC,SAAO,SAASC,OAAT,CAAkBC,QAAlB,EAA4BC,SAA5B,EAAuCC,QAAvC,
|
|
1
|
+
{"version":3,"sources":["@wordpress/hooks/src/createAddHook.js"],"names":["validateNamespace","validateHookName","createAddHook","hooks","storeKey","addHook","hookName","namespace","callback","priority","hooksStore","console","error","handler","handlers","i","length","splice","__current","forEach","hookInfo","name","currentIndex","runs","doAction"],"mappings":"AAAA;AACA;AACA;AACA,OAAOA,iBAAP,MAA8B,wBAA9B;AACA,OAAOC,gBAAP,MAA6B,uBAA7B;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,SAASC,aAAT,CAAwBC,KAAxB,EAA+BC,QAA/B,EAA0C;AACzC,SAAO,SAASC,OAAT,CAAkBC,QAAlB,EAA4BC,SAA5B,EAAuCC,QAAvC,EAAiE;AAAA,QAAhBC,QAAgB,uEAAL,EAAK;AACvE,UAAMC,UAAU,GAAGP,KAAK,CAAEC,QAAF,CAAxB;;AAEA,QAAK,CAAEH,gBAAgB,CAAEK,QAAF,CAAvB,EAAsC;AACrC;AACA;;AAED,QAAK,CAAEN,iBAAiB,CAAEO,SAAF,CAAxB,EAAwC;AACvC;AACA;;AAED,QAAK,eAAe,OAAOC,QAA3B,EAAsC;AACrC;AACAG,MAAAA,OAAO,CAACC,KAAR,CAAe,uCAAf;AACA;AACA,KAfsE,CAiBvE;;;AACA,QAAK,aAAa,OAAOH,QAAzB,EAAoC;AACnC;AACAE,MAAAA,OAAO,CAACC,KAAR,CACC,mDADD;AAGA;AACA;;AAED,UAAMC,OAAO,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A;;AAED,eAAeP,aAAf","sourcesContent":["/**\n * Internal dependencies\n */\nimport validateNamespace from './validateNamespace.js';\nimport validateHookName from './validateHookName.js';\n\n/**\n * @callback AddHook\n *\n * Adds the hook to the appropriate hooks container.\n *\n * @param {string} hookName Name of hook to add\n * @param {string} namespace The unique namespace identifying the callback in the form `vendor/plugin/function`.\n * @param {import('.').Callback} callback Function to call when the hook is run\n * @param {number} [priority=10] Priority of this hook\n */\n\n/**\n * Returns a function which, when invoked, will add a hook.\n *\n * @param {import('.').Hooks} hooks Hooks instance.\n * @param {import('.').StoreKey} storeKey\n *\n * @return {AddHook} Function that adds a new hook.\n */\nfunction createAddHook( hooks, storeKey ) {\n\treturn function addHook( hookName, namespace, callback, priority = 10 ) {\n\t\tconst hooksStore = hooks[ storeKey ];\n\n\t\tif ( ! validateHookName( hookName ) ) {\n\t\t\treturn;\n\t\t}\n\n\t\tif ( ! validateNamespace( namespace ) ) {\n\t\t\treturn;\n\t\t}\n\n\t\tif ( 'function' !== typeof callback ) {\n\t\t\t// eslint-disable-next-line no-console\n\t\t\tconsole.error( 'The hook callback must be a function.' );\n\t\t\treturn;\n\t\t}\n\n\t\t// Validate numeric priority\n\t\tif ( 'number' !== typeof priority ) {\n\t\t\t// eslint-disable-next-line no-console\n\t\t\tconsole.error(\n\t\t\t\t'If specified, the hook priority must be a number.'\n\t\t\t);\n\t\t\treturn;\n\t\t}\n\n\t\tconst handler = { callback, priority, namespace };\n\n\t\tif ( hooksStore[ hookName ] ) {\n\t\t\t// Find the correct insert index of the new hook.\n\t\t\tconst handlers = hooksStore[ hookName ].handlers;\n\n\t\t\t/** @type {number} */\n\t\t\tlet i;\n\t\t\tfor ( i = handlers.length; i > 0; i-- ) {\n\t\t\t\tif ( priority >= handlers[ i - 1 ].priority ) {\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif ( i === handlers.length ) {\n\t\t\t\t// If append, operate via direct assignment.\n\t\t\t\thandlers[ i ] = handler;\n\t\t\t} else {\n\t\t\t\t// Otherwise, insert before index via splice.\n\t\t\t\thandlers.splice( i, 0, handler );\n\t\t\t}\n\n\t\t\t// We may also be currently executing this hook. If the callback\n\t\t\t// we're adding would come after the current callback, there's no\n\t\t\t// problem; otherwise we need to increase the execution index of\n\t\t\t// any other runs by 1 to account for the added element.\n\t\t\thooksStore.__current.forEach( ( hookInfo ) => {\n\t\t\t\tif (\n\t\t\t\t\thookInfo.name === hookName &&\n\t\t\t\t\thookInfo.currentIndex >= i\n\t\t\t\t) {\n\t\t\t\t\thookInfo.currentIndex++;\n\t\t\t\t}\n\t\t\t} );\n\t\t} else {\n\t\t\t// This is the first hook of its type.\n\t\t\thooksStore[ hookName ] = {\n\t\t\t\thandlers: [ handler ],\n\t\t\t\truns: 0,\n\t\t\t};\n\t\t}\n\n\t\tif ( hookName !== 'hookAdded' ) {\n\t\t\thooks.doAction(\n\t\t\t\t'hookAdded',\n\t\t\t\thookName,\n\t\t\t\tnamespace,\n\t\t\t\tcallback,\n\t\t\t\tpriority\n\t\t\t);\n\t\t}\n\t};\n}\n\nexport default createAddHook;\n"]}
|
|
@@ -28,7 +28,8 @@ import validateHookName from './validateHookName.js';
|
|
|
28
28
|
* @return {RemoveHook} Function that removes hooks.
|
|
29
29
|
*/
|
|
30
30
|
|
|
31
|
-
function createRemoveHook(hooks, storeKey
|
|
31
|
+
function createRemoveHook(hooks, storeKey) {
|
|
32
|
+
let removeAll = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
|
|
32
33
|
return function removeHook(hookName, namespace) {
|
|
33
34
|
const hooksStore = hooks[storeKey];
|
|
34
35
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/hooks/src/createRemoveHook.js"],"names":["validateNamespace","validateHookName","createRemoveHook","hooks","storeKey","removeAll","removeHook","hookName","namespace","hooksStore","handlersRemoved","handlers","length","runs","i","splice","__current","forEach","hookInfo","name","currentIndex","doAction"],"mappings":"AAAA;AACA;AACA;AACA,OAAOA,iBAAP,MAA8B,wBAA9B;AACA,OAAOC,gBAAP,MAA6B,uBAA7B;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,SAASC,gBAAT,CAA2BC,KAA3B,EAAkCC,QAAlC,
|
|
1
|
+
{"version":3,"sources":["@wordpress/hooks/src/createRemoveHook.js"],"names":["validateNamespace","validateHookName","createRemoveHook","hooks","storeKey","removeAll","removeHook","hookName","namespace","hooksStore","handlersRemoved","handlers","length","runs","i","splice","__current","forEach","hookInfo","name","currentIndex","doAction"],"mappings":"AAAA;AACA;AACA;AACA,OAAOA,iBAAP,MAA8B,wBAA9B;AACA,OAAOC,gBAAP,MAA6B,uBAA7B;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,SAASC,gBAAT,CAA2BC,KAA3B,EAAkCC,QAAlC,EAAgE;AAAA,MAApBC,SAAoB,uEAAR,KAAQ;AAC/D,SAAO,SAASC,UAAT,CAAqBC,QAArB,EAA+BC,SAA/B,EAA2C;AACjD,UAAMC,UAAU,GAAGN,KAAK,CAAEC,QAAF,CAAxB;;AAEA,QAAK,CAAEH,gBAAgB,CAAEM,QAAF,CAAvB,EAAsC;AACrC;AACA;;AAED,QAAK,CAAEF,SAAF,IAAe,CAAEL,iBAAiB,CAAEQ,SAAF,CAAvC,EAAuD;AACtD;AACA,KATgD,CAWjD;;;AACA,QAAK,CAAEC,UAAU,CAAEF,QAAF,CAAjB,EAAgC;AAC/B,aAAO,CAAP;AACA;;AAED,QAAIG,eAAe,GAAG,CAAtB;;AAEA,QAAKL,SAAL,EAAiB;AAChBK,MAAAA,eAAe,GAAGD,UAAU,CAAEF,QAAF,CAAV,CAAuBI,QAAvB,CAAgCC,MAAlD;AACAH,MAAAA,UAAU,CAAEF,QAAF,CAAV,GAAyB;AACxBM,QAAAA,IAAI,EAAEJ,UAAU,CAAEF,QAAF,CAAV,CAAuBM,IADL;AAExBF,QAAAA,QAAQ,EAAE;AAFc,OAAzB;AAIA,KAND,MAMO;AACN;AACA,YAAMA,QAAQ,GAAGF,UAAU,CAAEF,QAAF,CAAV,CAAuBI,QAAxC;;AACA,WAAM,IAAIG,CAAC,GAAGH,QAAQ,CAACC,MAAT,GAAkB,CAAhC,EAAmCE,CAAC,IAAI,CAAxC,EAA2CA,CAAC,EAA5C,EAAiD;AAChD,YAAKH,QAAQ,CAAEG,CAAF,CAAR,CAAcN,SAAd,KAA4BA,SAAjC,EAA6C;AAC5CG,UAAAA,QAAQ,CAACI,MAAT,CAAiBD,CAAjB,EAAoB,CAApB;AACAJ,UAAAA,eAAe,GAF6B,CAG5C;AACA;AACA;AACA;AACA;;AACAD,UAAAA,UAAU,CAACO,SAAX,CAAqBC,OAArB,CAAgCC,QAAF,IAAgB;AAC7C,gBACCA,QAAQ,CAACC,IAAT,KAAkBZ,QAAlB,IACAW,QAAQ,CAACE,YAAT,IAAyBN,CAF1B,EAGE;AACDI,cAAAA,QAAQ,CAACE,YAAT;AACA;AACD,WAPD;AAQA;AACD;AACD;;AAED,QAAKb,QAAQ,KAAK,aAAlB,EAAkC;AACjCJ,MAAAA,KAAK,CAACkB,QAAN,CAAgB,aAAhB,EAA+Bd,QAA/B,EAAyCC,SAAzC;AACA;;AAED,WAAOE,eAAP;AACA,GArDD;AAsDA;;AAED,eAAeR,gBAAf","sourcesContent":["/**\n * Internal dependencies\n */\nimport validateNamespace from './validateNamespace.js';\nimport validateHookName from './validateHookName.js';\n\n/**\n * @callback RemoveHook\n * Removes the specified callback (or all callbacks) from the hook with a given hookName\n * and namespace.\n *\n * @param {string} hookName The name of the hook to modify.\n * @param {string} namespace The unique namespace identifying the callback in the\n * form `vendor/plugin/function`.\n *\n * @return {number | undefined} The number of callbacks removed.\n */\n\n/**\n * Returns a function which, when invoked, will remove a specified hook or all\n * hooks by the given name.\n *\n * @param {import('.').Hooks} hooks Hooks instance.\n * @param {import('.').StoreKey} storeKey\n * @param {boolean} [removeAll=false] Whether to remove all callbacks for a hookName,\n * without regard to namespace. Used to create\n * `removeAll*` functions.\n *\n * @return {RemoveHook} Function that removes hooks.\n */\nfunction createRemoveHook( hooks, storeKey, removeAll = false ) {\n\treturn function removeHook( hookName, namespace ) {\n\t\tconst hooksStore = hooks[ storeKey ];\n\n\t\tif ( ! validateHookName( hookName ) ) {\n\t\t\treturn;\n\t\t}\n\n\t\tif ( ! removeAll && ! validateNamespace( namespace ) ) {\n\t\t\treturn;\n\t\t}\n\n\t\t// Bail if no hooks exist by this name\n\t\tif ( ! hooksStore[ hookName ] ) {\n\t\t\treturn 0;\n\t\t}\n\n\t\tlet handlersRemoved = 0;\n\n\t\tif ( removeAll ) {\n\t\t\thandlersRemoved = hooksStore[ hookName ].handlers.length;\n\t\t\thooksStore[ hookName ] = {\n\t\t\t\truns: hooksStore[ hookName ].runs,\n\t\t\t\thandlers: [],\n\t\t\t};\n\t\t} else {\n\t\t\t// Try to find the specified callback to remove.\n\t\t\tconst handlers = hooksStore[ hookName ].handlers;\n\t\t\tfor ( let i = handlers.length - 1; i >= 0; i-- ) {\n\t\t\t\tif ( handlers[ i ].namespace === namespace ) {\n\t\t\t\t\thandlers.splice( i, 1 );\n\t\t\t\t\thandlersRemoved++;\n\t\t\t\t\t// This callback may also be part of a hook that is\n\t\t\t\t\t// currently executing. If the callback we're removing\n\t\t\t\t\t// comes after the current callback, there's no problem;\n\t\t\t\t\t// otherwise we need to decrease the execution index of any\n\t\t\t\t\t// other runs by 1 to account for the removed element.\n\t\t\t\t\thooksStore.__current.forEach( ( hookInfo ) => {\n\t\t\t\t\t\tif (\n\t\t\t\t\t\t\thookInfo.name === hookName &&\n\t\t\t\t\t\t\thookInfo.currentIndex >= i\n\t\t\t\t\t\t) {\n\t\t\t\t\t\t\thookInfo.currentIndex--;\n\t\t\t\t\t\t}\n\t\t\t\t\t} );\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif ( hookName !== 'hookRemoved' ) {\n\t\t\thooks.doAction( 'hookRemoved', hookName, namespace );\n\t\t}\n\n\t\treturn handlersRemoved;\n\t};\n}\n\nexport default createRemoveHook;\n"]}
|
|
@@ -10,8 +10,9 @@
|
|
|
10
10
|
*
|
|
11
11
|
* @return {(hookName:string, ...args: unknown[]) => unknown} Function that runs hook callbacks.
|
|
12
12
|
*/
|
|
13
|
-
function createRunHook(hooks, storeKey
|
|
14
|
-
|
|
13
|
+
function createRunHook(hooks, storeKey) {
|
|
14
|
+
let returnFirstArg = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
|
|
15
|
+
return function runHooks(hookName) {
|
|
15
16
|
const hooksStore = hooks[storeKey];
|
|
16
17
|
|
|
17
18
|
if (!hooksStore[hookName]) {
|
|
@@ -31,6 +32,10 @@ function createRunHook(hooks, storeKey, returnFirstArg = false) {
|
|
|
31
32
|
}
|
|
32
33
|
}
|
|
33
34
|
|
|
35
|
+
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
|
36
|
+
args[_key - 1] = arguments[_key];
|
|
37
|
+
}
|
|
38
|
+
|
|
34
39
|
if (!handlers || !handlers.length) {
|
|
35
40
|
return returnFirstArg ? args[0] : undefined;
|
|
36
41
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/hooks/src/createRunHook.js"],"names":["createRunHook","hooks","storeKey","returnFirstArg","runHooks","hookName","
|
|
1
|
+
{"version":3,"sources":["@wordpress/hooks/src/createRunHook.js"],"names":["createRunHook","hooks","storeKey","returnFirstArg","runHooks","hookName","hooksStore","handlers","runs","process","env","NODE_ENV","all","push","args","length","undefined","hookInfo","name","currentIndex","__current","handler","result","callback","apply","pop"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA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B;AAEA,YAAMG,MAAM,GAAGD,OAAO,CAACE,QAAR,CAAiBC,KAAjB,CAAwB,IAAxB,EAA8BV,IAA9B,CAAf;;AACA,UAAKX,cAAL,EAAsB;AACrBW,QAAAA,IAAI,CAAE,CAAF,CAAJ,GAAYQ,MAAZ;AACA;;AAEDL,MAAAA,QAAQ,CAACE,YAAT;AACA;;AAEDb,IAAAA,UAAU,CAACc,SAAX,CAAqBK,GAArB;;AAEA,QAAKtB,cAAL,EAAsB;AACrB,aAAOW,IAAI,CAAE,CAAF,CAAX;AACA;AACD,GAjDD;AAkDA;;AAED,eAAed,aAAf","sourcesContent":["/**\n * Returns a function which, when invoked, will execute all callbacks\n * registered to a hook of the specified type, optionally returning the final\n * value of the call chain.\n *\n * @param {import('.').Hooks} hooks Hooks instance.\n * @param {import('.').StoreKey} storeKey\n * @param {boolean} [returnFirstArg=false] Whether each hook callback is expected to\n * return its first argument.\n *\n * @return {(hookName:string, ...args: unknown[]) => unknown} Function that runs hook callbacks.\n */\nfunction createRunHook( hooks, storeKey, returnFirstArg = false ) {\n\treturn function runHooks( hookName, ...args ) {\n\t\tconst hooksStore = hooks[ storeKey ];\n\n\t\tif ( ! hooksStore[ hookName ] ) {\n\t\t\thooksStore[ hookName ] = {\n\t\t\t\thandlers: [],\n\t\t\t\truns: 0,\n\t\t\t};\n\t\t}\n\n\t\thooksStore[ hookName ].runs++;\n\n\t\tconst handlers = hooksStore[ hookName ].handlers;\n\n\t\t// The following code is stripped from production builds.\n\t\tif ( 'production' !== process.env.NODE_ENV ) {\n\t\t\t// Handle any 'all' hooks registered.\n\t\t\tif ( 'hookAdded' !== hookName && hooksStore.all ) {\n\t\t\t\thandlers.push( ...hooksStore.all.handlers );\n\t\t\t}\n\t\t}\n\n\t\tif ( ! handlers || ! handlers.length ) {\n\t\t\treturn returnFirstArg ? args[ 0 ] : undefined;\n\t\t}\n\n\t\tconst hookInfo = {\n\t\t\tname: hookName,\n\t\t\tcurrentIndex: 0,\n\t\t};\n\n\t\thooksStore.__current.push( hookInfo );\n\n\t\twhile ( hookInfo.currentIndex < handlers.length ) {\n\t\t\tconst handler = handlers[ hookInfo.currentIndex ];\n\n\t\t\tconst result = handler.callback.apply( null, args );\n\t\t\tif ( returnFirstArg ) {\n\t\t\t\targs[ 0 ] = result;\n\t\t\t}\n\n\t\t\thookInfo.currentIndex++;\n\t\t}\n\n\t\thooksStore.__current.pop();\n\n\t\tif ( returnFirstArg ) {\n\t\t\treturn args[ 0 ];\n\t\t}\n\t};\n}\n\nexport default createRunHook;\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wordpress/hooks",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.3.1",
|
|
4
4
|
"description": "WordPress hooks library.",
|
|
5
5
|
"author": "The WordPress Contributors",
|
|
6
6
|
"license": "GPL-2.0-or-later",
|
|
@@ -26,10 +26,10 @@
|
|
|
26
26
|
"react-native": "src/index",
|
|
27
27
|
"types": "build-types",
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@babel/runtime": "^7.
|
|
29
|
+
"@babel/runtime": "^7.16.0"
|
|
30
30
|
},
|
|
31
31
|
"publishConfig": {
|
|
32
32
|
"access": "public"
|
|
33
33
|
},
|
|
34
|
-
"gitHead": "
|
|
34
|
+
"gitHead": "2e4922861e49f5a090f9dc52056165092cfba163"
|
|
35
35
|
}
|