@wordpress/hooks 3.2.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.
Files changed (85) hide show
  1. package/CHANGELOG.md +72 -0
  2. package/LICENSE.md +788 -0
  3. package/README.md +66 -0
  4. package/benchmark/index.js +56 -0
  5. package/build/createAddHook.js +114 -0
  6. package/build/createAddHook.js.map +1 -0
  7. package/build/createCurrentHook.js +29 -0
  8. package/build/createCurrentHook.js.map +1 -0
  9. package/build/createDidHook.js +49 -0
  10. package/build/createDidHook.js.map +1 -0
  11. package/build/createDoingHook.js +43 -0
  12. package/build/createDoingHook.js.map +1 -0
  13. package/build/createHasHook.js +44 -0
  14. package/build/createHasHook.js.map +1 -0
  15. package/build/createHooks.js +80 -0
  16. package/build/createHooks.js.map +1 -0
  17. package/build/createRemoveHook.js +99 -0
  18. package/build/createRemoveHook.js.map +1 -0
  19. package/build/createRunHook.js +73 -0
  20. package/build/createRunHook.js.map +1 -0
  21. package/build/index.js +94 -0
  22. package/build/index.js.map +1 -0
  23. package/build/validateHookName.js +41 -0
  24. package/build/validateHookName.js.map +1 -0
  25. package/build/validateNamespace.js +34 -0
  26. package/build/validateNamespace.js.map +1 -0
  27. package/build-module/createAddHook.js +102 -0
  28. package/build-module/createAddHook.js.map +1 -0
  29. package/build-module/createCurrentHook.js +21 -0
  30. package/build-module/createCurrentHook.js.map +1 -0
  31. package/build-module/createDidHook.js +38 -0
  32. package/build-module/createDidHook.js.map +1 -0
  33. package/build-module/createDoingHook.js +35 -0
  34. package/build-module/createDoingHook.js.map +1 -0
  35. package/build-module/createHasHook.js +36 -0
  36. package/build-module/createHasHook.js.map +1 -0
  37. package/build-module/createHooks.js +60 -0
  38. package/build-module/createHooks.js.map +1 -0
  39. package/build-module/createRemoveHook.js +87 -0
  40. package/build-module/createRemoveHook.js.map +1 -0
  41. package/build-module/createRunHook.js +65 -0
  42. package/build-module/createRunHook.js.map +1 -0
  43. package/build-module/index.js +60 -0
  44. package/build-module/index.js.map +1 -0
  45. package/build-module/validateHookName.js +33 -0
  46. package/build-module/validateHookName.js.map +1 -0
  47. package/build-module/validateNamespace.js +26 -0
  48. package/build-module/validateNamespace.js.map +1 -0
  49. package/build-types/createAddHook.d.ts +25 -0
  50. package/build-types/createAddHook.d.ts.map +1 -0
  51. package/build-types/createCurrentHook.d.ts +13 -0
  52. package/build-types/createCurrentHook.d.ts.map +1 -0
  53. package/build-types/createDidHook.d.ts +25 -0
  54. package/build-types/createDidHook.d.ts.map +1 -0
  55. package/build-types/createDoingHook.d.ts +26 -0
  56. package/build-types/createDoingHook.d.ts.map +1 -0
  57. package/build-types/createHasHook.d.ts +28 -0
  58. package/build-types/createHasHook.d.ts.map +1 -0
  59. package/build-types/createHooks.d.ts +39 -0
  60. package/build-types/createHooks.d.ts.map +1 -0
  61. package/build-types/createRemoveHook.d.ts +31 -0
  62. package/build-types/createRemoveHook.d.ts.map +1 -0
  63. package/build-types/createRunHook.d.ts +15 -0
  64. package/build-types/createRunHook.d.ts.map +1 -0
  65. package/build-types/index.d.ts +88 -0
  66. package/build-types/index.d.ts.map +1 -0
  67. package/build-types/validateHookName.d.ts +12 -0
  68. package/build-types/validateHookName.d.ts.map +1 -0
  69. package/build-types/validateNamespace.d.ts +11 -0
  70. package/build-types/validateNamespace.d.ts.map +1 -0
  71. package/package.json +35 -0
  72. package/src/createAddHook.js +107 -0
  73. package/src/createCurrentHook.js +22 -0
  74. package/src/createDidHook.js +39 -0
  75. package/src/createDoingHook.js +37 -0
  76. package/src/createHasHook.js +40 -0
  77. package/src/createHooks.js +59 -0
  78. package/src/createRemoveHook.js +88 -0
  79. package/src/createRunHook.js +66 -0
  80. package/src/index.js +82 -0
  81. package/src/test/index.test.js +945 -0
  82. package/src/validateHookName.js +34 -0
  83. package/src/validateNamespace.js +27 -0
  84. package/tsconfig.json +9 -0
  85. package/tsconfig.tsbuildinfo +1 -0
