From f066e0707b7dea1b0d7a176ee615db40d1196658 Mon Sep 17 00:00:00 2001 From: talksik Date: Sat, 11 Jun 2022 16:59:59 -0500 Subject: [PATCH] cleaning and starting process for listening to socket events --- packages/core/sockets/channels.ts | 38 ------------------ .../src/providers/RealtimeProvider.tsx | 40 +++++++++++++++++++ 2 files changed, 40 insertions(+), 38 deletions(-) diff --git a/packages/core/sockets/channels.ts b/packages/core/sockets/channels.ts index 732ca6e..303aa9b 100644 --- a/packages/core/sockets/channels.ts +++ b/packages/core/sockets/channels.ts @@ -1,34 +1,3 @@ -// ! NOTE: these are legacy and too much thinking in the developers head to understand the flow -enum SocketChannels { - SEND_AUDIO_CLIP = 'SEND_AUDIO_CLIP', - SEND_USER_STATUS_UPDATE = 'SEND_USER_STATUS_UPDATE', - - SEND_STARTED_SPEAKING = 'SEND_STARTED_SPEAKING', - SEND_STOPPED_SPEAKING = 'SEND_STOPPED_SPEAKING', - - JOIN_LIVE_ROOM = 'JOIN_LIVE_ROOM', - GET_ALL_ACTIVE_SOCKET_IDS = 'GET_ALL_ACTIVE_SOCKET_IDS', - - SEND_SIGNAL = 'SEND_SIGNAL', - - RECEIVE_SIGNAL = 'RECEIVE_SIGNAL', - - // v3 - - /** - * when someone connects to a line, whether tuned in or not - */ - CONNECT_TO_LINE = 'CONNECT_TO_LINE', - SOMEONE_CONNECTED_TO_LINE = 'SOMEONE_CONNECTED_TO_LINE', - - TUNE_TO_LINE = 'TUNE_TO_LINE', - SOMEONE_TUNED_TO_LINE = 'SOMEONE_TUNED_TO_LINE', - - SOMEONE_UNTUNED_FROM_LINE = 'SOMEONE_UNTUNED_FROM_LINE', - - USER_BROADCAST_PUSH_PULL = 'USER_BROADCAST_PUSH_PULL', -} - // ! MAKE SURE THAT THE ENUMS BETWEEN REQUEST AND RESPONSE DON'T OVERLAP??? // ?maybe won't matter since it's different for server and client? export enum ServerRequestChannels { @@ -64,8 +33,6 @@ export enum ServerResponseChannels { SOMEONE_GOING_INTO_FLOW_STATE = 'SOMEONE_GOING_INTO_FLOW_STATE', } -export default SocketChannels; - export class ConnectToLineRequest { constructor(public lineId: string) {} } @@ -103,11 +70,6 @@ export class UserStoppedBroadcastingResponse { constructor(public lineId: string, public userId: string) {} } -// ?another approach to use switch statements, but it just requires same mess, just less methods with socket but who cares right now -export class SocketEmitter { - constructor(public channel: SocketChannels, data: T) {} -} - export class RtcCallRequest { constructor(public userToCall: string, public lineId: string, public simplePeerSignal: any) {} } diff --git a/packages/desktop/src/providers/RealtimeProvider.tsx b/packages/desktop/src/providers/RealtimeProvider.tsx index e69de29..0ffa8f6 100644 --- a/packages/desktop/src/providers/RealtimeProvider.tsx +++ b/packages/desktop/src/providers/RealtimeProvider.tsx @@ -0,0 +1,40 @@ +import React, { useEffect } from 'react'; +import { ServerResponseChannels, SomeoneTunedResponse } from '@nirvana/core/sockets/channels'; + +import { SomeoneConnectedResponse } from '@nirvana/core/sockets/channels'; +import useSockets from './SocketProvider'; + +/** + * Responsibility: provide real time feel of people presence + * + * Get to know when: + * - me or someone else tunes or untunes from a conversation + * - someone speaks in a chat for a conversation that I am tuned into + * - there is new content in a channel that I am connected or tuned into + * - someone in a chat that I am tuned into goes into flow state + * + * + * Firing Events - this could be in the universe as a whole through a hook and doesn't need to be in here + * - have helper async functions which take in a socket client and call the event + * - on load, we need to join the particular rooms based on priority vs inbox + * - move conversation to priority and back and mutate the conversation map + * + * Listening Events: + * - have a useEffect that listens to all events and mutates conversation map accordingly + * - + */ + +const RealtimeContext = React.createContext({}); + +export function RealtimeProvider({ children }: { children }) { + const { $ws } = useSockets(); + + useEffect(() => { + // $ws.on(ServerResponseChannels.SOMEONE_CONNECTED_TO_LINE, (res: SomeoneConnectedResponse) => { + // }) + // $ws.on(ServerResponseChannels.SOMEONE_TUNED_INTO_LINE, (res: SomeoneTunedResponse) => { + // }) + }, [$ws]); + + return {children}; +}