1
- import { constants } from ".. /constants" ;
1
+ import { constants } from "./constants" ;
2
2
import { GothamConnection } from './GothamConnection' ;
3
-
4
- export interface Dependency {
5
- [ type : string ] : string ;
6
- }
7
-
8
- export interface FnArgs {
9
- [ type : string ] : any ;
10
- }
11
-
12
- interface FnMappings {
13
- [ type : string ] : Function ;
14
- }
15
-
16
- interface HookMappings {
17
- [ type : string ] : Function [ ]
18
- }
19
-
20
- interface ProtocolMessage {
21
- requestId : string ;
22
- type : string ;
23
- }
24
-
25
- interface RegisterModuleRequest extends ProtocolMessage {
26
- type : 'moduleRegistration' ;
27
- moduleId : string ;
28
- version : string ;
29
- dependencies : Dependency ;
30
- }
31
-
32
- interface RegisterModuleResponse extends ProtocolMessage {
33
- type : 'moduleRegistered' ;
34
- }
35
-
36
- interface FunctionCallRequest extends ProtocolMessage {
37
- type : 'functionCall' ;
38
- function : string ;
39
- arguments : FnArgs ;
40
- }
41
-
42
- interface FunctionCallResponse extends ProtocolMessage {
43
- type : 'functionResponse' ;
44
- data : FnArgs ;
45
- }
46
-
47
- interface RegisterHookRequest extends ProtocolMessage {
48
- type : 'registerHook' ;
49
- hook : string ;
50
- }
51
-
52
- interface ListenHookResponse extends ProtocolMessage {
53
- type : 'hookRegistered' ;
54
- }
55
-
56
- interface TriggerHookRequest extends ProtocolMessage {
57
- type : 'triggerHook' ;
58
- hook : string ;
59
- }
60
-
61
- interface TriggerHookResponse extends ProtocolMessage {
62
- type : 'hookTriggered' ;
63
- hook : string ;
64
- }
65
-
66
- interface CallHookResponse extends ProtocolMessage {
67
- type : 'hookTriggered' ;
68
- hook : string ;
69
- }
70
-
71
- interface DeclareFunctionRequest extends ProtocolMessage {
72
- type : 'declareFunction' ;
73
- function : string ;
74
- }
75
-
76
- interface DeclareFunctionResponse extends ProtocolMessage {
77
- type : 'functionDeclared' ;
78
- function : string ;
79
- }
3
+ import { FnMappings , HookMappings , GothamRequest , Dependency , RegisterModuleRequest , RegisterHookRequest , TriggerHookRequest , DeclareFunctionRequest , FnArgs , FunctionCallRequest , GothamResponse , FunctionCallResponse } from "./types/protocol" ;
80
4
81
5
export class Protocol {
6
+ private registered : boolean ;
82
7
private moduleId ! : string ;
83
8
private functions : FnMappings = { } ;
84
9
private hookListeners : HookMappings = { } ;
@@ -88,6 +13,7 @@ export class Protocol {
88
13
89
14
constructor ( connection : GothamConnection ) {
90
15
this . connection = connection ;
16
+ this . registered = false ;
91
17
}
92
18
93
19
async connect ( ) {
@@ -101,7 +27,15 @@ export class Protocol {
101
27
await this . connection . closeConnection ( ) ;
102
28
}
103
29
104
- async sendRequest ( obj : any ) {
30
+ async sendRequest ( obj : GothamRequest ) {
31
+ if ( obj . type !== 'moduleRegistration' && ! this . registered ) {
32
+ throw new Error ( "Can't send request before module has been registered" ) ;
33
+ }
34
+
35
+ if ( obj . type === 'moduleRegistration' && this . registered ) {
36
+ throw new Error ( "Module already registered" ) ;
37
+ }
38
+
105
39
await this . connection . send ( obj ) ;
106
40
return new Promise ( ( resolve , reject ) => {
107
41
this . requests [ obj . requestId ] = ( res : Object ) => {
@@ -182,9 +116,10 @@ export class Protocol {
182
116
}
183
117
}
184
118
185
- async parseResponse ( obj : ProtocolMessage ) {
119
+ async parseResponse ( obj : GothamResponse ) {
186
120
let res ;
187
121
if ( obj . type === constants . responseType . moduleRegistered ) {
122
+ this . registered = true ;
188
123
res = true ;
189
124
} else if ( obj . type === constants . requestType . functionCall ) {
190
125
res = await this . parseFunctionCall ( obj as FunctionCallRequest ) ;
0 commit comments