Skip to content

Commit deeab19

Browse files
committed
fix: Make send request return promise, instead of callback interface
1 parent 467d4ea commit deeab19

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

src/main.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,14 @@
1-
/** Module Entrypoint */
1+
import { Protocol, Dependency } from "./protocol/protocol";
2+
3+
class Gotham {
4+
moduleId: string;
5+
protocol: Protocol
6+
constructor() {
7+
this.protocol = new Protocol();
8+
}
9+
async initialize(moduleId: string, version: string, deps: Dependency) {
10+
return await this.protocol.sendRequest(
11+
this.protocol.initialize(moduleId, version, deps),
12+
);
13+
}
14+
}

src/protocol/protocol.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { constants } from "../constants";
22
import { ConnectOpts } from 'net';
33
import { ConnectionOptions } from 'tls';
44

5-
interface Dependency {
5+
export interface Dependency {
66
[type: string]: string;
77
}
88

@@ -87,9 +87,17 @@ export class Protocol {
8787

8888
private connection: GothamConnection;
8989

90-
private async sendRequest(obj: any, cb: Function) {
90+
async sendRequest(obj: any) {
9191
await this.connection.send(obj);
92-
this.requests[obj.requestId] = cb;
92+
return new Promise((resolve, reject) => {
93+
this.requests[obj.requestId] = (res) => {
94+
if (res) {
95+
resolve(res);
96+
} else {
97+
reject(res);
98+
}
99+
}
100+
});
93101
}
94102

95103
private generateRequestId() {

0 commit comments

Comments
 (0)