Skip to content

Commit 1dfa638

Browse files
committed
fix: 1. JSON Parse received response from socket
2. Method to close socket connection 3. Connect to socket only when initialize() is called on the module
1 parent 0b1c8a4 commit 1dfa638

File tree

3 files changed

+61
-26
lines changed

3 files changed

+61
-26
lines changed

src/main.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import { Protocol, Dependency, FnArgs } from "./protocol/protocol";
2-
import { GothamConnection } from './protocol/GothamConnection';
2+
import { GothamConnection, SocketConnection } from './protocol/GothamConnection';
3+
import { isMainThread } from 'worker_threads';
34

4-
export class Gotham {
5+
export class GothamModule {
56
moduleId: string;
67
protocol: Protocol
78
constructor(connection: GothamConnection) {
89
this.protocol = new Protocol(connection);
910
}
11+
1012
async initialize(moduleId: string, version: string, deps: Dependency) {
13+
// Setup Connection only when initialize called?
14+
await this.protocol.connect();
1115
return await this.protocol.sendRequest(
1216
this.protocol.initialize(moduleId, version, deps),
1317
);
@@ -36,4 +40,8 @@ export class Gotham {
3640
this.protocol.triggerHook(hook)
3741
);
3842
}
39-
}
43+
44+
async close() {
45+
return await this.protocol.close();
46+
}
47+
}

src/protocol/GothamConnection.ts

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,52 @@
1-
import net from 'net';
1+
import * as net from 'net';
22

33
type dataListenerFn = (data: any) => any;
44
export abstract class GothamConnection {
55
dataListener: dataListenerFn;
6-
constructor() {
7-
this.setupConnection();
8-
}
96

10-
abstract setupConnection();
11-
abstract setupDataListener(dataListener: dataListenerFn);
12-
abstract async send(message: object): Promise<void>;
7+
abstract async setupConnection();
8+
abstract async closeConnection();
9+
abstract setupDataListener(dataListener: dataListenerFn);
10+
abstract async send(message: object): Promise<void>;
1311
}
1412

1513

1614

1715
export class SocketConnection extends GothamConnection {
1816
client: net.Socket;
1917
sockPath: string;
20-
constructor(sockPath: string, dataListener: dataListenerFn) {
18+
constructor(sockPath: string) {
2119
super();
2220
this.sockPath = sockPath;
2321
}
2422

25-
setupConnection() {
26-
this.client = net.createConnection(this.sockPath);
27-
this.client.on('connect', () => {
28-
console.log(`Connected to ${this.sockPath}!`);
23+
async setupConnection() {
24+
return new Promise((resolve, reject) => {
25+
this.client = net.createConnection(this.sockPath);
26+
this.client.on('connect', () => {
27+
// console.log(`Connected to ${this.sockPath}!`);
28+
resolve();
29+
});
2930
});
3031
}
3132

33+
async closeConnection() {
34+
this.client.destroy();
35+
}
36+
3237
setupDataListener(dataListener: dataListenerFn) {
33-
this.dataListener = dataListener;
34-
this.client.on('data', dataListener);
38+
this.dataListener = (data: Buffer) => {
39+
const parsed = JSON.parse(data.toString('utf-8'));
40+
// console.log("Socket Received Data:");
41+
// console.log(parsed);
42+
dataListener(parsed);
43+
};
44+
this.client.on('data', this.dataListener);
3545
}
3646

3747
async send(message: object) {
48+
// console.log("Socker Sending data:");
49+
// console.log(message);
3850
this.client.write(JSON.stringify(message) + '\n');
3951
}
4052
}

src/protocol/protocol.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,27 @@ interface DeclareFunctionResponse extends ProtocolMessage {
8080

8181
export class Protocol {
8282
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 = {};
8686

8787
private connection: GothamConnection;
8888

8989
constructor(connection: GothamConnection) {
9090
this.connection = connection;
91+
}
92+
93+
async connect() {
94+
await this.connection.setupConnection();
9195
this.connection.setupDataListener((data) => {
9296
this.parseResponse(data);
9397
});
9498
}
9599

100+
async close() {
101+
await this.connection.closeConnection();
102+
}
103+
96104
async sendRequest(obj: any) {
97105
await this.connection.send(obj);
98106
return new Promise((resolve, reject) => {
@@ -123,9 +131,9 @@ export class Protocol {
123131

124132
registerHook(hook: string, cb: Function): RegisterHookRequest {
125133
if (this.hookListeners[hook]) {
126-
this.hookListeners[hook] = [cb];
127-
} else {
128134
this.hookListeners[hook].push(cb);
135+
} else {
136+
this.hookListeners[hook] = [cb];
129137
}
130138
return {
131139
requestId: this.generateRequestId(),
@@ -155,7 +163,7 @@ export class Protocol {
155163
return {
156164
requestId: this.generateRequestId(),
157165
type: 'functionCall',
158-
function: `${this.moduleId}.${functionName}`,
166+
function: `${functionName}`,
159167
arguments: args
160168
}
161169
}
@@ -212,10 +220,17 @@ export class Protocol {
212220
}
213221

214222
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+
}
218229
}
230+
return true;
231+
} else {
232+
// This moddule triggered the hook. (TODO: Should the callback still be called?)
233+
return true;
219234
}
220235
}
221236
}

0 commit comments

Comments
 (0)