Skip to content

Commit 759a0bf

Browse files
committed
patch(fix): octo hook and module registrations are sync.
1 parent 28ae1e8 commit 759a0bf

File tree

4 files changed

+33
-25
lines changed

4 files changed

+33
-25
lines changed
Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1-
import { AHook } from '../functions/hook/hook.abstract.js';
2-
import { Container } from './container.js';
1+
import { PostModelActionHook } from '../functions/hook/post-model-action.hook.js';
2+
import { PreCommitHook } from '../functions/hook/pre-commit.hook.js';
33

44
type Hook = 'PostModelActionHook' | 'PreCommitHook';
55

66
export function EnableHook(hook: Hook): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void {
77
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
8-
Container.get<AHook>(hook)
9-
.then((aHook) => {
10-
aHook.registrar(target.constructor, propertyKey, descriptor);
11-
})
12-
.catch((error) => {
13-
console.error(error);
14-
});
8+
switch (hook) {
9+
case 'PostModelActionHook':
10+
PostModelActionHook.getInstance().registrar(target.constructor, propertyKey, descriptor);
11+
break;
12+
case 'PreCommitHook':
13+
PreCommitHook.getInstance().registrar(target.constructor, propertyKey, descriptor);
14+
break;
15+
default:
16+
throw new Error('Invalid hook!');
17+
}
1518
};
1619
}
Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Constructable } from '../app.type.js';
22
import { PostModelActionCallback, PostModelActionHook } from '../functions/hook/post-model-action.hook.js';
33
import { PreCommitCallback, PreCommitHook } from '../functions/hook/pre-commit.hook.js';
4-
import { Container } from './container.js';
54

65
export function Module({
76
imports = [],
@@ -14,23 +13,11 @@ export function Module({
1413
}): (constructor: any) => void {
1514
return function (constructor: any) {
1615
if (postModelActionHandles.length > 0) {
17-
Container.get(PostModelActionHook)
18-
.then((aHook) => {
19-
aHook.register(constructor.name, { imports, postModelActionHandles });
20-
})
21-
.catch((error) => {
22-
console.error(error);
23-
});
16+
PostModelActionHook.getInstance().register(constructor.name, { imports, postModelActionHandles });
2417
}
2518

2619
if (preCommitHandles.length > 0) {
27-
Container.get(PreCommitHook)
28-
.then((aHook) => {
29-
aHook.register(constructor.name, { imports, preCommitHandles });
30-
})
31-
.catch((error) => {
32-
console.error(error);
33-
});
20+
PreCommitHook.getInstance().register(constructor.name, { imports, preCommitHandles });
3421
}
3522
};
3623
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { AHook } from './hook.abstract.js';
66
export type PostModelActionCallback = (output: ActionOutputs) => Promise<ActionOutputs>;
77

88
export class PostModelActionHook extends AHook {
9+
private static instance: PostModelActionHook;
10+
911
private readonly callbacks: { [key: string]: PostModelActionCallback[] } = {};
1012

1113
private isInstanceOfModelAction(instance: any): instance is IModelAction {
@@ -23,6 +25,13 @@ export class PostModelActionHook extends AHook {
2325
}
2426
}
2527

28+
static getInstance(): PostModelActionHook {
29+
if (!this.instance) {
30+
this.instance = new PostModelActionHook();
31+
}
32+
return this.instance;
33+
}
34+
2635
override registrar(constructor: Constructable<unknown>, propertyKey: string, descriptor: PropertyDescriptor): void {
2736
if (!this.isInstanceOfModelAction(constructor.prototype)) {
2837
throw new Error('PostModelActionHook can only be used with ModelAction!');

packages/octo/src/functions/hook/pre-commit.hook.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { AHook } from './hook.abstract.js';
55
export type PreCommitCallback = (...args: any[]) => Promise<void>;
66

77
export class PreCommitHook extends AHook {
8+
private static instance: PreCommitHook;
9+
810
private readonly callbacks: PreCommitCallback[] = [];
911

1012
override generateCallbacks(): void {
@@ -15,6 +17,13 @@ export class PreCommitHook extends AHook {
1517
}
1618
}
1719

20+
static getInstance(): PreCommitHook {
21+
if (!this.instance) {
22+
this.instance = new PreCommitHook();
23+
}
24+
return this.instance;
25+
}
26+
1827
override registrar(constructor: Constructable<unknown>, propertyKey: string, descriptor: PropertyDescriptor): void {
1928
const originalMethod = descriptor.value;
2029
const self = this; // eslint-disable-line @typescript-eslint/no-this-alias
@@ -36,7 +45,7 @@ export class PreCommitHookFactory {
3645

3746
static async create(): Promise<PreCommitHook> {
3847
if (!this.instance) {
39-
this.instance = new PreCommitHook();
48+
this.instance = PreCommitHook.getInstance();
4049
}
4150
return this.instance;
4251
}

0 commit comments

Comments
 (0)