Skip to content

Commit 9a57314

Browse files
committed
GP scripts and reconnecting WS on Node, fixes #21
1 parent 9e94ad3 commit 9a57314

File tree

2 files changed

+41
-33
lines changed

2 files changed

+41
-33
lines changed

.gitpod.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ vscode:
66
- denoland.vscode-deno
77

88
tasks:
9-
- init: "deno run --allow-env mod.ts"
9+
- init: "deno run --allow-env --allow-net mod.ts"
1010

1111
github:
1212
prebuilds:
13-
master: true
13+
master: true

src/ws.ts

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -59,25 +59,25 @@ export class TpyWs {
5959
constructor(
6060
tpyInstance: Tpy,
6161
deploymentID: string,
62-
reconnectionTimeout = 250,
62+
reconnectionTimeout = 250
6363
) {
6464
if (!tpyInstance || !(tpyInstance instanceof Tpy)) {
6565
throw new TpyError(
6666
"Missing or Invalid Required Parameter",
6767
parametersPrompt(
6868
!tpyInstance ? "missing" : "incompatible",
69-
"tpyInstance",
69+
"tpyInstance"
7070
),
7171
"tpyInstance",
72-
tpyInstance,
72+
tpyInstance
7373
);
7474
}
7575
if (!deploymentID) {
7676
throw new TpyError(
7777
"Missing or Invalid Required Parameter",
7878
parametersPrompt("missing", "deploymentID"),
7979
"deploymentID",
80-
deploymentID,
80+
deploymentID
8181
);
8282
}
8383
this.tpyClient = tpyInstance;
@@ -124,20 +124,20 @@ export class TpyWs {
124124
*/
125125
on<T extends unknown[]>(
126126
type: "message",
127-
callback: (data: Unpacked<PylonWebSocket.Response<T>>) => void,
127+
callback: (data: Unpacked<PylonWebSocket.Response<T>>) => void
128128
): EventEmitter;
129129
on<T extends unknown[]>(type: messageTypes, callback: unknown) {
130130
if (typeof callback != "function") {
131131
throw new TpyError(
132132
"Missing or Invalid Required Parameter",
133133
parametersPrompt("incompatible", "callback"),
134134
"callback",
135-
typeof callback,
135+
typeof callback
136136
);
137137
}
138138
return this.eventEmitter.on(
139139
type,
140-
<(data: Unpacked<PylonWebSocket.Response<T>>) => void> callback,
140+
<(data: Unpacked<PylonWebSocket.Response<T>>) => void>callback
141141
);
142142
}
143143

@@ -147,38 +147,46 @@ export class TpyWs {
147147
async connect() {
148148
if (!this.tryToConnect) return;
149149
this._websocket = new WebSocket(
150-
(await this.tpyClient.getDeployment(this.deploymentID)).workbench_url,
150+
(await this.tpyClient.getDeployment(this.deploymentID)).workbench_url
151151
);
152-
this._websocket.onopen = this.onOpen.bind(this);
153-
this._websocket.onclose = this.onClose.bind(this);
154-
this._websocket.onerror = this.onError.bind(this);
155-
this._websocket.onmessage = this.onMessage.bind(this);
156-
}
157152

158-
private onOpen(event: Event) {
159-
this.eventEmitter.emit("open", event);
160-
}
153+
this._websocket.onopen = ((event: Event) => {
154+
this.eventEmitter.emit("open", event);
155+
}).bind(this);
161156

162-
private onError(event: Event | ErrorEvent) {
163-
if (!this.websocket) return;
164-
this.eventEmitter.emit("error", event);
157+
this._websocket.onclose = ((event: CloseEvent) => {
158+
this.eventEmitter.emit("close", event);
159+
this.timedReconnect();
165160

166-
try {
167-
this.websocket.close();
168-
// deno-lint-ignore no-empty
169-
} catch {}
161+
// (async () => await this.timedReconnect())();
162+
}).bind(this);
170163

171-
setTimeout(() => {
172-
this.connect();
173-
}, this.reconnectionTimout);
174-
}
164+
this._websocket.onerror = ((event: Event | ErrorEvent) => {
165+
if (!this.websocket) return;
166+
this.eventEmitter.emit("error", event);
167+
168+
try {
169+
this.websocket.close();
170+
// deno-lint-ignore no-empty
171+
} catch {}
172+
this.timedReconnect();
173+
// (async () => await this.timedReconnect())();
174+
}).bind(this);
175175

176-
private onClose(event: CloseEvent) {
177-
this.eventEmitter.emit("close", event);
176+
this._websocket.onmessage = ((event: MessageEvent) => {
177+
this.eventEmitter.emit("message", JSON.parse(String(event.data))[0]);
178+
}).bind(this);
178179
}
179180

180-
private onMessage(event: MessageEvent) {
181-
this.eventEmitter.emit("message", JSON.parse(String(event.data))[0]);
181+
timedReconnect() {
182+
// Check once to spare resources
183+
if (!this.tryToConnect) return;
184+
setTimeout(() => {
185+
(async () => {
186+
// Check again to ensure it hasn't changed
187+
if (this.tryToConnect) await this.connect();
188+
})();
189+
}, this.reconnectionTimout);
182190
}
183191

184192
/**

0 commit comments

Comments
 (0)