Skip to content

Commit

Permalink
bug #1921 [Live] Fix typing for loading hooks + HookManager (YummYume)
Browse files Browse the repository at this point in the history
This PR was merged into the 2.x branch.

Discussion
----------

[Live] Fix typing for loading hooks + HookManager

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| Issues        |
| License       | MIT

See symfony/ux#1916 (comment) for context

Fixes the typo on the loading hooks and add the hook typing to the `HookManager` (was forgotten in my first pr)

Commits
-------

94285477 Fix typing for loading hooks + HookManager
  • Loading branch information
kbond committed Jun 26, 2024
2 parents 406b7f6 + 303bbe6 commit 61fdca3
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 61fdca3

Please sign in to comment.