Skip to content

Commit 90747a1

Browse files
im3dabasiaim3dabasiamanzoorwanijk
authored andcommitted
Migrate hooks package to TypeScript (#70488)
Co-authored-by: im3dabasia <im3dabasia1@git.wordpress.org> Co-authored-by: manzoorwanijk <manzoorwanijk@git.wordpress.org>
1 parent 9a0d556 commit 90747a1

File tree

12 files changed

+199
-117
lines changed

12 files changed

+199
-117
lines changed

packages/hooks/src/createAddHook.js renamed to packages/hooks/src/createAddHook.ts

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,43 @@
11
/**
22
* Internal dependencies
33
*/
4-
import validateNamespace from './validateNamespace.js';
5-
import validateHookName from './validateHookName.js';
4+
import validateNamespace from './validateNamespace';
5+
import validateHookName from './validateHookName';
6+
import type { Callback, Hooks, StoreKey } from '.';
7+
import type { Handler, HookInfo } from './types';
68

79
/**
8-
* @callback AddHook
910
*
1011
* 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
1612
*/
13+
export type AddHook = (
14+
/**
15+
* Name of hook to add
16+
*/
17+
hookName: string,
18+
/**
19+
* The unique namespace identifying the callback in the form.
20+
*/
21+
namespace: string,
22+
/**
23+
* Function to call when the hook is run.
24+
*/
25+
callback: Callback,
26+
/**
27+
* Priority of this hook
28+
*/
29+
priority?: number
30+
) => void;
1731

1832
/**
1933
* Returns a function which, when invoked, will add a hook.
2034
*
21-
* @param {import('.').Hooks} hooks Hooks instance.
22-
* @param {import('.').StoreKey} storeKey
35+
* @param hooks Hooks instance.
36+
* @param storeKey
2337
*
24-
* @return {AddHook} Function that adds a new hook.
38+
* @return Function that adds a new hook.
2539
*/
26-
function createAddHook( hooks, storeKey ) {
40+
function createAddHook( hooks: Hooks, storeKey: StoreKey ): AddHook {
2741
return function addHook( hookName, namespace, callback, priority = 10 ) {
2842
const hooksStore = hooks[ storeKey ];
2943

@@ -50,14 +64,13 @@ function createAddHook( hooks, storeKey ) {
5064
return;
5165
}
5266

53-
const handler = { callback, priority, namespace };
67+
const handler: Handler = { callback, priority, namespace };
5468

5569
if ( hooksStore[ hookName ] ) {
5670
// Find the correct insert index of the new hook.
5771
const handlers = hooksStore[ hookName ].handlers;
5872

59-
/** @type {number} */
60-
let i;
73+
let i: number;
6174
for ( i = handlers.length; i > 0; i-- ) {
6275
if ( priority >= handlers[ i - 1 ].priority ) {
6376
break;
@@ -76,7 +89,7 @@ function createAddHook( hooks, storeKey ) {
7689
// we're adding would come after the current callback, there's no
7790
// problem; otherwise we need to increase the execution index of
7891
// any other runs by 1 to account for the added element.
79-
hooksStore.__current.forEach( ( hookInfo ) => {
92+
hooksStore.__current.forEach( ( hookInfo: HookInfo ) => {
8093
if (
8194
hookInfo.name === hookName &&
8295
hookInfo.currentIndex >= i

packages/hooks/src/createCurrentHook.js renamed to packages/hooks/src/createCurrentHook.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
1+
/**
2+
* Internal dependencies
3+
*/
4+
import type { Hooks, StoreKey } from './types';
5+
16
/**
27
* Returns a function which, when invoked, will return the name of the
38
* currently running hook, or `null` if no hook of the given type is currently
49
* running.
510
*
6-
* @param {import('.').Hooks} hooks Hooks instance.
7-
* @param {import('.').StoreKey} storeKey
11+
* @param hooks Hooks instance.
12+
* @param storeKey
813
*
9-
* @return {() => string | null} Function that returns the current hook name or null.
14+
* @return Function that returns the current hook name or null.
1015
*/
11-
function createCurrentHook( hooks, storeKey ) {
16+
function createCurrentHook(
17+
hooks: Hooks,
18+
storeKey: StoreKey
19+
): () => string | null {
1220
return function currentHook() {
1321
const hooksStore = hooks[ storeKey ];
1422
const currentArray = Array.from( hooksStore.__current );

packages/hooks/src/createDidHook.js renamed to packages/hooks/src/createDidHook.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
/**
22
* Internal dependencies
33
*/
4-
import validateHookName from './validateHookName.js';
4+
import validateHookName from './validateHookName';
5+
import type { Hooks, StoreKey } from './types';
56

67
/**
7-
* @callback DidHook
88
*
99
* Returns the number of times an action has been fired.
1010
*
11-
* @param {string} hookName The hook name to check.
12-
*
13-
* @return {number | undefined} The number of times the hook has run.
1411
*/
12+
export type DidHook = (
13+
/**
14+
* The hook name to check.
15+
*/
16+
hookName: string
17+
) => number | undefined;
1518

1619
/**
1720
* Returns a function which, when invoked, will return the number of times a
1821
* hook has been called.
1922
*
20-
* @param {import('.').Hooks} hooks Hooks instance.
21-
* @param {import('.').StoreKey} storeKey
23+
* @param hooks Hooks instance.
24+
* @param storeKey
2225
*
23-
* @return {DidHook} Function that returns a hook's call count.
26+
* @return Function that returns a hook's call count.
2427
*/
25-
function createDidHook( hooks, storeKey ) {
28+
function createDidHook( hooks: Hooks, storeKey: StoreKey ): DidHook {
2629
return function didHook( hookName ) {
2730
const hooksStore = hooks[ storeKey ];
2831

packages/hooks/src/createDoingHook.js renamed to packages/hooks/src/createDoingHook.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
11
/**
2-
* @callback DoingHook
2+
* Internal dependencies
3+
*/
4+
import type { Hooks, StoreKey } from './types';
5+
6+
/**
37
* Returns whether a hook is currently being executed.
48
*
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.
99
*/
10+
export type DoingHook = (
11+
/**
12+
* The name of the hook to check for.
13+
* If omitted, will check for any hook being executed.
14+
*/ hookName?: string
15+
) => boolean;
1016

1117
/**
1218
* Returns a function which, when invoked, will return whether a hook is
1319
* currently being executed.
1420
*
15-
* @param {import('.').Hooks} hooks Hooks instance.
16-
* @param {import('.').StoreKey} storeKey
21+
* @param hooks Hooks instance.
22+
* @param storeKey
1723
*
18-
* @return {DoingHook} Function that returns whether a hook is currently
24+
* @return Function that returns whether a hook is currently
1925
* being executed.
2026
*/
21-
function createDoingHook( hooks, storeKey ) {
27+
function createDoingHook( hooks: Hooks, storeKey: StoreKey ): DoingHook {
2228
return function doingHook( hookName ) {
2329
const hooksStore = hooks[ storeKey ];
2430

packages/hooks/src/createHasHook.js renamed to packages/hooks/src/createHasHook.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,33 @@
11
/**
2-
* @callback HasHook
2+
* Internal dependencies
3+
*/
4+
import type { Hooks, StoreKey } from './types';
5+
/**
36
*
47
* 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.
118
*/
9+
export type HasHook = (
10+
/**
11+
* The name of the hook to check for.
12+
*/
13+
hookname: string,
14+
/**
15+
* The unique namespace identifying the callback in the form `vendor/plugin/function`.
16+
*/
17+
namespace?: string
18+
) => boolean;
19+
1220
/**
1321
* Returns a function which, when invoked, will return whether any handlers are
1422
* attached to a particular hook.
1523
*
16-
* @param {import('.').Hooks} hooks Hooks instance.
17-
* @param {import('.').StoreKey} storeKey
24+
* @param hooks Hooks instance.
25+
* @param storeKey
1826
*
19-
* @return {HasHook} Function that returns whether any handlers are
27+
* @return Function that returns whether any handlers are
2028
* attached to a particular hook and optional namespace.
2129
*/
22-
function createHasHook( hooks, storeKey ) {
30+
function createHasHook( hooks: Hooks, storeKey: StoreKey ): HasHook {
2331
return function hasHook( hookName, namespace ) {
2432
const hooksStore = hooks[ storeKey ];
2533

packages/hooks/src/createHooks.js renamed to packages/hooks/src/createHooks.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import createRunHook from './createRunHook';
88
import createCurrentHook from './createCurrentHook';
99
import createDoingHook from './createDoingHook';
1010
import createDidHook from './createDidHook';
11+
import type { Store } from './types';
1112

1213
/**
1314
* Internal class for constructing hooks. Use `createHooks()` function
@@ -17,12 +18,32 @@ import createDidHook from './createDidHook';
1718
* @private
1819
*/
1920
export class _Hooks {
21+
public actions: Store;
22+
public filters: Store;
23+
24+
public addAction: ReturnType< typeof createAddHook >;
25+
public addFilter: ReturnType< typeof createAddHook >;
26+
public removeAction: ReturnType< typeof createRemoveHook >;
27+
public removeFilter: ReturnType< typeof createRemoveHook >;
28+
public hasAction: ReturnType< typeof createHasHook >;
29+
public hasFilter: ReturnType< typeof createHasHook >;
30+
public removeAllActions: ReturnType< typeof createRemoveHook >;
31+
public removeAllFilters: ReturnType< typeof createRemoveHook >;
32+
public doAction: ReturnType< typeof createRunHook >;
33+
public doActionAsync: ReturnType< typeof createRunHook >;
34+
public applyFilters: ReturnType< typeof createRunHook >;
35+
public applyFiltersAsync: ReturnType< typeof createRunHook >;
36+
public currentAction: ReturnType< typeof createCurrentHook >;
37+
public currentFilter: ReturnType< typeof createCurrentHook >;
38+
public doingAction: ReturnType< typeof createDoingHook >;
39+
public doingFilter: ReturnType< typeof createDoingHook >;
40+
public didAction: ReturnType< typeof createDidHook >;
41+
public didFilter: ReturnType< typeof createDidHook >;
42+
2043
constructor() {
21-
/** @type {import('.').Store} actions */
2244
this.actions = Object.create( null );
2345
this.actions.__current = new Set();
2446

25-
/** @type {import('.').Store} filters */
2647
this.filters = Object.create( null );
2748
this.filters.__current = new Set();
2849

@@ -47,14 +68,14 @@ export class _Hooks {
4768
}
4869
}
4970

50-
/** @typedef {_Hooks} Hooks */
71+
export type Hooks = _Hooks;
5172

5273
/**
5374
* Returns an instance of the hooks object.
5475
*
55-
* @return {Hooks} A Hooks instance.
76+
* @return A Hooks instance.
5677
*/
57-
function createHooks() {
78+
function createHooks(): Hooks {
5879
return new _Hooks();
5980
}
6081

packages/hooks/src/createRemoveHook.js renamed to packages/hooks/src/createRemoveHook.ts

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,42 @@
11
/**
22
* Internal dependencies
33
*/
4-
import validateNamespace from './validateNamespace.js';
5-
import validateHookName from './validateHookName.js';
4+
import validateNamespace from './validateNamespace';
5+
import validateHookName from './validateHookName';
6+
import type { Hooks, StoreKey } from './types';
67

78
/**
8-
* @callback RemoveHook
99
* Removes the specified callback (or all callbacks) from the hook with a given hookName
1010
* 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.
1711
*/
12+
export type RemoveHook = (
13+
/**
14+
* The name of the hook to modify.
15+
*/
16+
hookName: string,
17+
/**
18+
* The unique namespace identifying the callback in the form `vendor/plugin/function`.
19+
*/
20+
namespace: string
21+
) => number | undefined;
1822

1923
/**
2024
* Returns a function which, when invoked, will remove a specified hook or all
2125
* hooks by the given name.
2226
*
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.
27+
* @param hooks Hooks instance.
28+
* @param storeKey
29+
* @param [removeAll=false] Whether to remove all callbacks for a hookName,
30+
* without regard to namespace. Used to create
31+
* `removeAll*` functions.
2832
*
29-
* @return {RemoveHook} Function that removes hooks.
33+
* @return Function that removes hooks.
3034
*/
31-
function createRemoveHook( hooks, storeKey, removeAll = false ) {
35+
function createRemoveHook(
36+
hooks: Hooks,
37+
storeKey: StoreKey,
38+
removeAll: boolean = false
39+
): RemoveHook {
3240
return function removeHook( hookName, namespace ) {
3341
const hooksStore = hooks[ storeKey ];
3442

packages/hooks/src/createRunHook.js renamed to packages/hooks/src/createRunHook.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,31 @@
1+
/**
2+
* Internal dependencies
3+
*/
4+
import type { Hooks, StoreKey } from './types';
5+
6+
export type RunHook = (
7+
hookName: string,
8+
...args: unknown[]
9+
) => undefined | unknown;
10+
111
/**
212
* Returns a function which, when invoked, will execute all callbacks
313
* registered to a hook of the specified type, optionally returning the final
414
* value of the call chain.
515
*
6-
* @param {import('.').Hooks} hooks Hooks instance.
7-
* @param {import('.').StoreKey} storeKey
8-
* @param {boolean} returnFirstArg Whether each hook callback is expected to return its first argument.
9-
* @param {boolean} async Whether the hook callback should be run asynchronously
16+
* @param hooks Hooks instance.
17+
* @param storeKey
18+
* @param returnFirstArg Whether each hook callback is expected to return its first argument.
19+
* @param async Whether the hook callback should be run asynchronously
1020
*
11-
* @return {(hookName:string, ...args: unknown[]) => undefined|unknown} Function that runs hook callbacks.
21+
* @return Function that runs hook callbacks.
1222
*/
13-
function createRunHook( hooks, storeKey, returnFirstArg, async ) {
23+
function createRunHook(
24+
hooks: Hooks,
25+
storeKey: StoreKey,
26+
returnFirstArg: boolean,
27+
async: boolean
28+
): RunHook {
1429
return function runHook( hookName, ...args ) {
1530
const hooksStore = hooks[ storeKey ];
1631

0 commit comments

Comments
 (0)