@@ -13,10 +13,12 @@ import SocketConnection from './connection/socket-connection';
13
13
export default class GothamModule {
14
14
15
15
private protocol : BaseProtocol ;
16
+ private moduleId ?: string ;
16
17
private connection : BaseConnection ;
17
18
private requests : { [ type : string ] : Function } = { } ;
18
19
private functions : { [ type : string ] : Function } = { } ;
19
20
private hookListeners : { [ type : string ] : Function [ ] } = { } ;
21
+ private messagBuffer ?: Buffer ;
20
22
private registered = false ;
21
23
22
24
constructor ( connection : BaseConnection , protocol : BaseProtocol ) {
@@ -34,6 +36,7 @@ export default class GothamModule {
34
36
version : string ,
35
37
deps : { [ type : string ] : string } = { }
36
38
) {
39
+ this . moduleId = moduleId ;
37
40
// Setup Connection only when initialize called?
38
41
await this . connection . setupConnection ( ) ;
39
42
this . connection . setOnDataListener ( ( data ) => {
@@ -85,17 +88,22 @@ export default class GothamModule {
85
88
}
86
89
87
90
private async sendRequest ( request : GothamMessage ) {
88
- if ( request . type !== RequestTypes . ModuleRegistration && ! this . registered ) {
89
- throw new Error ( 'Can\'t send request before module has been registered' ) ;
90
- }
91
-
92
- if ( request . type === 'moduleRegistration' && this . registered ) {
91
+ if ( request . type === RequestTypes . ModuleRegistration && this . registered ) {
93
92
throw new Error ( 'Module already registered' ) ;
94
93
}
95
94
96
- await this . connection . send (
97
- this . protocol . encode ( request )
98
- ) ;
95
+ const encoded = this . protocol . encode ( request ) ;
96
+ if ( this . registered || request . type === RequestTypes . ModuleRegistration ) {
97
+ await this . connection . send (
98
+ encoded
99
+ ) ;
100
+ } else {
101
+ if ( this . messagBuffer ) {
102
+ this . messagBuffer = Buffer . concat ( [ this . messagBuffer , encoded ] )
103
+ } else {
104
+ this . messagBuffer = encoded ;
105
+ }
106
+ }
99
107
100
108
return new Promise ( ( resolve , reject ) => {
101
109
this . requests [ request . requestId ] = ( response : any ) => {
@@ -113,7 +121,6 @@ export default class GothamModule {
113
121
let value ;
114
122
switch ( response . type ) {
115
123
case ResponseTypes . ModuleRegistered : {
116
- this . registered = true ;
117
124
value = true ;
118
125
break ;
119
126
}
@@ -159,7 +166,7 @@ export default class GothamModule {
159
166
}
160
167
this . sendRequest ( {
161
168
requestId : request . requestId ,
162
- type : 'functionResponse' ,
169
+ type : ResponseTypes . FunctionResponse ,
163
170
data : res || { }
164
171
} ) ;
165
172
return true ;
@@ -172,7 +179,12 @@ export default class GothamModule {
172
179
private async executeHookTriggered ( request : TriggerHookRequest ) {
173
180
if ( request . hook ) {
174
181
// Hook triggered by another module.
175
- if ( this . hookListeners [ request . hook ] ) {
182
+ if ( request . hook === `gotham.activated` ) {
183
+ this . registered = true ;
184
+ if ( this . messagBuffer ) {
185
+ this . connection . send ( this . messagBuffer ) ;
186
+ }
187
+ } else if ( this . hookListeners [ request . hook ] ) {
176
188
for ( const listener of this . hookListeners [ request . hook ] ) {
177
189
listener ( ) ;
178
190
}
0 commit comments