Skip to content

Commit

Permalink
ipc
Browse files Browse the repository at this point in the history
  • Loading branch information
duke79 committed Jul 5, 2024
1 parent eefb7f6 commit 34ef7e9
Show file tree
Hide file tree
Showing 8 changed files with 13,378 additions and 13,184 deletions.
26,434 changes: 13,257 additions & 13,177 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,8 @@
"tsdx": "^0.14.1",
"tslib": "^2.6.3",
"typescript": "^3.9.10"
},
"dependencies": {
"socket.io-client": "^4.7.5"
}
}
}
7 changes: 1 addition & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
export const sum = (a: number, b: number) => {
if ('development' === process.env.NODE_ENV) {
console.log('boop');
}
return a + b;
};
export * from './ipc';
9 changes: 9 additions & 0 deletions src/ipc/channels.enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export enum ClientToServerChannel {
SendMessage = 'clientToServer.sendMessage',
RequestChatHistory = 'clientToServer.requestChatHistory',
}

export enum ServerToClientChannel {
SendMessage = 'serverToClient.sendMessage',
SendChatHistory = 'serverToClient.sendChatHistory',
}
18 changes: 18 additions & 0 deletions src/ipc/channels.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { ClientToServerChannel, ServerToClientChannel } from "./channels.enum";

export type ChannelBody<T extends ClientToServerChannel | ServerToClientChannel> =
T extends ClientToServerChannel.SendMessage ? { message: string } :
T extends ServerToClientChannel.SendMessage ? { message: string } :

T extends ClientToServerChannel.RequestChatHistory ? {
chatId?: string
} :
T extends ServerToClientChannel.SendChatHistory ? {
chatId: string;
messages: {
user: string;
message: string;
}[];
} :

never;
43 changes: 43 additions & 0 deletions src/ipc/client-ipc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { ClientToServerChannel, ServerToClientChannel } from "./channels.enum";
import { ChannelBody } from "./channels.type";

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

private constructor(
private sendMessage: (channel: ClientToServerChannel, body: ChannelBody<ClientToServerChannel>) => void,
private onMessage: (cb: (channel: ServerToClientChannel, body: ChannelBody<ServerToClientChannel>) => void) => void
) {
this._listeners = [];
this.onMessage((channel: ServerToClientChannel, body: ChannelBody<ServerToClientChannel>) => {
console.log({ dataOnServerSide: body });
this._listeners.forEach((listener) => {
if (listener.channel === channel) {
listener.callback(body);
}
});
});
}

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

sendToServer<T extends ClientToServerChannel>(channel: T, body: ChannelBody<T>): void {
this.sendMessage(channel, body);
}

onServerMessage<T extends ServerToClientChannel>(channel: T, callback: (body: ChannelBody<T>) => void): void {
this._listeners.push({ channel, callback: callback as any });
}
}
4 changes: 4 additions & 0 deletions src/ipc/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './channels.enum';
export * from './channels.type';
export * from './client-ipc';
export * from './server-ipc';
42 changes: 42 additions & 0 deletions src/ipc/server-ipc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { ClientToServerChannel, ServerToClientChannel } from "./channels.enum";
import { ChannelBody } from "./channels.type";

export class ServerIPC {
private static _instance?: ServerIPC;
private _listeners: {
channel: ClientToServerChannel,
callback: (body: ChannelBody<ClientToServerChannel>) => void
}[];
private constructor(
private sendMessage: (channel: ServerToClientChannel, body: ChannelBody<ServerToClientChannel>) => void,
private onMessage: (cb: (channel: ClientToServerChannel, body: ChannelBody<ClientToServerChannel>) => void) => void
) {
this._listeners = [];
this.onMessage((data: any) => {
this._listeners.forEach((listener) => {
if (listener.channel === data.channel) {
listener.callback(data.body);
}
});
});
}

static getInstance(
sendMessage: (channel: ServerToClientChannel, body: ChannelBody<ServerToClientChannel>) => void,
onMessage: (cb: (channel: ClientToServerChannel, body: ChannelBody<ClientToServerChannel>) => void) => void
) {
if (onMessage && sendMessage) {
ServerIPC._instance = new ServerIPC(sendMessage, onMessage);
}
return ServerIPC._instance;
}

// Server-to-Client communication
sendToClient<T extends ServerToClientChannel>(channel: T, body: ChannelBody<T>): void {
this.sendMessage(channel, body);
}

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

0 comments on commit 34ef7e9

Please sign in to comment.