File tree Expand file tree Collapse file tree 4 files changed +46
-14
lines changed Expand file tree Collapse file tree 4 files changed +46
-14
lines changed Original file line number Diff line number Diff line change 1
1
import { Protocol , Dependency , FnArgs } from "./protocol/protocol" ;
2
+ import { GothamConnection } from './protocol/GothamConnection' ;
2
3
3
4
export class Gotham {
4
5
moduleId : string ;
5
6
protocol : Protocol
6
- constructor ( ) {
7
- this . protocol = new Protocol ( ) ;
7
+ constructor ( connection : GothamConnection ) {
8
+ this . protocol = new Protocol ( connection ) ;
8
9
}
9
10
async initialize ( moduleId : string , version : string , deps : Dependency ) {
10
11
return await this . protocol . sendRequest (
Original file line number Diff line number Diff line change 1
- class GothamConnection {
1
+ import net from 'net' ;
2
+
3
+ type dataListenerFn = ( data : any ) => any ;
4
+ export abstract class GothamConnection {
5
+ dataListener : dataListenerFn ;
2
6
constructor ( ) {
3
- // Connect to socket
7
+ this . setupConnection ( ) ;
8
+ }
9
+
10
+ abstract setupConnection ( ) ;
11
+ abstract setupDataListener ( dataListener : dataListenerFn ) ;
12
+ abstract async send ( message : object ) : Promise < void > ;
13
+ }
14
+
15
+
16
+
17
+ export class SocketConnection extends GothamConnection {
18
+ client : net . Socket ;
19
+ sockPath : string ;
20
+ constructor ( sockPath : string , dataListener : dataListenerFn ) {
21
+ super ( ) ;
22
+ this . sockPath = sockPath ;
23
+ }
24
+
25
+ setupConnection ( ) {
26
+ this . client = net . createConnection ( this . sockPath ) ;
27
+ this . client . on ( 'connect' , ( ) => {
28
+ console . log ( `Connected to ${ this . sockPath } !` ) ;
29
+ } ) ;
4
30
}
5
31
6
- async send ( _message : object ) {
7
- // Send message over socket
32
+ setupDataListener ( dataListener : dataListenerFn ) {
33
+ this . dataListener = dataListener ;
34
+ this . client . on ( 'data' , dataListener ) ;
8
35
}
9
36
10
- async receive ( ) {
11
- // Receive a message from the socket
37
+ async send ( message : object ) {
38
+ this . client . write ( JSON . stringify ( message ) + '\n' ) ;
12
39
}
13
- }
40
+ }
Original file line number Diff line number Diff line change 1
1
import { constants } from "../constants" ;
2
- import { ConnectOpts } from 'net' ;
3
- import { ConnectionOptions } from 'tls' ;
2
+ import { GothamConnection } from './GothamConnection' ;
4
3
5
4
export interface Dependency {
6
5
[ type : string ] : string ;
@@ -87,6 +86,13 @@ export class Protocol {
87
86
88
87
private connection : GothamConnection ;
89
88
89
+ constructor ( connection : GothamConnection ) {
90
+ this . connection = connection ;
91
+ this . connection . setupDataListener ( ( data ) => {
92
+ this . parseResponse ( data ) ;
93
+ } ) ;
94
+ }
95
+
90
96
async sendRequest ( obj : any ) {
91
97
await this . connection . send ( obj ) ;
92
98
return new Promise ( ( resolve , reject ) => {
@@ -184,7 +190,7 @@ export class Protocol {
184
190
res = false ;
185
191
}
186
192
187
- // Call the callback proviced while sending the request
193
+ // Call the callback provided while sending the request
188
194
if ( this . requests [ obj . requestId ] ) {
189
195
this . requests [ obj . requestId ] ( res ) ;
190
196
delete this . requests [ obj . requestId ] ;
Original file line number Diff line number Diff line change 12
12
"forceConsistentCasingInFileNames" : true ,
13
13
"noFallthroughCasesInSwitch" : true ,
14
14
"noImplicitReturns" : true ,
15
- "noUnusedLocals" : true ,
16
- "noUnusedParameters" : true ,
17
15
"noImplicitAny" : false ,
18
16
"noImplicitThis" : false ,
19
17
"strictNullChecks" : false
You can’t perform that action at this time.
0 commit comments