1+ import type { LatestBlock } from "./BlockMonitor"
12import { Topics , eventBus } from "./EventBus.js"
23import type { Transaction } from "./Transaction.js"
34
45export enum TxmHookType {
56 All = "All" ,
67 TransactionStatusChanged = "TransactionStatusChanged" ,
8+ NewBlock = "NewBlock" ,
79}
810
9- export type TxmHookPayload = {
10- type : TxmHookType
11+ export type TxmTransactionStatusChangedHookPayload = {
12+ type : TxmHookType . TransactionStatusChanged
1113 transaction : Transaction
1214}
1315
14- export type TxmHookHandler = ( event : TxmHookPayload ) => void
16+ export type TxmNewBlockHookPayload = {
17+ type : TxmHookType . NewBlock
18+ block : LatestBlock
19+ }
20+
21+ export type TxmHookPayload = TxmTransactionStatusChangedHookPayload | TxmNewBlockHookPayload
22+
23+ export type TxmHookHandler < T extends TxmHookType = TxmHookType > = (
24+ event : Extract < TxmHookPayload , { type : T } > extends never ? TxmHookPayload : Extract < TxmHookPayload , { type : T } > ,
25+ ) => void
1526
1627/**
1728 * This module manages the hooks system. A hook in the transaction manager is a callback function that
@@ -24,31 +35,42 @@ export type TxmHookHandler = (event: TxmHookPayload) => void
2435 * - The callback function that executes when the event occurs.
2536 */
2637export class HookManager {
27- private hooks : Record < TxmHookType , TxmHookHandler [ ] >
38+ private hooks : {
39+ [ T in TxmHookType ] : TxmHookHandler < T > [ ]
40+ }
2841
2942 constructor ( ) {
3043 this . hooks = {
3144 [ TxmHookType . All ] : [ ] ,
3245 [ TxmHookType . TransactionStatusChanged ] : [ ] ,
46+ [ TxmHookType . NewBlock ] : [ ] ,
3347 }
3448 eventBus . on ( Topics . TransactionStatusChanged , this . onTransactionStatusChanged . bind ( this ) )
49+ eventBus . on ( Topics . NewBlock , this . onNewBlock . bind ( this ) )
3550 }
3651
37- public async addHook ( handler : TxmHookHandler , type : TxmHookType ) : Promise < void > {
52+ public async addHook < T extends TxmHookType > ( handler : TxmHookHandler < T > , type : T ) : Promise < void > {
3853 if ( ! this . hooks [ type ] ) {
3954 this . hooks [ type ] = [ ]
4055 }
4156 this . hooks [ type ] . push ( handler )
4257 }
4358
44- private async onTransactionStatusChanged ( payload : {
45- transaction : Transaction
46- } ) : Promise < void > {
47- this . hooks [ TxmHookType . TransactionStatusChanged ] . concat ( this . hooks [ TxmHookType . All ] ) . map ( ( h ) =>
59+ private async onTransactionStatusChanged ( payload : { transaction : Transaction } ) : Promise < void > {
60+ ; [ ...this . hooks [ TxmHookType . TransactionStatusChanged ] , ...this . hooks [ TxmHookType . All ] ] . forEach ( ( h ) =>
4861 h ( {
4962 type : TxmHookType . TransactionStatusChanged ,
5063 transaction : payload . transaction ,
5164 } ) ,
5265 )
5366 }
67+
68+ private async onNewBlock ( block : LatestBlock ) : Promise < void > {
69+ ; [ ...this . hooks [ TxmHookType . NewBlock ] , ...this . hooks [ TxmHookType . All ] ] . forEach ( ( h ) =>
70+ h ( {
71+ type : TxmHookType . NewBlock ,
72+ block,
73+ } ) ,
74+ )
75+ }
5476}
0 commit comments