-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
13,378 additions
and
13,184 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
} | ||
} |