Skip to content

Commit c726281

Browse files
committed
patch(fix): hook callback standards.
- pre-* hooks to be called with same args as original method. - post-* hooks to be called with same returns type of original method.
1 parent 2ad3486 commit c726281

File tree

3 files changed

+10
-14
lines changed

3 files changed

+10
-14
lines changed
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { Constructable } from '../app.type.js';
22
import { AHook } from '../functions/hook/hook.abstract.js';
33

4-
export function EnableHook<T>(hook: {
4+
export function EnableHook(hook: {
55
new (): AHook;
6-
registrar: (constructor: Constructable<unknown>, propertyKey: string, descriptor: TypedPropertyDescriptor<T>) => void;
7-
}): (target: any, propertyKey: string, descriptor: TypedPropertyDescriptor<T>) => void {
8-
return function (target: any, propertyKey: string, descriptor: TypedPropertyDescriptor<T>) {
6+
registrar: (constructor: Constructable<unknown>, propertyKey: string, descriptor: PropertyDescriptor) => void;
7+
}): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void {
8+
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
99
hook.registrar(target.constructor, propertyKey, descriptor);
1010
};
1111
}

packages/octo/src/functions/hook/post-model-action-handle.hook.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { IModelAction } from '../../models/model-action.interface.js';
33
import { AHook } from './hook.abstract.js';
44

55
export type PostModelActionCallback = (output: ActionOutputs) => Promise<ActionOutputs>;
6-
type PostModelActionMethodSignature = (...args: any[]) => Promise<ActionOutputs>;
76

87
export class PostModelActionHandleHook extends AHook {
98
private static readonly callbacks: { [key: string]: PostModelActionCallback[] } = {};
@@ -23,7 +22,7 @@ export class PostModelActionHandleHook extends AHook {
2322
static override registrar(
2423
constructor: Constructable<unknown>,
2524
propertyKey: string,
26-
descriptor: TypedPropertyDescriptor<PostModelActionMethodSignature>,
25+
descriptor: PropertyDescriptor,
2726
): void {
2827
if (!this.isInstanceOfModelAction(constructor.prototype)) {
2928
throw new Error('PostModelActionHandleHook can only be used with ModelAction!');
Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import { Constructable } from '../../app.type.js';
2-
import { App } from '../../models/app/app.model.js';
3-
import { DiffMetadata } from '../diff/diff-metadata.js';
42
import { AHook } from './hook.abstract.js';
53

6-
export type PreCommitCallback = (modelTransaction: DiffMetadata[][]) => Promise<void>;
7-
type PreCommitMethodSignature = (app: App, modelTransaction: DiffMetadata[][]) => Promise<void>;
4+
export type PreCommitCallback = (...args: any[]) => Promise<void>;
85

96
export class PreCommitHandleHook extends AHook {
107
private static readonly callbacks: PreCommitCallback[] = [];
@@ -17,18 +14,18 @@ export class PreCommitHandleHook extends AHook {
1714
static override registrar(
1815
constructor: Constructable<unknown>,
1916
propertyKey: string,
20-
descriptor: TypedPropertyDescriptor<PreCommitMethodSignature>,
17+
descriptor: PropertyDescriptor,
2118
): void {
2219
const originalMethod = descriptor.value;
2320
const self = this; // eslint-disable-line @typescript-eslint/no-this-alias
2421

2522
// `self` here references PreCommitHandleHook, vs `this` references the original method.
26-
descriptor.value = async function (...args: [app: App, modelTransaction: DiffMetadata[][]]): Promise<void> {
23+
descriptor.value = async function (...args: any[]): Promise<any> {
2724
for (const callback of self.callbacks) {
28-
await callback(args[1]);
25+
await callback.apply(this, args);
2926
}
3027

31-
await originalMethod!.apply(this, args);
28+
return await originalMethod!.apply(this, args);
3229
};
3330
}
3431
}

0 commit comments

Comments
 (0)