package/README.md ADDED
@@ -0,0 +1,66 @@
1
+ # Hooks
2
+
3
+ A lightweight & efficient EventManager for JavaScript.
4
+
5
+ ## Installation
6
+
7
+ Install the module
8
+
9
+ ```bash
10
+ npm install @wordpress/hooks --save
11
+ ```
12
+
13
+ _This package assumes that your code will run in an **ES2015+** environment. If you're using an environment that has limited or no support for such language features and APIs, you should include [the polyfill shipped in `@wordpress/babel-preset-default`](https://github.com/WordPress/gutenberg/tree/HEAD/packages/babel-preset-default#polyfill) in your code._
14
+
15
+ ### Usage
16
+
17
+ In your JavaScript project, use hooks as follows:
18
+
19
+ ```javascript
20
+ import { createHooks } from '@wordpress/hooks';
21
+
22
+ myObject.hooks = createHooks();
23
+ myObject.hooks.addAction(); //etc...
24
+ ```
25
+
26
+ #### The global instance
27
+
28
+ In the above example, we are creating a custom instance of the `Hooks` object and registering hooks there. The package also creates a default global instance that's accessible through the `defaultHooks` named exports, and its methods are also separately exported one-by-one.
29
+
30
+ In the WordPress context, that enables API functions to be called via the global `wp.hooks` object, like `wp.hooks.addAction()`, etc.
31
+
32
+ One notable difference between the JS and PHP hooks API is that in the JS version, `addAction()` and `addFilter()` also need to include a namespace as the second argument. Namespace uniquely identifies a callback in the form `vendor/plugin/function`.
33
+
34
+ ### API Usage
35
+
36
+ - `createHooks()`
37
+ - `addAction( 'hookName', 'namespace', callback, priority )`
38
+ - `addFilter( 'hookName', 'namespace', callback, priority )`
39
+ - `removeAction( 'hookName', 'namespace' )`
40
+ - `removeFilter( 'hookName', 'namespace' )`
41
+ - `removeAllActions( 'hookName' )`
42
+ - `removeAllFilters( 'hookName' )`
43
+ - `doAction( 'hookName', arg1, arg2, moreArgs, finalArg )`
44
+ - `applyFilters( 'hookName', content, arg1, arg2, moreArgs, finalArg )`
45
+ - `doingAction( 'hookName' )`
46
+ - `doingFilter( 'hookName' )`
47
+ - `didAction( 'hookName' )`
48
+ - `didFilter( 'hookName' )`
49
+ - `hasAction( 'hookName', 'namespace' )`
50
+ - `hasFilter( 'hookName', 'namespace' )`
51
+ - `actions`
52
+ - `filters`
53
+ - `defaultHooks`
54
+
55
+ ### Events on action/filter add or remove
56
+
57
+ Whenever an action or filter is added or removed, a matching `hookAdded` or `hookRemoved` action is triggered.
58
+
59
+ - `hookAdded` action is triggered when `addFilter()` or `addAction()` method is called, passing values for `hookName`, `functionName`, `callback` and `priority`.
60
+ - `hookRemoved` action is triggered when `removeFilter()` or `removeAction()` method is called, passing values for `hookName` and `functionName`.
61
+
62
+ ### The `all` hook
63
+
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
+
66
+ <br/><br/><p align="center"><img src="https://s.w.org/style/images/codeispoetry.png?1" alt="Code is Poetry." /></p>
@@ -0,0 +1,56 @@
1
+ /**
2
+ * External dependencies
3
+ */
4
+ const Benchmark = require( 'benchmark' );
5
+
6
+ /**
7
+ * Internal dependencies
8
+ */
9
+ const hooks = require( '../' );
10
+
11
+ const suite = new Benchmark.Suite();
12
+
13
+ const filter = process.argv[ 2 ];
14
+ const isInFilter = ( key ) => ! filter || filter === key;
15
+
16
+ function reset() {
17
+ hooks.removeAllFilters( 'example' );
18
+ hooks.removeAllActions( 'example' );
19
+ }
20
+
21
+ if ( isInFilter( 'applyFilters' ) ) {
22
+ function myCallback() {}
23
+
24
+ hooks.addFilter( 'example', 'myCallback', myCallback );
25
+
26
+ suite
27
+ .add( 'applyFilters - handled', () => {
28
+ hooks.applyFilters( 'handled' );
29
+ } )
30
+ .add( 'applyFilters - unhandled', () => {
31
+ hooks.applyFilters( 'unhandled' );
32
+ } );
33
+ }
34
+
35
+ if ( isInFilter( 'addFilter' ) ) {
36
+ let hasSetHighPriority = false;
37
+
38
+ suite
39
+ .add( 'addFilter - append last', () => {
40
+ hooks.addFilter( 'example', 'myCallback', () => {} );
41
+ } )
42
+ .add( 'addFilter - default before higher priority', () => {
43
+ if ( ! hasSetHighPriority ) {
44
+ hasSetHighPriority = true;
45
+ hooks.addFilter( 'example', 'priority', () => {}, 20 );
46
+ }
47
+
48
+ hooks.addFilter( 'example', 'myCallback', () => {} );
49
+ } );
50
+ }
51
+
52
+ suite
53
+ .on( 'cycle', reset )
54
+ // eslint-disable-next-line no-console
55
+ .on( 'cycle', ( event ) => console.log( event.target.toString() ) )
56
+ .run( { async: true } );
@@ -0,0 +1,114 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.default = void 0;
9
+
10
+ var _validateNamespace = _interopRequireDefault(require("./validateNamespace.js"));
11
+
12
+ var _validateHookName = _interopRequireDefault(require("./validateHookName.js"));
13
+
14
+ /**
15
+ * Internal dependencies
16
+ */
17
+
18
+ /**
19
+ * @callback AddHook
20
+ *
21
+ * Adds the hook to the appropriate hooks container.
22
+ *
23
+ * @param {string} hookName Name of hook to add
24
+ * @param {string} namespace The unique namespace identifying the callback in the form `vendor/plugin/function`.
25
+ * @param {import('.').Callback} callback Function to call when the hook is run
26
+ * @param {number} [priority=10] Priority of this hook
27
+ */
28
+
29
+ /**
30
+ * Returns a function which, when invoked, will add a hook.
31
+ *
32
+ * @param {import('.').Hooks} hooks Hooks instance.
33
+ * @param {import('.').StoreKey} storeKey
34
+ *
35
+ * @return {AddHook} Function that adds a new hook.
36
+ */
37
+ function createAddHook(hooks, storeKey) {
38
+ return function addHook(hookName, namespace, callback, priority = 10) {
39
+ const hooksStore = hooks[storeKey];
40
+
41
+ if (!(0, _validateHookName.default)(hookName)) {
42
+ return;
43
+ }
44
+
45
+ if (!(0, _validateNamespace.default)(namespace)) {
46
+ return;
47
+ }
48
+
49
+ if ('function' !== typeof callback) {
50
+ // eslint-disable-next-line no-console
51
+ console.error('The hook callback must be a function.');
52
+ return;
53
+ } // Validate numeric priority
54
+
55
+
56
+ if ('number' !== typeof priority) {
57
+ // eslint-disable-next-line no-console
58
+ console.error('If specified, the hook priority must be a number.');
59
+ return;
60
+ }
61
+
62
+ const handler = {
63
+ callback,
64
+ priority,
65
+ namespace
66
+ };
67
+
68
+ if (hooksStore[hookName]) {
69
+ // Find the correct insert index of the new hook.
70
+ const handlers = hooksStore[hookName].handlers;
71
+ /** @type {number} */
72
+
73
+ let i;
74
+
75
+ for (i = handlers.length; i > 0; i--) {
76
+ if (priority >= handlers[i - 1].priority) {
77
+ break;
78
+ }
79
+ }
80
+
81
+ if (i === handlers.length) {
82
+ // If append, operate via direct assignment.
83
+ handlers[i] = handler;
84
+ } else {
85
+ // Otherwise, insert before index via splice.
86
+ handlers.splice(i, 0, handler);
87
+ } // We may also be currently executing this hook. If the callback
88
+ // we're adding would come after the current callback, there's no
89
+ // problem; otherwise we need to increase the execution index of
90
+ // any other runs by 1 to account for the added element.
91
+
92
+
93
+ hooksStore.__current.forEach(hookInfo => {
94
+ if (hookInfo.name === hookName && hookInfo.currentIndex >= i) {
95
+ hookInfo.currentIndex++;
96
+ }
97
+ });
98
+ } else {
99
+ // This is the first hook of its type.
100
+ hooksStore[hookName] = {
101
+ handlers: [handler],
102
+ runs: 0
103
+ };
104
+ }
105
+
106
+ if (hookName !== 'hookAdded') {
107
+ hooks.doAction('hookAdded', hookName, namespace, callback, priority);
108
+ }
109
+ };
110
+ }
111
+
112
+ var _default = createAddHook;
113
+ exports.default = _default;
114
+ //# sourceMappingURL=createAddHook.js.map
@@ -0,0 +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,EAAiDC,QAAQ,GAAG,EAA5D,EAAiE;AACvE,UAAMC,UAAU,GAAGP,KAAK,CAAEC,QAAF,CAAxB;;AAEA,QAAK,CAAE,+BAAkBE,QAAlB,CAAP,EAAsC;AACrC;AACA;;AAED,QAAK,CAAE,gCAAmBC,SAAnB,CAAP,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,GAAG;AAAEL,MAAAA,QAAF;AAAYC,MAAAA,QAAZ;AAAsBF,MAAAA;AAAtB,KAAhB;;AAEA,QAAKG,UAAU,CAAEJ,QAAF,CAAf,EAA8B;AAC7B;AACA,YAAMQ,QAAQ,GAAGJ,UAAU,CAAEJ,QAAF,CAAV,CAAuBQ,QAAxC;AAEA;;AACA,UAAIC,CAAJ;;AACA,WAAMA,CAAC,GAAGD,QAAQ,CAACE,MAAnB,EAA2BD,CAAC,GAAG,CAA/B,EAAkCA,CAAC,EAAnC,EAAwC;AACvC,YAAKN,QAAQ,IAAIK,QAAQ,CAAEC,CAAC,GAAG,CAAN,CAAR,CAAkBN,QAAnC,EAA8C;AAC7C;AACA;AACD;;AAED,UAAKM,CAAC,KAAKD,QAAQ,CAACE,MAApB,EAA6B;AAC5B;AACAF,QAAAA,QAAQ,CAAEC,CAAF,CAAR,GAAgBF,OAAhB;AACA,OAHD,MAGO;AACN;AACAC,QAAAA,QAAQ,CAACG,MAAT,CAAiBF,CAAjB,EAAoB,CAApB,EAAuBF,OAAvB;AACA,OAlB4B,CAoB7B;AACA;AACA;AACA;;;AACAH,MAAAA,UAAU,CAACQ,SAAX,CAAqBC,OAArB,CAAgCC,QAAF,IAAgB;AAC7C,YACCA,QAAQ,CAACC,IAAT,KAAkBf,QAAlB,IACAc,QAAQ,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"]}
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ /**
9
+ * Returns a function which, when invoked, will return the name of the
10
+ * currently running hook, or `null` if no hook of the given type is currently
11
+ * running.
12
+ *
13
+ * @param {import('.').Hooks} hooks Hooks instance.
14
+ * @param {import('.').StoreKey} storeKey
15
+ *
16
+ * @return {() => string | null} Function that returns the current hook name or null.
17
+ */
18
+ function createCurrentHook(hooks, storeKey) {
19
+ return function currentHook() {
20
+ var _hooksStore$__current, _hooksStore$__current2;
21
+
22
+ const hooksStore = hooks[storeKey];
23
+ return (_hooksStore$__current = (_hooksStore$__current2 = hooksStore.__current[hooksStore.__current.length - 1]) === null || _hooksStore$__current2 === void 0 ? void 0 : _hooksStore$__current2.name) !== null && _hooksStore$__current !== void 0 ? _hooksStore$__current : null;
24
+ };
25
+ }
26
+
27
+ var _default = createCurrentHook;
28
+ exports.default = _default;
29
+ //# sourceMappingURL=createCurrentHook.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["@wordpress/hooks/src/createCurrentHook.js"],"names":["createCurrentHook","hooks","storeKey","currentHook","hooksStore","__current","length","name"],"mappings":";;;;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAASA,iBAAT,CAA4BC,KAA5B,EAAmCC,QAAnC,EAA8C;AAC7C,SAAO,SAASC,WAAT,GAAuB;AAAA;;AAC7B,UAAMC,UAAU,GAAGH,KAAK,CAAEC,QAAF,CAAxB;AAEA,8DACCE,UAAU,CAACC,SAAX,CAAsBD,UAAU,CAACC,SAAX,CAAqBC,MAArB,GAA8B,CAApD,CADD,2DACC,uBAAyDC,IAD1D,yEAEC,IAFD;AAIA,GAPD;AAQA;;eAEcP,iB","sourcesContent":["/**\n * Returns a function which, when invoked, will return the name of the\n * currently running hook, or `null` if no hook of the given type is currently\n * running.\n *\n * @param {import('.').Hooks} hooks Hooks instance.\n * @param {import('.').StoreKey} storeKey\n *\n * @return {() => string | null} Function that returns the current hook name or null.\n */\nfunction createCurrentHook( hooks, storeKey ) {\n\treturn function currentHook() {\n\t\tconst hooksStore = hooks[ storeKey ];\n\n\t\treturn (\n\t\t\thooksStore.__current[ hooksStore.__current.length - 1 ]?.name ??\n\t\t\tnull\n\t\t);\n\t};\n}\n\nexport default createCurrentHook;\n"]}
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.default = void 0;
9
+
10
+ var _validateHookName = _interopRequireDefault(require("./validateHookName.js"));
11
+
12
+ /**
13
+ * Internal dependencies
14
+ */
15
+
16
+ /**
17
+ * @callback DidHook
18
+ *
19
+ * Returns the number of times an action has been fired.
20
+ *
21
+ * @param {string} hookName The hook name to check.
22
+ *
23
+ * @return {number | undefined} The number of times the hook has run.
24
+ */
25
+
26
+ /**
27
+ * Returns a function which, when invoked, will return the number of times a
28
+ * hook has been called.
29
+ *
30
+ * @param {import('.').Hooks} hooks Hooks instance.
31
+ * @param {import('.').StoreKey} storeKey
32
+ *
33
+ * @return {DidHook} Function that returns a hook's call count.
34
+ */
35
+ function createDidHook(hooks, storeKey) {
36
+ return function didHook(hookName) {
37
+ const hooksStore = hooks[storeKey];
38
+
39
+ if (!(0, _validateHookName.default)(hookName)) {
40
+ return;
41
+ }
42
+
43
+ return hooksStore[hookName] && hooksStore[hookName].runs ? hooksStore[hookName].runs : 0;
44
+ };
45
+ }
46
+
47
+ var _default = createDidHook;
48
+ exports.default = _default;
49
+ //# sourceMappingURL=createDidHook.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["@wordpress/hooks/src/createDidHook.js"],"names":["createDidHook","hooks","storeKey","didHook","hookName","hooksStore","runs"],"mappings":";;;;;;;;;AAGA;;AAHA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAASA,aAAT,CAAwBC,KAAxB,EAA+BC,QAA/B,EAA0C;AACzC,SAAO,SAASC,OAAT,CAAkBC,QAAlB,EAA6B;AACnC,UAAMC,UAAU,GAAGJ,KAAK,CAAEC,QAAF,CAAxB;;AAEA,QAAK,CAAE,+BAAkBE,QAAlB,CAAP,EAAsC;AACrC;AACA;;AAED,WAAOC,UAAU,CAAED,QAAF,CAAV,IAA0BC,UAAU,CAAED,QAAF,CAAV,CAAuBE,IAAjD,GACJD,UAAU,CAAED,QAAF,CAAV,CAAuBE,IADnB,GAEJ,CAFH;AAGA,GAVD;AAWA;;eAEcN,a","sourcesContent":["/**\n * Internal dependencies\n */\nimport validateHookName from './validateHookName.js';\n\n/**\n * @callback DidHook\n *\n * Returns the number of times an action has been fired.\n *\n * @param {string} hookName The hook name to check.\n *\n * @return {number | undefined} The number of times the hook has run.\n */\n\n/**\n * Returns a function which, when invoked, will return the number of times a\n * hook has been called.\n *\n * @param {import('.').Hooks} hooks Hooks instance.\n * @param {import('.').StoreKey} storeKey\n *\n * @return {DidHook} Function that returns a hook's call count.\n */\nfunction createDidHook( hooks, storeKey ) {\n\treturn function didHook( hookName ) {\n\t\tconst hooksStore = hooks[ storeKey ];\n\n\t\tif ( ! validateHookName( hookName ) ) {\n\t\t\treturn;\n\t\t}\n\n\t\treturn hooksStore[ hookName ] && hooksStore[ hookName ].runs\n\t\t\t? hooksStore[ hookName ].runs\n\t\t\t: 0;\n\t};\n}\n\nexport default createDidHook;\n"]}
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ /**
9
+ * @callback DoingHook
10
+ * Returns whether a hook is currently being executed.
11
+ *
12
+ * @param {string} [hookName] The name of the hook to check for. If
13
+ * omitted, will check for any hook being executed.
14
+ *
15
+ * @return {boolean} Whether the hook is being executed.
16
+ */
17
+
18
+ /**
19
+ * Returns a function which, when invoked, will return whether a hook is
20
+ * currently being executed.
21
+ *
22
+ * @param {import('.').Hooks} hooks Hooks instance.
23
+ * @param {import('.').StoreKey} storeKey
24
+ *
25
+ * @return {DoingHook} Function that returns whether a hook is currently
26
+ * being executed.
27
+ */
28
+ function createDoingHook(hooks, storeKey) {
29
+ return function doingHook(hookName) {
30
+ const hooksStore = hooks[storeKey]; // If the hookName was not passed, check for any current hook.
31
+
32
+ if ('undefined' === typeof hookName) {
33
+ return 'undefined' !== typeof hooksStore.__current[0];
34
+ } // Return the __current hook.
35
+
36
+
37
+ return hooksStore.__current[0] ? hookName === hooksStore.__current[0].name : false;
38
+ };
39
+ }
40
+
41
+ var _default = createDoingHook;
42
+ exports.default = _default;
43
+ //# sourceMappingURL=createDoingHook.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["@wordpress/hooks/src/createDoingHook.js"],"names":["createDoingHook","hooks","storeKey","doingHook","hookName","hooksStore","__current","name"],"mappings":";;;;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAASA,eAAT,CAA0BC,KAA1B,EAAiCC,QAAjC,EAA4C;AAC3C,SAAO,SAASC,SAAT,CAAoBC,QAApB,EAA+B;AACrC,UAAMC,UAAU,GAAGJ,KAAK,CAAEC,QAAF,CAAxB,CADqC,CAGrC;;AACA,QAAK,gBAAgB,OAAOE,QAA5B,EAAuC;AACtC,aAAO,gBAAgB,OAAOC,UAAU,CAACC,SAAX,CAAsB,CAAtB,CAA9B;AACA,KANoC,CAQrC;;;AACA,WAAOD,UAAU,CAACC,SAAX,CAAsB,CAAtB,IACJF,QAAQ,KAAKC,UAAU,CAACC,SAAX,CAAsB,CAAtB,EAA0BC,IADnC,GAEJ,KAFH;AAGA,GAZD;AAaA;;eAEcP,e","sourcesContent":["/**\n * @callback DoingHook\n * Returns whether a hook is currently being executed.\n *\n * @param {string} [hookName] The name of the hook to check for. If\n * omitted, will check for any hook being executed.\n *\n * @return {boolean} Whether the hook is being executed.\n */\n\n/**\n * Returns a function which, when invoked, will return whether a hook is\n * currently being executed.\n *\n * @param {import('.').Hooks} hooks Hooks instance.\n * @param {import('.').StoreKey} storeKey\n *\n * @return {DoingHook} Function that returns whether a hook is currently\n * being executed.\n */\nfunction createDoingHook( hooks, storeKey ) {\n\treturn function doingHook( hookName ) {\n\t\tconst hooksStore = hooks[ storeKey ];\n\n\t\t// If the hookName was not passed, check for any current hook.\n\t\tif ( 'undefined' === typeof hookName ) {\n\t\t\treturn 'undefined' !== typeof hooksStore.__current[ 0 ];\n\t\t}\n\n\t\t// Return the __current hook.\n\t\treturn hooksStore.__current[ 0 ]\n\t\t\t? hookName === hooksStore.__current[ 0 ].name\n\t\t\t: false;\n\t};\n}\n\nexport default createDoingHook;\n"]}
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ /**
9
+ * @callback HasHook
10
+ *
11
+ * Returns whether any handlers are attached for the given hookName and optional namespace.
12
+ *
13
+ * @param {string} hookName The name of the hook to check for.
14
+ * @param {string} [namespace] Optional. The unique namespace identifying the callback
15
+ * in the form `vendor/plugin/function`.
16
+ *
17
+ * @return {boolean} Whether there are handlers that are attached to the given hook.
18
+ */
19
+
20
+ /**
21
+ * Returns a function which, when invoked, will return whether any handlers are
22
+ * attached to a particular hook.
23
+ *
24
+ * @param {import('.').Hooks} hooks Hooks instance.
25
+ * @param {import('.').StoreKey} storeKey
26
+ *
27
+ * @return {HasHook} Function that returns whether any handlers are
28
+ * attached to a particular hook and optional namespace.
29
+ */
30
+ function createHasHook(hooks, storeKey) {
31
+ return function hasHook(hookName, namespace) {
32
+ const hooksStore = hooks[storeKey]; // Use the namespace if provided.
33
+
34
+ if ('undefined' !== typeof namespace) {
35
+ return hookName in hooksStore && hooksStore[hookName].handlers.some(hook => hook.namespace === namespace);
36
+ }
37
+
38
+ return hookName in hooksStore;
39
+ };
40
+ }
41
+
42
+ var _default = createHasHook;
43
+ exports.default = _default;
44
+ //# sourceMappingURL=createHasHook.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["@wordpress/hooks/src/createHasHook.js"],"names":["createHasHook","hooks","storeKey","hasHook","hookName","namespace","hooksStore","handlers","some","hook"],"mappings":";;;;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA;AACA;AACA;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,EAAwC;AAC9C,UAAMC,UAAU,GAAGL,KAAK,CAAEC,QAAF,CAAxB,CAD8C,CAG9C;;AACA,QAAK,gBAAgB,OAAOG,SAA5B,EAAwC;AACvC,aACCD,QAAQ,IAAIE,UAAZ,IACAA,UAAU,CAAEF,QAAF,CAAV,CAAuBG,QAAvB,CAAgCC,IAAhC,CACGC,IAAF,IAAYA,IAAI,CAACJ,SAAL,KAAmBA,SADhC,CAFD;AAMA;;AAED,WAAOD,QAAQ,IAAIE,UAAnB;AACA,GAdD;AAeA;;eAEcN,a","sourcesContent":["/**\n * @callback HasHook\n *\n * Returns whether any handlers are attached for the given hookName and optional namespace.\n *\n * @param {string} hookName The name of the hook to check for.\n * @param {string} [namespace] Optional. The unique namespace identifying the callback\n * in the form `vendor/plugin/function`.\n *\n * @return {boolean} Whether there are handlers that are attached to the given hook.\n */\n/**\n * Returns a function which, when invoked, will return whether any handlers are\n * attached to a particular hook.\n *\n * @param {import('.').Hooks} hooks Hooks instance.\n * @param {import('.').StoreKey} storeKey\n *\n * @return {HasHook} Function that returns whether any handlers are\n * attached to a particular hook and optional namespace.\n */\nfunction createHasHook( hooks, storeKey ) {\n\treturn function hasHook( hookName, namespace ) {\n\t\tconst hooksStore = hooks[ storeKey ];\n\n\t\t// Use the namespace if provided.\n\t\tif ( 'undefined' !== typeof namespace ) {\n\t\t\treturn (\n\t\t\t\thookName in hooksStore &&\n\t\t\t\thooksStore[ hookName ].handlers.some(\n\t\t\t\t\t( hook ) => hook.namespace === namespace\n\t\t\t\t)\n\t\t\t);\n\t\t}\n\n\t\treturn hookName in hooksStore;\n\t};\n}\n\nexport default createHasHook;\n"]}
@@ -0,0 +1,80 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.default = exports._Hooks = void 0;
9
+
10
+ var _createAddHook = _interopRequireDefault(require("./createAddHook"));
11
+
12
+ var _createRemoveHook = _interopRequireDefault(require("./createRemoveHook"));
13
+
14
+ var _createHasHook = _interopRequireDefault(require("./createHasHook"));
15
+
16
+ var _createRunHook = _interopRequireDefault(require("./createRunHook"));
17
+
18
+ var _createCurrentHook = _interopRequireDefault(require("./createCurrentHook"));
19
+
20
+ var _createDoingHook = _interopRequireDefault(require("./createDoingHook"));
21
+
22
+ var _createDidHook = _interopRequireDefault(require("./createDidHook"));
23
+
24
+ /**
25
+ * Internal dependencies
26
+ */
27
+
28
+ /**
29
+ * Internal class for constructing hooks. Use `createHooks()` function
30
+ *
31
+ * Note, it is necessary to expose this class to make its type public.
32
+ *
33
+ * @private
34
+ */
35
+ class _Hooks {
36
+ constructor() {
37
+ /** @type {import('.').Store} actions */
38
+ this.actions = Object.create(null);
39
+ this.actions.__current = [];
40
+ /** @type {import('.').Store} filters */
41
+
42
+ this.filters = Object.create(null);
43
+ this.filters.__current = [];
44
+ this.addAction = (0, _createAddHook.default)(this, 'actions');
45
+ this.addFilter = (0, _createAddHook.default)(this, 'filters');
46
+ this.removeAction = (0, _createRemoveHook.default)(this, 'actions');
47
+ this.removeFilter = (0, _createRemoveHook.default)(this, 'filters');
48
+ this.hasAction = (0, _createHasHook.default)(this, 'actions');
49
+ this.hasFilter = (0, _createHasHook.default)(this, 'filters');
50
+ this.removeAllActions = (0, _createRemoveHook.default)(this, 'actions', true);
51
+ this.removeAllFilters = (0, _createRemoveHook.default)(this, 'filters', true);
52
+ this.doAction = (0, _createRunHook.default)(this, 'actions');
53
+ this.applyFilters = (0, _createRunHook.default)(this, 'filters', true);
54
+ this.currentAction = (0, _createCurrentHook.default)(this, 'actions');
55
+ this.currentFilter = (0, _createCurrentHook.default)(this, 'filters');
56
+ this.doingAction = (0, _createDoingHook.default)(this, 'actions');
57
+ this.doingFilter = (0, _createDoingHook.default)(this, 'filters');
58
+ this.didAction = (0, _createDidHook.default)(this, 'actions');
59
+ this.didFilter = (0, _createDidHook.default)(this, 'filters');
60
+ }
61
+
62
+ }
63
+ /** @typedef {_Hooks} Hooks */
64
+
65
+ /**
66
+ * Returns an instance of the hooks object.
67
+ *
68
+ * @return {Hooks} A Hooks instance.
69
+ */
70
+
71
+
72
+ exports._Hooks = _Hooks;
73
+
74
+ function createHooks() {
75
+ return new _Hooks();
76
+ }
77
+
78
+ var _default = createHooks;
79
+ exports.default = _default;
80
+ //# sourceMappingURL=createHooks.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["@wordpress/hooks/src/createHooks.js"],"names":["_Hooks","constructor","actions","Object","create","__current","filters","addAction","addFilter","removeAction","removeFilter","hasAction","hasFilter","removeAllActions","removeAllFilters","doAction","applyFilters","currentAction","currentFilter","doingAction","doingFilter","didAction","didFilter","createHooks"],"mappings":";;;;;;;;;AAGA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;AATA;AACA;AACA;;AASA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,MAAMA,MAAN,CAAa;AACnBC,EAAAA,WAAW,GAAG;AACb;AACA,SAAKC,OAAL,GAAeC,MAAM,CAACC,MAAP,CAAe,IAAf,CAAf;AACA,SAAKF,OAAL,CAAaG,SAAb,GAAyB,EAAzB;AAEA;;AACA,SAAKC,OAAL,GAAeH,MAAM,CAACC,MAAP,CAAe,IAAf,CAAf;AACA,SAAKE,OAAL,CAAaD,SAAb,GAAyB,EAAzB;AAEA,SAAKE,SAAL,GAAiB,4BAAe,IAAf,EAAqB,SAArB,CAAjB;AACA,SAAKC,SAAL,GAAiB,4BAAe,IAAf,EAAqB,SAArB,CAAjB;AACA,SAAKC,YAAL,GAAoB,+BAAkB,IAAlB,EAAwB,SAAxB,CAApB;AACA,SAAKC,YAAL,GAAoB,+BAAkB,IAAlB,EAAwB,SAAxB,CAApB;AACA,SAAKC,SAAL,GAAiB,4BAAe,IAAf,EAAqB,SAArB,CAAjB;AACA,SAAKC,SAAL,GAAiB,4BAAe,IAAf,EAAqB,SAArB,CAAjB;AACA,SAAKC,gBAAL,GAAwB,+BAAkB,IAAlB,EAAwB,SAAxB,EAAmC,IAAnC,CAAxB;AACA,SAAKC,gBAAL,GAAwB,+BAAkB,IAAlB,EAAwB,SAAxB,EAAmC,IAAnC,CAAxB;AACA,SAAKC,QAAL,GAAgB,4BAAe,IAAf,EAAqB,SAArB,CAAhB;AACA,SAAKC,YAAL,GAAoB,4BAAe,IAAf,EAAqB,SAArB,EAAgC,IAAhC,CAApB;AACA,SAAKC,aAAL,GAAqB,gCAAmB,IAAnB,EAAyB,SAAzB,CAArB;AACA,SAAKC,aAAL,GAAqB,gCAAmB,IAAnB,EAAyB,SAAzB,CAArB;AACA,SAAKC,WAAL,GAAmB,8BAAiB,IAAjB,EAAuB,SAAvB,CAAnB;AACA,SAAKC,WAAL,GAAmB,8BAAiB,IAAjB,EAAuB,SAAvB,CAAnB;AACA,SAAKC,SAAL,GAAiB,4BAAe,IAAf,EAAqB,SAArB,CAAjB;AACA,SAAKC,SAAL,GAAiB,4BAAe,IAAf,EAAqB,SAArB,CAAjB;AACA;;AA1BkB;AA6BpB;;AAEA;AACA;AACA;AACA;AACA;;;;;AACA,SAASC,WAAT,GAAuB;AACtB,SAAO,IAAIvB,MAAJ,EAAP;AACA;;eAEcuB,W","sourcesContent":["/**\n * Internal dependencies\n */\nimport createAddHook from './createAddHook';\nimport createRemoveHook from './createRemoveHook';\nimport createHasHook from './createHasHook';\nimport createRunHook from './createRunHook';\nimport createCurrentHook from './createCurrentHook';\nimport createDoingHook from './createDoingHook';\nimport createDidHook from './createDidHook';\n\n/**\n * Internal class for constructing hooks. Use `createHooks()` function\n *\n * Note, it is necessary to expose this class to make its type public.\n *\n * @private\n */\nexport class _Hooks {\n\tconstructor() {\n\t\t/** @type {import('.').Store} actions */\n\t\tthis.actions = Object.create( null );\n\t\tthis.actions.__current = [];\n\n\t\t/** @type {import('.').Store} filters */\n\t\tthis.filters = Object.create( null );\n\t\tthis.filters.__current = [];\n\n\t\tthis.addAction = createAddHook( this, 'actions' );\n\t\tthis.addFilter = createAddHook( this, 'filters' );\n\t\tthis.removeAction = createRemoveHook( this, 'actions' );\n\t\tthis.removeFilter = createRemoveHook( this, 'filters' );\n\t\tthis.hasAction = createHasHook( this, 'actions' );\n\t\tthis.hasFilter = createHasHook( this, 'filters' );\n\t\tthis.removeAllActions = createRemoveHook( this, 'actions', true );\n\t\tthis.removeAllFilters = createRemoveHook( this, 'filters', true );\n\t\tthis.doAction = createRunHook( this, 'actions' );\n\t\tthis.applyFilters = createRunHook( this, 'filters', true );\n\t\tthis.currentAction = createCurrentHook( this, 'actions' );\n\t\tthis.currentFilter = createCurrentHook( this, 'filters' );\n\t\tthis.doingAction = createDoingHook( this, 'actions' );\n\t\tthis.doingFilter = createDoingHook( this, 'filters' );\n\t\tthis.didAction = createDidHook( this, 'actions' );\n\t\tthis.didFilter = createDidHook( this, 'filters' );\n\t}\n}\n\n/** @typedef {_Hooks} Hooks */\n\n/**\n * Returns an instance of the hooks object.\n *\n * @return {Hooks} A Hooks instance.\n */\nfunction createHooks() {\n\treturn new _Hooks();\n}\n\nexport default createHooks;\n"]}
@@ -0,0 +1,99 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.default = void 0;
9
+
10
+ var _validateNamespace = _interopRequireDefault(require("./validateNamespace.js"));
11
+
12
+ var _validateHookName = _interopRequireDefault(require("./validateHookName.js"));
13
+
14
+ /**
15
+ * Internal dependencies
16
+ */
17
+
18
+ /**
19
+ * @callback RemoveHook
20
+ * Removes the specified callback (or all callbacks) from the hook with a given hookName
21
+ * and namespace.
22
+ *
23
+ * @param {string} hookName The name of the hook to modify.
24
+ * @param {string} namespace The unique namespace identifying the callback in the
25
+ * form `vendor/plugin/function`.
26
+ *
27
+ * @return {number | undefined} The number of callbacks removed.
28
+ */
29
+
30
+ /**
31
+ * Returns a function which, when invoked, will remove a specified hook or all
32
+ * hooks by the given name.
33
+ *
34
+ * @param {import('.').Hooks} hooks Hooks instance.
35
+ * @param {import('.').StoreKey} storeKey
36
+ * @param {boolean} [removeAll=false] Whether to remove all callbacks for a hookName,
37
+ * without regard to namespace. Used to create
38
+ * `removeAll*` functions.
39
+ *
40
+ * @return {RemoveHook} Function that removes hooks.
41
+ */
42
+ function createRemoveHook(hooks, storeKey, removeAll = false) {
43
+ return function removeHook(hookName, namespace) {
44
+ const hooksStore = hooks[storeKey];
45
+
46
+ if (!(0, _validateHookName.default)(hookName)) {
47
+ return;
48
+ }
49
+
50
+ if (!removeAll && !(0, _validateNamespace.default)(namespace)) {
51
+ return;
52
+ } // Bail if no hooks exist by this name
53
+
54
+
55
+ if (!hooksStore[hookName]) {
56
+ return 0;
57
+ }
58
+
59
+ let handlersRemoved = 0;
60
+
61
+ if (removeAll) {
62
+ handlersRemoved = hooksStore[hookName].handlers.length;
63
+ hooksStore[hookName] = {
64
+ runs: hooksStore[hookName].runs,
65
+ handlers: []
66
+ };
67
+ } else {
68
+ // Try to find the specified callback to remove.
69
+ const handlers = hooksStore[hookName].handlers;
70
+
71
+ for (let i = handlers.length - 1; i >= 0; i--) {
72
+ if (handlers[i].namespace === namespace) {
73
+ handlers.splice(i, 1);
74
+ handlersRemoved++; // This callback may also be part of a hook that is
75
+ // currently executing. If the callback we're removing
76
+ // comes after the current callback, there's no problem;
77
+ // otherwise we need to decrease the execution index of any
78
+ // other runs by 1 to account for the removed element.
79
+
80
+ hooksStore.__current.forEach(hookInfo => {
81
+ if (hookInfo.name === hookName && hookInfo.currentIndex >= i) {
82
+ hookInfo.currentIndex--;
83
+ }
84
+ });
85
+ }
86
+ }
87
+ }
88
+
89
+ if (hookName !== 'hookRemoved') {
90
+ hooks.doAction('hookRemoved', hookName, namespace);
91
+ }
92
+
93
+ return handlersRemoved;
94
+ };
95
+ }
96
+
97
+ var _default = createRemoveHook;
98
+ exports.default = _default;
99
+ //# sourceMappingURL=createRemoveHook.js.map
@@ -0,0 +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,EAA4CC,SAAS,GAAG,KAAxD,EAAgE;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,gCAAmBG,SAAnB,CAAtB,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;;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"]}