@@ -80,19 +80,27 @@ interface DeclareFunctionResponse extends ProtocolMessage {
80
80
81
81
export class Protocol {
82
82
private moduleId : string ;
83
- private functions : FnMappings ;
84
- private hookListeners : HookMappings ;
85
- private requests : FnMappings ;
83
+ private functions : FnMappings = { } ;
84
+ private hookListeners : HookMappings = { } ;
85
+ private requests : FnMappings = { } ;
86
86
87
87
private connection : GothamConnection ;
88
88
89
89
constructor ( connection : GothamConnection ) {
90
90
this . connection = connection ;
91
+ }
92
+
93
+ async connect ( ) {
94
+ await this . connection . setupConnection ( ) ;
91
95
this . connection . setupDataListener ( ( data ) => {
92
96
this . parseResponse ( data ) ;
93
97
} ) ;
94
98
}
95
99
100
+ async close ( ) {
101
+ await this . connection . closeConnection ( ) ;
102
+ }
103
+
96
104
async sendRequest ( obj : any ) {
97
105
await this . connection . send ( obj ) ;
98
106
return new Promise ( ( resolve , reject ) => {
@@ -123,9 +131,9 @@ export class Protocol {
123
131
124
132
registerHook ( hook : string , cb : Function ) : RegisterHookRequest {
125
133
if ( this . hookListeners [ hook ] ) {
126
- this . hookListeners [ hook ] = [ cb ] ;
127
- } else {
128
134
this . hookListeners [ hook ] . push ( cb ) ;
135
+ } else {
136
+ this . hookListeners [ hook ] = [ cb ] ;
129
137
}
130
138
return {
131
139
requestId : this . generateRequestId ( ) ,
@@ -155,7 +163,7 @@ export class Protocol {
155
163
return {
156
164
requestId : this . generateRequestId ( ) ,
157
165
type : 'functionCall' ,
158
- function : `${ this . moduleId } . ${ functionName } ` ,
166
+ function : `${ functionName } ` ,
159
167
arguments : args
160
168
}
161
169
}
@@ -212,10 +220,17 @@ export class Protocol {
212
220
}
213
221
214
222
async parseHookTriggered ( obj : TriggerHookRequest ) {
215
- if ( this . hookListeners [ obj . hook ] ) {
216
- for ( const listener of this . hookListeners [ obj . hook ] ) {
217
- listener ( ) ;
223
+ if ( obj . hook ) {
224
+ // Hook triggered by another module.
225
+ if ( this . hookListeners [ obj . hook ] ) {
226
+ for ( const listener of this . hookListeners [ obj . hook ] ) {
227
+ listener ( ) ;
228
+ }
218
229
}
230
+ return true ;
231
+ } else {
232
+ // This moddule triggered the hook. (TODO: Should the callback still be called?)
233
+ return true ;
219
234
}
220
235
}
221
236
}
0 commit comments