From 424b1c56a421f1f2574d80bc5cd8f7625bb8d2d9 Mon Sep 17 00:00:00 2001 From: talksik Date: Sat, 7 May 2022 14:24:01 -0500 Subject: [PATCH] untuning with escape...which should then remove from tunedids list and thus the source of truth whether or not to render the peer connector and rtc stuff --- packages/api/sockets/index.ts | 20 +++++++++---------- .../src/controller/lineDataProvider.tsx | 16 +++++++++++++++ packages/desktop/src/pages/terminal/index.tsx | 16 ++++++++++++--- 3 files changed, 39 insertions(+), 13 deletions(-) diff --git a/packages/api/sockets/index.ts b/packages/api/sockets/index.ts index 49e7678..d66ea2c 100644 --- a/packages/api/sockets/index.ts +++ b/packages/api/sockets/index.ts @@ -86,7 +86,7 @@ export default function InitializeWs(io: any) { console.log(`${socket.id} now in rooms ${socket.rooms}`); const clientUserIdsInRoom = [ - ...io.sockets.adapter.rooms.get(roomName), + ...(io.sockets.adapter.rooms.get(roomName) ?? []), ].map( (otherUserSocketId: string) => socketIdsToUserIds[otherUserSocketId] ); @@ -102,6 +102,13 @@ export default function InitializeWs(io: any) { } ); + /** + * TODO: handle when user wants to completely leave a line (delete or removed from one) + */ + socket.on(ServerRequestChannels.DISCONNECT_FROM_LINE, () => + console.log("not implemented") + ); + /** TUNE | User tunes into the line either temporarily or toggled in */ socket.on( ServerRequestChannels.TUNE_INTO_LINE, @@ -131,7 +138,7 @@ export default function InitializeWs(io: any) { } const clientUserIdsInRoom = [ - ...io.sockets.adapter.rooms.get(roomName), + ...(io.sockets.adapter.rooms.get(roomName) ?? []), ].map( (otherUserSocketId: string) => socketIdsToUserIds[otherUserSocketId] ); @@ -151,13 +158,6 @@ export default function InitializeWs(io: any) { } ); - /** - * TODO: handle when user wants to completely leave a line (delete or removed from one) - */ - socket.on(ServerRequestChannels.DISCONNECT_FROM_LINE, () => - console.log("not implemented") - ); - /** * Notify all connected users when someone UNTUNES from a room * ?might not be needed, all users' memory of tuned in users is irrelevant? don't need real time? but UI will show # of users tuned in? @@ -171,7 +171,7 @@ export default function InitializeWs(io: any) { console.log("someone left room"); const clientUserIdsInRoom = [ - ...io.sockets.adapter.rooms.get(roomName), + ...(io.sockets.adapter.rooms.get(roomName) ?? []), ].map( (otherUserSocketId: string) => socketIdsToUserIds[otherUserSocketId] ); diff --git a/packages/desktop/src/controller/lineDataProvider.tsx b/packages/desktop/src/controller/lineDataProvider.tsx index 22edc10..5e08484 100644 --- a/packages/desktop/src/controller/lineDataProvider.tsx +++ b/packages/desktop/src/controller/lineDataProvider.tsx @@ -9,6 +9,7 @@ import { StartBroadcastingRequest, StopBroadcastingRequest, TuneToLineRequest, + UntuneFromLineRequest, UserStartedBroadcastingResponse, UserStoppedBroadcastingResponse, } from "@nirvana/core/sockets/channels"; @@ -257,6 +258,20 @@ function useSocketHandler(linesData: MasterLineData[]) { [$ws] ); + const handleUnTuneToLine = useCallback( + (lineId: string) => { + // they already are in the socket room for updates including media connections and disconnections + // but set the flag so that the line row can know whether or not to start the webrtc process + // and know when to get out or disconnect from the webrtc when the flag turns off + + $ws.emit( + ServerRequestChannels.UNTUNE_FROM_LINE, + new UntuneFromLineRequest(lineId) + ); + }, + [$ws] + ); + /** * This is when the user wants to tell everyone that they are streaming/broadcasting/buzzing to * a specific line, whether they are toggle tuned or temporarily tuned in @@ -313,6 +328,7 @@ function useSocketHandler(linesData: MasterLineData[]) { handleStartBroadcast, handleStopBroadcast, handleFetchMoreAudioBlocks, + handleUnTuneToLine, }; } diff --git a/packages/desktop/src/pages/terminal/index.tsx b/packages/desktop/src/pages/terminal/index.tsx index 6e038cd..6c1c259 100644 --- a/packages/desktop/src/pages/terminal/index.tsx +++ b/packages/desktop/src/pages/terminal/index.tsx @@ -25,7 +25,8 @@ export default function NirvanaTerminal() { // simply using this query for specific data on loading // todo: add these properties in context provider value although more work down the line for control const { isLoading: isLoadingInitialLines } = useUserLines(); - const { linesMap, handleTuneToLine } = useLineDataProvider(); + const { linesMap, handleTuneToLine, handleUnTuneToLine } = + useLineDataProvider(); useEffect(() => { console.log("change/update in lines map"); @@ -56,8 +57,17 @@ export default function NirvanaTerminal() { const handleEscape = useCallback(() => { console.log("deselecting line"); - setSelectedLineId(null); - }, [setSelectedLineId]); + setSelectedLineId((prevSelectedLineId) => { + // ! only want to untune if it's a temporarily tuned line + if ( + linesMap[prevSelectedLineId].currentUserMember?.state === + LineMemberState.INBOX + ) + handleUnTuneToLine(prevSelectedLineId); + + return null; + }); + }, [setSelectedLineId, linesMap]); const handleStartBroadcast = useCallback( (lineId: string) => () => {