Skip to content

Commit

Permalink
client ipc
Browse files Browse the repository at this point in the history
  • Loading branch information
duke79 committed Jul 5, 2024
1 parent 34ef7e9 commit af516bd
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 14 deletions.
16 changes: 13 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"typescript": "^3.9.10"
},
"dependencies": {
"react": "^18.3.1",
"socket.io-client": "^4.7.5"
}
}
4 changes: 4 additions & 0 deletions src/ipc/channels.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ export enum ClientToServerChannel {
export enum ServerToClientChannel {
SendMessage = 'serverToClient.sendMessage',
SendChatHistory = 'serverToClient.sendChatHistory',
progress = 'progress',
result = 'result',
error = 'error',
complete = 'complete'
}
25 changes: 16 additions & 9 deletions src/ipc/client-ipc.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import { ClientToServerChannel, ServerToClientChannel } from "./channels.enum";
import { ChannelBody } from "./channels.type";
import { Socket, io } from "socket.io-client";

export class ClientIPC {
private static _instance?: ClientIPC;
private _listeners: {
channel: ServerToClientChannel,
callback: (body: ChannelBody<ServerToClientChannel>) => void
}[];
private _socket?: Socket;

private constructor(
private sendMessage: (channel: ClientToServerChannel, body: ChannelBody<ClientToServerChannel>) => void,
private onMessage: (cb: (channel: ServerToClientChannel, body: ChannelBody<ServerToClientChannel>) => void) => void
url: string,
) {
this._listeners = [];
this.onMessage((channel: ServerToClientChannel, body: ChannelBody<ServerToClientChannel>) => {
console.log("Client IPC created", url);
const newSocket = io(url)
this._socket = newSocket;
this._socket.onAny((channel: ServerToClientChannel, body: ChannelBody<ServerToClientChannel>) => {
console.log({ dataOnServerSide: body });
this._listeners.forEach((listener) => {
if (listener.channel === channel) {
Expand All @@ -23,21 +27,24 @@ export class ClientIPC {
});
}

static getInstance(
sendMessage: (channel: ClientToServerChannel, body: ChannelBody<ClientToServerChannel>) => void,
onMessage: (cb: (channel: ServerToClientChannel, body: ChannelBody<ServerToClientChannel>) => void) => void
): ClientIPC {
static getInstance(url: string): ClientIPC {
console.log({ instance: ClientIPC._instance })
if (!ClientIPC._instance) {
ClientIPC._instance = new ClientIPC(sendMessage, onMessage);
ClientIPC._instance = new ClientIPC(url);
}
return ClientIPC._instance;
}

sendToServer<T extends ClientToServerChannel>(channel: T, body: ChannelBody<T>): void {
this.sendMessage(channel, body);
console.log({ dataOnClientSide: body });
this._socket?.emit(channel, body);
}

onServerMessage<T extends ServerToClientChannel>(channel: T, callback: (body: ChannelBody<T>) => void): void {
this._listeners.push({ channel, callback: callback as any });
}

disconnect() {
this._socket?.disconnect();
}
}
5 changes: 3 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
"strict": true,
// linter checks for common issues
"noImplicitReturns": true,
"noImplicitAny": false,
"noFallthroughCasesInSwitch": true,
// noUnused* overlap with @typescript-eslint/no-unused-vars, can disable if duplicative
"noUnusedLocals": true,
"noUnusedParameters": true,
"noUnusedLocals": false,
"noUnusedParameters": false,
// use Node's module resolution algorithm, instead of the legacy TS one
"moduleResolution": "node",
// transpile JSX to React.createElement
Expand Down

0 comments on commit af516bd

Please sign in to comment.