@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.
- package/CHANGELOG.md +72 -0
- package/LICENSE.md +788 -0
- package/README.md +66 -0
- package/benchmark/index.js +56 -0
- package/build/createAddHook.js +114 -0
- package/build/createAddHook.js.map +1 -0
- package/build/createCurrentHook.js +29 -0
- package/build/createCurrentHook.js.map +1 -0
- package/build/createDidHook.js +49 -0
- package/build/createDidHook.js.map +1 -0
- package/build/createDoingHook.js +43 -0
- package/build/createDoingHook.js.map +1 -0
- package/build/createHasHook.js +44 -0
- package/build/createHasHook.js.map +1 -0
- package/build/createHooks.js +80 -0
- package/build/createHooks.js.map +1 -0
- package/build/createRemoveHook.js +99 -0
- package/build/createRemoveHook.js.map +1 -0
- package/build/createRunHook.js +73 -0
- package/build/createRunHook.js.map +1 -0
- package/build/index.js +94 -0
- package/build/index.js.map +1 -0
- package/build/validateHookName.js +41 -0
- package/build/validateHookName.js.map +1 -0
- package/build/validateNamespace.js +34 -0
- package/build/validateNamespace.js.map +1 -0
- package/build-module/createAddHook.js +102 -0
- package/build-module/createAddHook.js.map +1 -0
- package/build-module/createCurrentHook.js +21 -0
- package/build-module/createCurrentHook.js.map +1 -0
- package/build-module/createDidHook.js +38 -0
- package/build-module/createDidHook.js.map +1 -0
- package/build-module/createDoingHook.js +35 -0
- package/build-module/createDoingHook.js.map +1 -0
- package/build-module/createHasHook.js +36 -0
- package/build-module/createHasHook.js.map +1 -0
- package/build-module/createHooks.js +60 -0
- package/build-module/createHooks.js.map +1 -0
- package/build-module/createRemoveHook.js +87 -0
- package/build-module/createRemoveHook.js.map +1 -0
- package/build-module/createRunHook.js +65 -0
- package/build-module/createRunHook.js.map +1 -0
- package/build-module/index.js +60 -0
- package/build-module/index.js.map +1 -0
- package/build-module/validateHookName.js +33 -0
- package/build-module/validateHookName.js.map +1 -0
- package/build-module/validateNamespace.js +26 -0
- package/build-module/validateNamespace.js.map +1 -0
- package/build-types/createAddHook.d.ts +25 -0
- package/build-types/createAddHook.d.ts.map +1 -0
- package/build-types/createCurrentHook.d.ts +13 -0
- package/build-types/createCurrentHook.d.ts.map +1 -0
- package/build-types/createDidHook.d.ts +25 -0
- package/build-types/createDidHook.d.ts.map +1 -0
- package/build-types/createDoingHook.d.ts +26 -0
- package/build-types/createDoingHook.d.ts.map +1 -0
- package/build-types/createHasHook.d.ts +28 -0
- package/build-types/createHasHook.d.ts.map +1 -0
- package/build-types/createHooks.d.ts +39 -0
- package/build-types/createHooks.d.ts.map +1 -0
- package/build-types/createRemoveHook.d.ts +31 -0
- package/build-types/createRemoveHook.d.ts.map +1 -0
- package/build-types/createRunHook.d.ts +15 -0
- package/build-types/createRunHook.d.ts.map +1 -0
- package/build-types/index.d.ts +88 -0
- package/build-types/index.d.ts.map +1 -0
- package/build-types/validateHookName.d.ts +12 -0
- package/build-types/validateHookName.d.ts.map +1 -0
- package/build-types/validateNamespace.d.ts +11 -0
- package/build-types/validateNamespace.d.ts.map +1 -0
- package/package.json +35 -0
- package/src/createAddHook.js +107 -0
- package/src/createCurrentHook.js +22 -0
- package/src/createDidHook.js +39 -0
- package/src/createDoingHook.js +37 -0
- package/src/createHasHook.js +40 -0
- package/src/createHooks.js +59 -0
- package/src/createRemoveHook.js +88 -0
- package/src/createRunHook.js +66 -0
- package/src/index.js +82 -0
- package/src/test/index.test.js +945 -0
- package/src/validateHookName.js +34 -0
- package/src/validateNamespace.js +27 -0
- package/tsconfig.json +9 -0
- package/tsconfig.tsbuildinfo +1 -0
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export default validateHookName;
|
|
2
|
+
/**
|
|
3
|
+
* Validate a hookName string.
|
|
4
|
+
*
|
|
5
|
+
* @param {string} hookName The hook name to validate. Should be a non empty string containing
|
|
6
|
+
* only numbers, letters, dashes, periods and underscores. Also,
|
|
7
|
+
* the hook name cannot begin with `__`.
|
|
8
|
+
*
|
|
9
|
+
* @return {boolean} Whether the hook name is valid.
|
|
10
|
+
*/
|
|
11
|
+
declare function validateHookName(hookName: string): boolean;
|
|
12
|
+
//# sourceMappingURL=validateHookName.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validateHookName.d.ts","sourceRoot":"","sources":["../src/validateHookName.js"],"names":[],"mappings":";AAAA;;;;;;;;GAQG;AACH,4CANW,MAAM,GAIL,OAAO,CAwBlB"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export default validateNamespace;
|
|
2
|
+
/**
|
|
3
|
+
* Validate a namespace string.
|
|
4
|
+
*
|
|
5
|
+
* @param {string} namespace The namespace to validate - should take the form
|
|
6
|
+
* `vendor/plugin/function`.
|
|
7
|
+
*
|
|
8
|
+
* @return {boolean} Whether the namespace is valid.
|
|
9
|
+
*/
|
|
10
|
+
declare function validateNamespace(namespace: string): boolean;
|
|
11
|
+
//# sourceMappingURL=validateNamespace.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validateNamespace.d.ts","sourceRoot":"","sources":["../src/validateNamespace.js"],"names":[],"mappings":";AAAA;;;;;;;GAOG;AACH,8CALW,MAAM,GAGL,OAAO,CAkBlB"}
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@wordpress/hooks",
|
|
3
|
+
"version": "3.2.1",
|
|
4
|
+
"description": "WordPress hooks library.",
|
|
5
|
+
"author": "The WordPress Contributors",
|
|
6
|
+
"license": "GPL-2.0-or-later",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"wordpress",
|
|
9
|
+
"gutenberg",
|
|
10
|
+
"hooks"
|
|
11
|
+
],
|
|
12
|
+
"homepage": "https://github.com/WordPress/gutenberg/tree/HEAD/packages/hooks/README.md",
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "https://github.com/WordPress/gutenberg.git",
|
|
16
|
+
"directory": "packages/hooks"
|
|
17
|
+
},
|
|
18
|
+
"bugs": {
|
|
19
|
+
"url": "https://github.com/WordPress/gutenberg/issues"
|
|
20
|
+
},
|
|
21
|
+
"engines": {
|
|
22
|
+
"node": ">=12"
|
|
23
|
+
},
|
|
24
|
+
"main": "build/index.js",
|
|
25
|
+
"module": "build-module/index.js",
|
|
26
|
+
"react-native": "src/index",
|
|
27
|
+
"types": "build-types",
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@babel/runtime": "^7.13.10"
|
|
30
|
+
},
|
|
31
|
+
"publishConfig": {
|
|
32
|
+
"access": "public"
|
|
33
|
+
},
|
|
34
|
+
"gitHead": "8f7f052bc04e3f4eb50f479ced14be1489b9fa79"
|
|
35
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal dependencies
|
|
3
|
+
*/
|
|
4
|
+
import validateNamespace from './validateNamespace.js';
|
|
5
|
+
import validateHookName from './validateHookName.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @callback AddHook
|
|
9
|
+
*
|
|
10
|
+
* Adds the hook to the appropriate hooks container.
|
|
11
|
+
*
|
|
12
|
+
* @param {string} hookName Name of hook to add
|
|
13
|
+
* @param {string} namespace The unique namespace identifying the callback in the form `vendor/plugin/function`.
|
|
14
|
+
* @param {import('.').Callback} callback Function to call when the hook is run
|
|
15
|
+
* @param {number} [priority=10] Priority of this hook
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Returns a function which, when invoked, will add a hook.
|
|
20
|
+
*
|
|
21
|
+
* @param {import('.').Hooks} hooks Hooks instance.
|
|
22
|
+
* @param {import('.').StoreKey} storeKey
|
|
23
|
+
*
|
|
24
|
+
* @return {AddHook} Function that adds a new hook.
|
|
25
|
+
*/
|
|
26
|
+
function createAddHook( hooks, storeKey ) {
|
|
27
|
+
return function addHook( hookName, namespace, callback, priority = 10 ) {
|
|
28
|
+
const hooksStore = hooks[ storeKey ];
|
|
29
|
+
|
|
30
|
+
if ( ! validateHookName( hookName ) ) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if ( ! validateNamespace( namespace ) ) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if ( 'function' !== typeof callback ) {
|
|
39
|
+
// eslint-disable-next-line no-console
|
|
40
|
+
console.error( 'The hook callback must be a function.' );
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Validate numeric priority
|
|
45
|
+
if ( 'number' !== typeof priority ) {
|
|
46
|
+
// eslint-disable-next-line no-console
|
|
47
|
+
console.error(
|
|
48
|
+
'If specified, the hook priority must be a number.'
|
|
49
|
+
);
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const handler = { callback, priority, namespace };
|
|
54
|
+
|
|
55
|
+
if ( hooksStore[ hookName ] ) {
|
|
56
|
+
// Find the correct insert index of the new hook.
|
|
57
|
+
const handlers = hooksStore[ hookName ].handlers;
|
|
58
|
+
|
|
59
|
+
/** @type {number} */
|
|
60
|
+
let i;
|
|
61
|
+
for ( i = handlers.length; i > 0; i-- ) {
|
|
62
|
+
if ( priority >= handlers[ i - 1 ].priority ) {
|
|
63
|
+
break;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if ( i === handlers.length ) {
|
|
68
|
+
// If append, operate via direct assignment.
|
|
69
|
+
handlers[ i ] = handler;
|
|
70
|
+
} else {
|
|
71
|
+
// Otherwise, insert before index via splice.
|
|
72
|
+
handlers.splice( i, 0, handler );
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// We may also be currently executing this hook. If the callback
|
|
76
|
+
// we're adding would come after the current callback, there's no
|
|
77
|
+
// problem; otherwise we need to increase the execution index of
|
|
78
|
+
// any other runs by 1 to account for the added element.
|
|
79
|
+
hooksStore.__current.forEach( ( hookInfo ) => {
|
|
80
|
+
if (
|
|
81
|
+
hookInfo.name === hookName &&
|
|
82
|
+
hookInfo.currentIndex >= i
|
|
83
|
+
) {
|
|
84
|
+
hookInfo.currentIndex++;
|
|
85
|
+
}
|
|
86
|
+
} );
|
|
87
|
+
} else {
|
|
88
|
+
// This is the first hook of its type.
|
|
89
|
+
hooksStore[ hookName ] = {
|
|
90
|
+
handlers: [ handler ],
|
|
91
|
+
runs: 0,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if ( hookName !== 'hookAdded' ) {
|
|
96
|
+
hooks.doAction(
|
|
97
|
+
'hookAdded',
|
|
98
|
+
hookName,
|
|
99
|
+
namespace,
|
|
100
|
+
callback,
|
|
101
|
+
priority
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export default createAddHook;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a function which, when invoked, will return the name of the
|
|
3
|
+
* currently running hook, or `null` if no hook of the given type is currently
|
|
4
|
+
* running.
|
|
5
|
+
*
|
|
6
|
+
* @param {import('.').Hooks} hooks Hooks instance.
|
|
7
|
+
* @param {import('.').StoreKey} storeKey
|
|
8
|
+
*
|
|
9
|
+
* @return {() => string | null} Function that returns the current hook name or null.
|
|
10
|
+
*/
|
|
11
|
+
function createCurrentHook( hooks, storeKey ) {
|
|
12
|
+
return function currentHook() {
|
|
13
|
+
const hooksStore = hooks[ storeKey ];
|
|
14
|
+
|
|
15
|
+
return (
|
|
16
|
+
hooksStore.__current[ hooksStore.__current.length - 1 ]?.name ??
|
|
17
|
+
null
|
|
18
|
+
);
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export default createCurrentHook;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal dependencies
|
|
3
|
+
*/
|
|
4
|
+
import validateHookName from './validateHookName.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @callback DidHook
|
|
8
|
+
*
|
|
9
|
+
* Returns the number of times an action has been fired.
|
|
10
|
+
*
|
|
11
|
+
* @param {string} hookName The hook name to check.
|
|
12
|
+
*
|
|
13
|
+
* @return {number | undefined} The number of times the hook has run.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Returns a function which, when invoked, will return the number of times a
|
|
18
|
+
* hook has been called.
|
|
19
|
+
*
|
|
20
|
+
* @param {import('.').Hooks} hooks Hooks instance.
|
|
21
|
+
* @param {import('.').StoreKey} storeKey
|
|
22
|
+
*
|
|
23
|
+
* @return {DidHook} Function that returns a hook's call count.
|
|
24
|
+
*/
|
|
25
|
+
function createDidHook( hooks, storeKey ) {
|
|
26
|
+
return function didHook( hookName ) {
|
|
27
|
+
const hooksStore = hooks[ storeKey ];
|
|
28
|
+
|
|
29
|
+
if ( ! validateHookName( hookName ) ) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return hooksStore[ hookName ] && hooksStore[ hookName ].runs
|
|
34
|
+
? hooksStore[ hookName ].runs
|
|
35
|
+
: 0;
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export default createDidHook;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @callback DoingHook
|
|
3
|
+
* Returns whether a hook is currently being executed.
|
|
4
|
+
*
|
|
5
|
+
* @param {string} [hookName] The name of the hook to check for. If
|
|
6
|
+
* omitted, will check for any hook being executed.
|
|
7
|
+
*
|
|
8
|
+
* @return {boolean} Whether the hook is being executed.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Returns a function which, when invoked, will return whether a hook is
|
|
13
|
+
* currently being executed.
|
|
14
|
+
*
|
|
15
|
+
* @param {import('.').Hooks} hooks Hooks instance.
|
|
16
|
+
* @param {import('.').StoreKey} storeKey
|
|
17
|
+
*
|
|
18
|
+
* @return {DoingHook} Function that returns whether a hook is currently
|
|
19
|
+
* being executed.
|
|
20
|
+
*/
|
|
21
|
+
function createDoingHook( hooks, storeKey ) {
|
|
22
|
+
return function doingHook( hookName ) {
|
|
23
|
+
const hooksStore = hooks[ storeKey ];
|
|
24
|
+
|
|
25
|
+
// If the hookName was not passed, check for any current hook.
|
|
26
|
+
if ( 'undefined' === typeof hookName ) {
|
|
27
|
+
return 'undefined' !== typeof hooksStore.__current[ 0 ];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Return the __current hook.
|
|
31
|
+
return hooksStore.__current[ 0 ]
|
|
32
|
+
? hookName === hooksStore.__current[ 0 ].name
|
|
33
|
+
: false;
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export default createDoingHook;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @callback HasHook
|
|
3
|
+
*
|
|
4
|
+
* Returns whether any handlers are attached for the given hookName and optional namespace.
|
|
5
|
+
*
|
|
6
|
+
* @param {string} hookName The name of the hook to check for.
|
|
7
|
+
* @param {string} [namespace] Optional. The unique namespace identifying the callback
|
|
8
|
+
* in the form `vendor/plugin/function`.
|
|
9
|
+
*
|
|
10
|
+
* @return {boolean} Whether there are handlers that are attached to the given hook.
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* Returns a function which, when invoked, will return whether any handlers are
|
|
14
|
+
* attached to a particular hook.
|
|
15
|
+
*
|
|
16
|
+
* @param {import('.').Hooks} hooks Hooks instance.
|
|
17
|
+
* @param {import('.').StoreKey} storeKey
|
|
18
|
+
*
|
|
19
|
+
* @return {HasHook} Function that returns whether any handlers are
|
|
20
|
+
* attached to a particular hook and optional namespace.
|
|
21
|
+
*/
|
|
22
|
+
function createHasHook( hooks, storeKey ) {
|
|
23
|
+
return function hasHook( hookName, namespace ) {
|
|
24
|
+
const hooksStore = hooks[ storeKey ];
|
|
25
|
+
|
|
26
|
+
// Use the namespace if provided.
|
|
27
|
+
if ( 'undefined' !== typeof namespace ) {
|
|
28
|
+
return (
|
|
29
|
+
hookName in hooksStore &&
|
|
30
|
+
hooksStore[ hookName ].handlers.some(
|
|
31
|
+
( hook ) => hook.namespace === namespace
|
|
32
|
+
)
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return hookName in hooksStore;
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export default createHasHook;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal dependencies
|
|
3
|
+
*/
|
|
4
|
+
import createAddHook from './createAddHook';
|
|
5
|
+
import createRemoveHook from './createRemoveHook';
|
|
6
|
+
import createHasHook from './createHasHook';
|
|
7
|
+
import createRunHook from './createRunHook';
|
|
8
|
+
import createCurrentHook from './createCurrentHook';
|
|
9
|
+
import createDoingHook from './createDoingHook';
|
|
10
|
+
import createDidHook from './createDidHook';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Internal class for constructing hooks. Use `createHooks()` function
|
|
14
|
+
*
|
|
15
|
+
* Note, it is necessary to expose this class to make its type public.
|
|
16
|
+
*
|
|
17
|
+
* @private
|
|
18
|
+
*/
|
|
19
|
+
export class _Hooks {
|
|
20
|
+
constructor() {
|
|
21
|
+
/** @type {import('.').Store} actions */
|
|
22
|
+
this.actions = Object.create( null );
|
|
23
|
+
this.actions.__current = [];
|
|
24
|
+
|
|
25
|
+
/** @type {import('.').Store} filters */
|
|
26
|
+
this.filters = Object.create( null );
|
|
27
|
+
this.filters.__current = [];
|
|
28
|
+
|
|
29
|
+
this.addAction = createAddHook( this, 'actions' );
|
|
30
|
+
this.addFilter = createAddHook( this, 'filters' );
|
|
31
|
+
this.removeAction = createRemoveHook( this, 'actions' );
|
|
32
|
+
this.removeFilter = createRemoveHook( this, 'filters' );
|
|
33
|
+
this.hasAction = createHasHook( this, 'actions' );
|
|
34
|
+
this.hasFilter = createHasHook( this, 'filters' );
|
|
35
|
+
this.removeAllActions = createRemoveHook( this, 'actions', true );
|
|
36
|
+
this.removeAllFilters = createRemoveHook( this, 'filters', true );
|
|
37
|
+
this.doAction = createRunHook( this, 'actions' );
|
|
38
|
+
this.applyFilters = createRunHook( this, 'filters', true );
|
|
39
|
+
this.currentAction = createCurrentHook( this, 'actions' );
|
|
40
|
+
this.currentFilter = createCurrentHook( this, 'filters' );
|
|
41
|
+
this.doingAction = createDoingHook( this, 'actions' );
|
|
42
|
+
this.doingFilter = createDoingHook( this, 'filters' );
|
|
43
|
+
this.didAction = createDidHook( this, 'actions' );
|
|
44
|
+
this.didFilter = createDidHook( this, 'filters' );
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** @typedef {_Hooks} Hooks */
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Returns an instance of the hooks object.
|
|
52
|
+
*
|
|
53
|
+
* @return {Hooks} A Hooks instance.
|
|
54
|
+
*/
|
|
55
|
+
function createHooks() {
|
|
56
|
+
return new _Hooks();
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export default createHooks;
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal dependencies
|
|
3
|
+
*/
|
|
4
|
+
import validateNamespace from './validateNamespace.js';
|
|
5
|
+
import validateHookName from './validateHookName.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @callback RemoveHook
|
|
9
|
+
* Removes the specified callback (or all callbacks) from the hook with a given hookName
|
|
10
|
+
* and namespace.
|
|
11
|
+
*
|
|
12
|
+
* @param {string} hookName The name of the hook to modify.
|
|
13
|
+
* @param {string} namespace The unique namespace identifying the callback in the
|
|
14
|
+
* form `vendor/plugin/function`.
|
|
15
|
+
*
|
|
16
|
+
* @return {number | undefined} The number of callbacks removed.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Returns a function which, when invoked, will remove a specified hook or all
|
|
21
|
+
* hooks by the given name.
|
|
22
|
+
*
|
|
23
|
+
* @param {import('.').Hooks} hooks Hooks instance.
|
|
24
|
+
* @param {import('.').StoreKey} storeKey
|
|
25
|
+
* @param {boolean} [removeAll=false] Whether to remove all callbacks for a hookName,
|
|
26
|
+
* without regard to namespace. Used to create
|
|
27
|
+
* `removeAll*` functions.
|
|
28
|
+
*
|
|
29
|
+
* @return {RemoveHook} Function that removes hooks.
|
|
30
|
+
*/
|
|
31
|
+
function createRemoveHook( hooks, storeKey, removeAll = false ) {
|
|
32
|
+
return function removeHook( hookName, namespace ) {
|
|
33
|
+
const hooksStore = hooks[ storeKey ];
|
|
34
|
+
|
|
35
|
+
if ( ! validateHookName( hookName ) ) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if ( ! removeAll && ! validateNamespace( namespace ) ) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Bail if no hooks exist by this name
|
|
44
|
+
if ( ! hooksStore[ hookName ] ) {
|
|
45
|
+
return 0;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
let handlersRemoved = 0;
|
|
49
|
+
|
|
50
|
+
if ( removeAll ) {
|
|
51
|
+
handlersRemoved = hooksStore[ hookName ].handlers.length;
|
|
52
|
+
hooksStore[ hookName ] = {
|
|
53
|
+
runs: hooksStore[ hookName ].runs,
|
|
54
|
+
handlers: [],
|
|
55
|
+
};
|
|
56
|
+
} else {
|
|
57
|
+
// Try to find the specified callback to remove.
|
|
58
|
+
const handlers = hooksStore[ hookName ].handlers;
|
|
59
|
+
for ( let i = handlers.length - 1; i >= 0; i-- ) {
|
|
60
|
+
if ( handlers[ i ].namespace === namespace ) {
|
|
61
|
+
handlers.splice( i, 1 );
|
|
62
|
+
handlersRemoved++;
|
|
63
|
+
// This callback may also be part of a hook that is
|
|
64
|
+
// currently executing. If the callback we're removing
|
|
65
|
+
// comes after the current callback, there's no problem;
|
|
66
|
+
// otherwise we need to decrease the execution index of any
|
|
67
|
+
// other runs by 1 to account for the removed element.
|
|
68
|
+
hooksStore.__current.forEach( ( hookInfo ) => {
|
|
69
|
+
if (
|
|
70
|
+
hookInfo.name === hookName &&
|
|
71
|
+
hookInfo.currentIndex >= i
|
|
72
|
+
) {
|
|
73
|
+
hookInfo.currentIndex--;
|
|
74
|
+
}
|
|
75
|
+
} );
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if ( hookName !== 'hookRemoved' ) {
|
|
81
|
+
hooks.doAction( 'hookRemoved', hookName, namespace );
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return handlersRemoved;
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export default createRemoveHook;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a function which, when invoked, will execute all callbacks
|
|
3
|
+
* registered to a hook of the specified type, optionally returning the final
|
|
4
|
+
* value of the call chain.
|
|
5
|
+
*
|
|
6
|
+
* @param {import('.').Hooks} hooks Hooks instance.
|
|
7
|
+
* @param {import('.').StoreKey} storeKey
|
|
8
|
+
* @param {boolean} [returnFirstArg=false] Whether each hook callback is expected to
|
|
9
|
+
* return its first argument.
|
|
10
|
+
*
|
|
11
|
+
* @return {(hookName:string, ...args: unknown[]) => unknown} Function that runs hook callbacks.
|
|
12
|
+
*/
|
|
13
|
+
function createRunHook( hooks, storeKey, returnFirstArg = false ) {
|
|
14
|
+
return function runHooks( hookName, ...args ) {
|
|
15
|
+
const hooksStore = hooks[ storeKey ];
|
|
16
|
+
|
|
17
|
+
if ( ! hooksStore[ hookName ] ) {
|
|
18
|
+
hooksStore[ hookName ] = {
|
|
19
|
+
handlers: [],
|
|
20
|
+
runs: 0,
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
hooksStore[ hookName ].runs++;
|
|
25
|
+
|
|
26
|
+
const handlers = hooksStore[ hookName ].handlers;
|
|
27
|
+
|
|
28
|
+
// The following code is stripped from production builds.
|
|
29
|
+
if ( 'production' !== process.env.NODE_ENV ) {
|
|
30
|
+
// Handle any 'all' hooks registered.
|
|
31
|
+
if ( 'hookAdded' !== hookName && hooksStore.all ) {
|
|
32
|
+
handlers.push( ...hooksStore.all.handlers );
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if ( ! handlers || ! handlers.length ) {
|
|
37
|
+
return returnFirstArg ? args[ 0 ] : undefined;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const hookInfo = {
|
|
41
|
+
name: hookName,
|
|
42
|
+
currentIndex: 0,
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
hooksStore.__current.push( hookInfo );
|
|
46
|
+
|
|
47
|
+
while ( hookInfo.currentIndex < handlers.length ) {
|
|
48
|
+
const handler = handlers[ hookInfo.currentIndex ];
|
|
49
|
+
|
|
50
|
+
const result = handler.callback.apply( null, args );
|
|
51
|
+
if ( returnFirstArg ) {
|
|
52
|
+
args[ 0 ] = result;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
hookInfo.currentIndex++;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
hooksStore.__current.pop();
|
|
59
|
+
|
|
60
|
+
if ( returnFirstArg ) {
|
|
61
|
+
return args[ 0 ];
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export default createRunHook;
|
package/src/index.js
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal dependencies
|
|
3
|
+
*/
|
|
4
|
+
import createHooks from './createHooks';
|
|
5
|
+
|
|
6
|
+
/** @typedef {(...args: any[])=>any} Callback */
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @typedef Handler
|
|
10
|
+
* @property {Callback} callback The callback
|
|
11
|
+
* @property {string} namespace The namespace
|
|
12
|
+
* @property {number} priority The namespace
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @typedef Hook
|
|
17
|
+
* @property {Handler[]} handlers Array of handlers
|
|
18
|
+
* @property {number} runs Run counter
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @typedef Current
|
|
23
|
+
* @property {string} name Hook name
|
|
24
|
+
* @property {number} currentIndex The index
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @typedef {Record<string, Hook> & {__current: Current[]}} Store
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* @typedef {'actions' | 'filters'} StoreKey
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* @typedef {import('./createHooks').Hooks} Hooks
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
export const defaultHooks = createHooks();
|
|
40
|
+
|
|
41
|
+
const {
|
|
42
|
+
addAction,
|
|
43
|
+
addFilter,
|
|
44
|
+
removeAction,
|
|
45
|
+
removeFilter,
|
|
46
|
+
hasAction,
|
|
47
|
+
hasFilter,
|
|
48
|
+
removeAllActions,
|
|
49
|
+
removeAllFilters,
|
|
50
|
+
doAction,
|
|
51
|
+
applyFilters,
|
|
52
|
+
currentAction,
|
|
53
|
+
currentFilter,
|
|
54
|
+
doingAction,
|
|
55
|
+
doingFilter,
|
|
56
|
+
didAction,
|
|
57
|
+
didFilter,
|
|
58
|
+
actions,
|
|
59
|
+
filters,
|
|
60
|
+
} = defaultHooks;
|
|
61
|
+
|
|
62
|
+
export {
|
|
63
|
+
createHooks,
|
|
64
|
+
addAction,
|
|
65
|
+
addFilter,
|
|
66
|
+
removeAction,
|
|
67
|
+
removeFilter,
|
|
68
|
+
hasAction,
|
|
69
|
+
hasFilter,
|
|
70
|
+
removeAllActions,
|
|
71
|
+
removeAllFilters,
|
|
72
|
+
doAction,
|
|
73
|
+
applyFilters,
|
|
74
|
+
currentAction,
|
|
75
|
+
currentFilter,
|
|
76
|
+
doingAction,
|
|
77
|
+
doingFilter,
|
|
78
|
+
didAction,
|
|
79
|
+
didFilter,
|
|
80
|
+
actions,
|
|
81
|
+
filters,
|
|
82
|
+
};
|