Skip to content

Commit

Permalink
Fix typing for loading hooks + HookManager
Browse files Browse the repository at this point in the history
  • Loading branch information
YummYume committed Jun 15, 2024
1 parent 29c4798 commit 303bbe6
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
4 changes: 2 additions & 2 deletions assets/dist/Component/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export type ComponentHooks = {
'response:error': (backendResponse: BackendResponse, controls: {
displayError: boolean;
}) => MaybePromise;
'loading.state.started': (element: HTMLElement, request: BackendRequest) => MaybePromise;
'loading.state.finished': (element: HTMLElement) => MaybePromise;
'loading.state:started': (element: HTMLElement, request: BackendRequest) => MaybePromise;
'loading.state:finished': (element: HTMLElement) => MaybePromise;
'model:set': (model: string, value: any, component: Component) => MaybePromise;
};
export type ComponentHookName = keyof ComponentHooks;
Expand Down
7 changes: 4 additions & 3 deletions assets/dist/HookManager.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { ComponentHookName, ComponentHookCallback } from './Component';
export default class {
private hooks;
register(hookName: string, callback: (...args: any[]) => void): void;
unregister(hookName: string, callback: (...args: any[]) => void): void;
triggerHook(hookName: string, ...args: any[]): void;
register<T extends string | ComponentHookName = ComponentHookName>(hookName: T, callback: ComponentHookCallback<T>): void;
unregister<T extends string | ComponentHookName = ComponentHookName>(hookName: T, callback: ComponentHookCallback<T>): void;
triggerHook<T extends string | ComponentHookName = ComponentHookName>(hookName: T, ...args: Parameters<ComponentHookCallback<T>>): void;
}
4 changes: 2 additions & 2 deletions assets/src/Component/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ export type ComponentHooks = {
'request:started': (requestConfig: any) => MaybePromise,
'render:finished': (component: Component) => MaybePromise,
'response:error': (backendResponse: BackendResponse, controls: { displayError: boolean }) => MaybePromise,
'loading.state.started': (element: HTMLElement, request: BackendRequest) => MaybePromise,
'loading.state.finished': (element: HTMLElement) => MaybePromise,
'loading.state:started': (element: HTMLElement, request: BackendRequest) => MaybePromise,
'loading.state:finished': (element: HTMLElement) => MaybePromise,
'model:set': (model: string, value: any, component: Component) => MaybePromise,
};

Expand Down
19 changes: 15 additions & 4 deletions assets/src/HookManager.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import type { ComponentHookName, ComponentHookCallback } from './Component';

export default class {
private hooks: Map<string, Array<(...args: any[]) => void>> = new Map();
private hooks: Map<ComponentHookName | string, Array<(...args: any[]) => void>> = new Map();

register(hookName: string, callback: (...args: any[]) => void): void {
register<T extends string | ComponentHookName = ComponentHookName>(
hookName: T,
callback: ComponentHookCallback<T>
): void {
const hooks = this.hooks.get(hookName) || [];
hooks.push(callback);
this.hooks.set(hookName, hooks);
}

unregister(hookName: string, callback: (...args: any[]) => void): void {
unregister<T extends string | ComponentHookName = ComponentHookName>(
hookName: T,
callback: ComponentHookCallback<T>
): void {
const hooks = this.hooks.get(hookName) || [];
const index = hooks.indexOf(callback);
if (index === -1) {
Expand All @@ -18,7 +26,10 @@ export default class {
this.hooks.set(hookName, hooks);
}

triggerHook(hookName: string, ...args: any[]): void {
triggerHook<T extends string | ComponentHookName = ComponentHookName>(
hookName: T,
...args: Parameters<ComponentHookCallback<T>>
): void {
const hooks = this.hooks.get(hookName) || [];
hooks.forEach((callback) => callback(...args));
}
Expand Down

0 comments on commit 303bbe6

Please sign in to comment.