File tree Expand file tree Collapse file tree 4 files changed +33
-25
lines changed Expand file tree Collapse file tree 4 files changed +33
-25
lines changed Original file line number Diff line number Diff line change 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' ;
3
3
4
4
type Hook = 'PostModelActionHook' | 'PreCommitHook' ;
5
5
6
6
export function EnableHook ( hook : Hook ) : ( target : any , propertyKey : string , descriptor : PropertyDescriptor ) => void {
7
7
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
+ }
15
18
} ;
16
19
}
Original file line number Diff line number Diff line change 1
1
import { Constructable } from '../app.type.js' ;
2
2
import { PostModelActionCallback , PostModelActionHook } from '../functions/hook/post-model-action.hook.js' ;
3
3
import { PreCommitCallback , PreCommitHook } from '../functions/hook/pre-commit.hook.js' ;
4
- import { Container } from './container.js' ;
5
4
6
5
export function Module ( {
7
6
imports = [ ] ,
@@ -14,23 +13,11 @@ export function Module({
14
13
} ) : ( constructor : any ) => void {
15
14
return function ( constructor : any ) {
16
15
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 } ) ;
24
17
}
25
18
26
19
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 } ) ;
34
21
}
35
22
} ;
36
23
}
Original file line number Diff line number Diff line change @@ -6,6 +6,8 @@ import { AHook } from './hook.abstract.js';
6
6
export type PostModelActionCallback = ( output : ActionOutputs ) => Promise < ActionOutputs > ;
7
7
8
8
export class PostModelActionHook extends AHook {
9
+ private static instance : PostModelActionHook ;
10
+
9
11
private readonly callbacks : { [ key : string ] : PostModelActionCallback [ ] } = { } ;
10
12
11
13
private isInstanceOfModelAction ( instance : any ) : instance is IModelAction {
@@ -23,6 +25,13 @@ export class PostModelActionHook extends AHook {
23
25
}
24
26
}
25
27
28
+ static getInstance ( ) : PostModelActionHook {
29
+ if ( ! this . instance ) {
30
+ this . instance = new PostModelActionHook ( ) ;
31
+ }
32
+ return this . instance ;
33
+ }
34
+
26
35
override registrar ( constructor : Constructable < unknown > , propertyKey : string , descriptor : PropertyDescriptor ) : void {
27
36
if ( ! this . isInstanceOfModelAction ( constructor . prototype ) ) {
28
37
throw new Error ( 'PostModelActionHook can only be used with ModelAction!' ) ;
Original file line number Diff line number Diff line change @@ -5,6 +5,8 @@ import { AHook } from './hook.abstract.js';
5
5
export type PreCommitCallback = ( ...args : any [ ] ) => Promise < void > ;
6
6
7
7
export class PreCommitHook extends AHook {
8
+ private static instance : PreCommitHook ;
9
+
8
10
private readonly callbacks : PreCommitCallback [ ] = [ ] ;
9
11
10
12
override generateCallbacks ( ) : void {
@@ -15,6 +17,13 @@ export class PreCommitHook extends AHook {
15
17
}
16
18
}
17
19
20
+ static getInstance ( ) : PreCommitHook {
21
+ if ( ! this . instance ) {
22
+ this . instance = new PreCommitHook ( ) ;
23
+ }
24
+ return this . instance ;
25
+ }
26
+
18
27
override registrar ( constructor : Constructable < unknown > , propertyKey : string , descriptor : PropertyDescriptor ) : void {
19
28
const originalMethod = descriptor . value ;
20
29
const self = this ; // eslint-disable-line @typescript-eslint/no-this-alias
@@ -36,7 +45,7 @@ export class PreCommitHookFactory {
36
45
37
46
static async create ( ) : Promise < PreCommitHook > {
38
47
if ( ! this . instance ) {
39
- this . instance = new PreCommitHook ( ) ;
48
+ this . instance = PreCommitHook . getInstance ( ) ;
40
49
}
41
50
return this . instance ;
42
51
}
You can’t perform that action at this time.
0 commit comments