Skip to content

Commit

Permalink
refactor: Drop close lifecycle methods in favor of overwriting the …
Browse files Browse the repository at this point in the history
…event listeners when necessary
  • Loading branch information
pojntfx committed Jan 2, 2024
1 parent 608d9f4 commit 17ddf74
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 50 deletions.
8 changes: 2 additions & 6 deletions ts/ltsrpc-example-tcp-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ await new Promise<void>((res, rej) => {
socket.on("error", rej);
});

const { remote, close } = linkTCPSocket(
const remote = linkTCPSocket(
socket,

{
Expand All @@ -49,11 +49,7 @@ const { remote, close } = linkTCPSocket(
(v) => v,
(v) => v
);
socket.on("close", () => {
close();

exit(0);
});
socket.on("close", () => exit(0));

console.log("Connected to", raddr);

Expand Down
4 changes: 1 addition & 3 deletions ts/ltsrpc-example-tcp-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const server = createServer(async (socket) => {
console.error("Client disconnected with error:", e.cause);
});

const { remote, close } = linkTCPSocket(
const remote = linkTCPSocket(
socket,

{
Expand Down Expand Up @@ -45,8 +45,6 @@ const server = createServer(async (socket) => {
(v) => v
);
socket.on("close", () => {
close();

clients--;

console.log(clients, "clients connected");
Expand Down
3 changes: 1 addition & 2 deletions ts/ltsrpc-example-websocket-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ await new Promise<void>((res, rej) => {
socket.addEventListener("error", rej);
});

const { remote, close } = linkWebSocket(
const remote = linkWebSocket(
socket,

{
Expand All @@ -39,7 +39,6 @@ const { remote, close } = linkWebSocket(
(v) => v,
(v) => v
);
socket.addEventListener("close", close);

console.log("Connected to", raddr);

Expand Down
4 changes: 1 addition & 3 deletions ts/ltsrpc-example-websocket-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ server.on("connection", async (socket) => {
console.error("Client disconnected with error:", e);
});

const { remote, close } = linkWebSocket(
const remote = linkWebSocket(
socket,

{
Expand Down Expand Up @@ -51,8 +51,6 @@ server.on("connection", async (socket) => {
(v) => v
);
socket.addEventListener("close", () => {
close();

clients--;

console.log(clients, "clients connected");
Expand Down
47 changes: 11 additions & 36 deletions ts/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,32 +41,25 @@ export interface IRequestResponseReader<T> {
}

class WebSocketRequestResponseReader<T> implements IRequestResponseReader<T> {
private socket: WebSocket;

private handler: (event: MessageEvent<string | Buffer>) => any;

private requestListener?: (message: T) => void;

private responseListener?: (message: T) => void;

constructor(socket: WebSocket, parse: (text: string) => any) {
this.socket = socket;

this.handler = (event: MessageEvent<string | Buffer>) => {
socket.addEventListener(
"message",
(event: MessageEvent<string | Buffer>) => {
const msg = unmarshalMessage<T>(event.data as string, parse);

if (msg.request) {
this.requestListener?.(msg.request);
} else if (msg.response) {
this.responseListener?.(msg.response);
}
};
}
);
}

open = () => this.socket.addEventListener("message", this.handler);

close = () => this.socket.removeEventListener("message", this.handler);

on = (
event: "request" | "response",
listener: (message: T) => void
Expand All @@ -82,32 +75,22 @@ class WebSocketRequestResponseReader<T> implements IRequestResponseReader<T> {
}

class TCPSocketRequestResponseReader<T> implements IRequestResponseReader<T> {
private socket: Socket;

private handler: (event: MessageEvent<string | Buffer>) => any;

private requestListener?: (message: T) => void;

private responseListener?: (message: T) => void;

constructor(socket: Socket, parse: (text: string) => any) {
this.socket = socket;

this.handler = (data: any) => {
socket.addListener("data", (data: any) => {
const msg = unmarshalMessage<T>(data.toString() as string, parse);

if (msg.request) {
this.requestListener?.(msg.request);
} else if (msg.response) {
this.responseListener?.(msg.response);
}
};
});
}

open = () => this.socket.addListener("data", this.handler);

close = () => this.socket.removeListener("data", this.handler);

on = (
event: "request" | "response",
listener: (message: T) => void
Expand Down Expand Up @@ -251,10 +234,8 @@ export const linkWebSocket = <L extends ILocal, R extends IRemote, T>(
socket,
parse
);
requestResponseReceiver.open();

return {
remote: linkMessage(
return linkMessage(
local,
remote,

Expand All @@ -267,9 +248,7 @@ export const linkWebSocket = <L extends ILocal, R extends IRemote, T>(

stringifyNested,
parseNested
),
close: requestResponseReceiver.close,
};
);
};

/**
Expand Down Expand Up @@ -299,10 +278,8 @@ export const linkTCPSocket = <L extends ILocal, R extends IRemote, T>(
socket,
parse
);
requestResponseReceiver.open();

return {
remote: linkMessage(
return linkMessage(
local,
remote,

Expand All @@ -315,7 +292,5 @@ export const linkTCPSocket = <L extends ILocal, R extends IRemote, T>(

stringifyNested,
parseNested
),
close: requestResponseReceiver.close,
};
);
};

0 comments on commit 17ddf74

Please sign in to comment.