Skip to content

Commit

Permalink
Allow TachyonClient send client to get send result
Browse files Browse the repository at this point in the history
This will be useful for the procedure to catchup updates after
prolonged server disconnect, we want to push more messages down
the socket only when we know that socket is drained to not buffer
in multiple places and blow up memory.
  • Loading branch information
p2004a committed Jan 3, 2025
1 parent 04ed058 commit 9b0e64a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ async function main(argv: string[]) {
return;
}
if (msg.type == 'event') return;
client.send(await callTachyonAutohost(msg, autohost));
client.send(await callTachyonAutohost(msg, autohost)).catch(() => undefined);
});

try {
Expand Down
15 changes: 11 additions & 4 deletions src/tachyonClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export class TachyonClient extends TypedEmitter<{
headers: {
authorization: `Bearer ${accessToken}`,
},
perMessageDeflate: false,
});
this.ws = ws;

Expand Down Expand Up @@ -114,13 +115,19 @@ export class TachyonClient extends TypedEmitter<{
this.close();
}

public send(msg: TachyonCommand): void {
public send(msg: TachyonCommand): Promise<void> {
if (this.state !== ClientState.CONNECTED) {
throw new Error('Client is not connected');
}
if (this.ws) {
this.ws.send(JSON.stringify(msg));
}
return new Promise((resolve, reject) => {
this.ws!.send(JSON.stringify(msg), { binary: false }, (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}

public close() {
Expand Down
4 changes: 2 additions & 2 deletions src/tachyonTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ export interface TachyonAutohost {
* The interface that represents the functionality that the autohost can call on the server.
*/
export interface TachyonServer {
status(event: AutohostStatusEventData): void;
update(event: AutohostUpdateEventData): void;
status(event: AutohostStatusEventData): Promise<void>;
update(event: AutohostUpdateEventData): Promise<void>;
}

// Helper that works as a type guard to check if an element is included in an array.
Expand Down

0 comments on commit 9b0e64a

Please sign in to comment.