@@ -9,6 +9,7 @@ import React, {
99 useMemo ,
1010 useRef ,
1111 useState ,
12+ useEffect ,
1213} from 'react' ;
1314
1415type InitializeRoomParams = {
@@ -57,7 +58,6 @@ interface RoomContextInternalProps {
5758 leaveRoom : ( ) => void ;
5859 addComponent : ( component : unknown ) => void ;
5960 removeComponent : ( component : unknown ) => void ;
60- setCallbacks : ( callbacks : RoomCallbacks ) => void ;
6161 getParticipants : ( ) => Promise < Participant [ ] > ;
6262 room : Room | null ;
6363 components : Record < string , unknown > ;
@@ -70,17 +70,8 @@ const RoomProvider: React.FC<{
7070 developerToken : string
7171} > = ( { children, developerToken } ) => {
7272 const [ components , setComponents ] = useState < Record < string , unknown > > ( { } ) ;
73- const callbacks = useRef < Record < keyof RoomCallbacks , Callback [ ] > > ( {
74- onMyParticipantJoined : [ ] ,
75- onMyParticipantLeft : [ ] ,
76- onMyParticipantUpdated : [ ] ,
77- onParticipantJoined : [ ] ,
78- onParticipantLeft : [ ] ,
79- onParticipantUpdated : [ ] ,
80- onError : [ ] ,
81- onRoomUpdated : [ ] ,
82- } ) ;
83- const room = useRef < Room | null > ( null ) ;
73+ const roomInstance = useRef < Room | null > ( null ) ;
74+ const [ room , setRoom ] = useState < Room | null > ( null ) ;
8475 const initialized = useRef < boolean > ( false ) ;
8576
8677 const joinRoom = useCallback ( async ( options : InitializeRoomParams ) => {
@@ -91,48 +82,21 @@ const RoomProvider: React.FC<{
9182
9283 initialized . current = true ;
9384
94- room . current = await createRoom (
85+ roomInstance . current = await createRoom (
9586 Object . assign ( { } , options , { developerToken } )
9687 ) ;
9788
98- room . current . subscribe ( 'my-participant.joined' , onMyParticipantJoined ) ;
99- room . current . subscribe ( 'my-participant.left' , onMyParticipantLeft ) ;
100- room . current . subscribe ( 'my-participant.updated' , onMyParticipantUpdated ) ;
101- room . current . subscribe ( 'participant.joined' , onParticipantJoined ) ;
102- room . current . subscribe ( 'participant.left' , onParticipantLeft ) ;
103- room . current . subscribe ( 'participant.updated' , onParticipantUpdated ) ;
104- room . current . subscribe ( 'room.error' , onError ) ;
105- room . current . subscribe ( 'room.update' , onRoomUpdated ) ;
106- } , [ callbacks , initialized ] ) ;
89+ setRoom ( roomInstance . current ) ;
90+ } , [ developerToken , setRoom , initialized . current , roomInstance . current ] ) ;
10791
10892 const leaveRoom = useCallback ( ( ) => {
10993 if ( ! initialized . current ) {
11094 console . warn ( '[SuperViz] Room not initialized, nothing to leave' ) ;
11195 return ;
11296 }
11397
114- room . current ?. leave ( ) ;
115- room . current ?. unsubscribe ( 'my-participant.joined' , onMyParticipantJoined ) ;
116- room . current ?. unsubscribe ( 'my-participant.left' , onMyParticipantLeft ) ;
117- room . current ?. unsubscribe ( 'my-participant.updated' , onMyParticipantUpdated ) ;
118- room . current ?. unsubscribe ( 'participant.joined' , onParticipantJoined ) ;
119- room . current ?. unsubscribe ( 'participant.left' , onParticipantLeft ) ;
120- room . current ?. unsubscribe ( 'participant.updated' , onParticipantUpdated ) ;
121- room . current ?. unsubscribe ( 'room.error' , onError ) ;
122- room . current ?. unsubscribe ( 'room.update' , onRoomUpdated ) ;
123- callbacks . current = {
124- onMyParticipantJoined : [ ] ,
125- onMyParticipantLeft : [ ] ,
126- onMyParticipantUpdated : [ ] ,
127- onParticipantJoined : [ ] ,
128- onParticipantLeft : [ ] ,
129- onParticipantUpdated : [ ] ,
130- onError : [ ] ,
131- onRoomUpdated : [ ] ,
132- }
133-
134- initialized . current = false ;
135- } , [ ] ) ;
98+ roomInstance . current ?. leave ( ) ;
99+ } , [ initialized ] ) ;
136100
137101 const addComponent = useCallback ( ( component : unknown ) => {
138102 if ( ! initialized . current ) {
@@ -145,10 +109,11 @@ const RoomProvider: React.FC<{
145109 return ;
146110 }
147111
148- setComponents ( prev => ( { ... prev , [ ( component as Component ) ?. name ] : component } ) ) ;
112+ roomInstance . current ?. addComponent ( component ) ;
149113
150- room . current ?. addComponent ( component ) ;
151- } , [ ] ) ;
114+ console . log ( roomInstance . current ) ;
115+ setComponents ( prev => ( { ...prev , [ ( component as Component ) ?. name ] : component } ) ) ;
116+ } , [ room , initialized . current ] ) ;
152117
153118 const removeComponent = useCallback ( ( component : unknown ) => {
154119 if ( ! initialized . current ) {
@@ -166,97 +131,28 @@ const RoomProvider: React.FC<{
166131 return prev ;
167132 } ) ;
168133
169- room . current ?. removeComponent ( component ) ;
134+ roomInstance . current ?. removeComponent ( component ) ;
170135 } , [ ] ) ;
171136
172- const updateCallbacks = ( newCallbacks : RoomCallbacks ) => {
173- Object . keys ( newCallbacks ) . forEach ( ( key ) => {
174- const callbackKey = key as keyof RoomCallbacks ;
175-
176- if ( callbacks . current [ callbackKey ] ) {
177- const existingCallbacks = callbacks . current [ callbackKey ] ;
178-
179- const callbackMap : Map < string , Callback > = new Map (
180- existingCallbacks . map ( ( cb ) => [ cb . toString ( ) , cb ] )
181- ) ;
182-
183- const newCallback = newCallbacks [ callbackKey ] as Callback ;
184-
185- if ( newCallback && ! callbackMap . has ( newCallback . toString ( ) ) ) {
186- callbackMap . set ( newCallback . toString ( ) , newCallback ) ;
187- callbacks . current [ callbackKey ] = Array . from ( callbackMap . values ( ) ) ;
188- }
189- }
190- } ) ;
191-
192- } ;
193-
194- const onMyParticipantJoined = useCallback ( ( participant : Participant ) => {
195- if ( callbacks . current . onMyParticipantJoined ) {
196- callbacks . current . onMyParticipantJoined . forEach ( cb => cb ( participant ) ) ;
197- }
198- } , [ callbacks . current ] ) ;
199-
200- const onMyParticipantLeft = useCallback ( ( participant : Participant ) => {
201- if ( callbacks . current . onMyParticipantLeft ) {
202- callbacks . current . onMyParticipantLeft . forEach ( cb => cb ( participant ) ) ;
203- }
204- } , [ callbacks . current ] ) ;
205-
206- const onMyParticipantUpdated = useCallback ( ( participant : Participant ) => {
207- if ( callbacks . current . onMyParticipantUpdated ) {
208- callbacks . current . onMyParticipantUpdated . forEach ( cb => cb ( participant ) ) ;
209- }
210- } , [ callbacks . current ] ) ;
211-
212- const onParticipantJoined = useCallback ( ( participant : Participant ) => {
213- if ( callbacks . current . onParticipantJoined ) {
214- callbacks . current . onParticipantJoined . forEach ( cb => cb ( participant ) ) ;
215- }
216- } , [ callbacks . current ] ) ;
217-
218- const onParticipantLeft = useCallback ( ( participant : Participant ) => {
219- if ( callbacks . current . onParticipantLeft ) {
220- callbacks . current . onParticipantLeft . forEach ( cb => cb ( participant ) ) ;
221- }
222- } , [ callbacks . current ] ) ;
223-
224- const onParticipantUpdated = useCallback ( ( participant : Participant ) => {
225- if ( callbacks . current . onParticipantUpdated ) {
226- callbacks . current . onParticipantUpdated . forEach ( cb => cb ( participant ) ) ;
227- }
228- } , [ callbacks . current ] ) ;
229-
230- const onError = useCallback ( ( error : RoomError ) => {
231- if ( callbacks . current . onError ) {
232- callbacks . current . onError . forEach ( cb => cb ( error ) ) ;
233- }
234- } , [ callbacks . current ] ) ;
235137
236- const onRoomUpdated = useCallback ( ( data : RoomUpdate ) => {
237- if ( callbacks . current . onRoomUpdated ) {
238- callbacks . current . onRoomUpdated . forEach ( cb => cb ( data ) ) ;
239- }
240- } , [ callbacks . current ] ) ;
241138
242139 const getParticipants = useCallback ( async ( ) => {
243- if ( ! room . current ) {
140+ if ( ! roomInstance . current ) {
244141 console . warn ( '[SuperViz] Room not initialized, cannot get participants' ) ;
245142 return [ ] ;
246143 }
247144
248- return room . current . getParticipants ( ) ;
249- } , [ room . current ] ) ;
145+ return roomInstance . current ? .getParticipants ( ) ;
146+ } , [ roomInstance . current ] ) ;
250147
251148 return (
252149 < RoomContext . Provider
253150 value = { {
254151 joinRoom,
255152 leaveRoom,
256- setCallbacks : updateCallbacks ,
257153 addComponent,
258154 removeComponent,
259- room : room . current ,
155+ room : room ,
260156 components,
261157 getParticipants
262158 } }
@@ -283,10 +179,64 @@ const useRoom = (callbacks?: RoomCallbacks) => {
283179 throw new Error ( 'useRoom must be used within a RoomProvider' ) ;
284180 }
285181
286- if ( callbacks && Object . keys ( callbacks ) . length ) {
287- context . setCallbacks ( callbacks ) ;
182+ const onMyParticipantJoined = ( participant : Participant ) => {
183+ callbacks ?. onMyParticipantJoined ?.( participant ) ;
184+ }
185+
186+ const onMyParticipantLeft = ( participant : Participant ) => {
187+ callbacks ?. onMyParticipantLeft ?.( participant ) ;
188+ }
189+
190+ const onMyParticipantUpdated = ( participant : Participant ) => {
191+ callbacks ?. onMyParticipantUpdated ?.( participant ) ;
192+ }
193+
194+ const onParticipantJoined = ( participant : Participant ) => {
195+ callbacks ?. onParticipantJoined ?.( participant ) ;
196+ }
197+
198+ const onParticipantLeft = ( participant : Participant ) => {
199+ callbacks ?. onParticipantLeft ?.( participant ) ;
288200 }
289201
202+ const onParticipantUpdated = ( participant : Participant ) => {
203+ callbacks ?. onParticipantUpdated ?.( participant ) ;
204+ }
205+
206+ const onRoomUpdated = ( data : RoomUpdate ) => {
207+ callbacks ?. onRoomUpdated ?.( data ) ;
208+ }
209+
210+ const onError = ( error : RoomError ) => {
211+ callbacks ?. onError ?.( error ) ;
212+ }
213+
214+ useEffect ( ( ) => {
215+ if ( context . room ) {
216+ context . room ?. subscribe ( 'my-participant.joined' , onMyParticipantJoined ) ;
217+ context . room ?. subscribe ( 'my-participant.left' , onMyParticipantLeft ) ;
218+ context . room ?. subscribe ( 'my-participant.updated' , onMyParticipantUpdated ) ;
219+ context . room ?. subscribe ( 'participant.joined' , onParticipantJoined ) ;
220+ context . room ?. subscribe ( 'participant.left' , onParticipantLeft ) ;
221+ context . room ?. subscribe ( 'participant.updated' , onParticipantUpdated ) ;
222+ context . room ?. subscribe ( 'room.error' , onError ) ;
223+ context . room ?. subscribe ( 'room.update' , onRoomUpdated ) ;
224+ }
225+
226+ return ( ) => {
227+ context . room ?. unsubscribe ( 'my-participant.joined' , onMyParticipantJoined ) ;
228+ context . room ?. unsubscribe ( 'my-participant.left' , onMyParticipantLeft ) ;
229+ context . room ?. unsubscribe ( 'my-participant.updated' , onMyParticipantUpdated ) ;
230+ context . room ?. unsubscribe ( 'participant.joined' , onParticipantJoined ) ;
231+ context . room ?. unsubscribe ( 'participant.left' , onParticipantLeft ) ;
232+ context . room ?. unsubscribe ( 'participant.updated' , onParticipantUpdated ) ;
233+ context . room ?. unsubscribe ( 'room.error' , onError ) ;
234+ context . room ?. unsubscribe ( 'room.update' , onRoomUpdated ) ;
235+ }
236+ } , [ context . room ] )
237+
238+
239+
290240 return {
291241 joinRoom : context . joinRoom ,
292242 leaveRoom : context . leaveRoom ,
0 commit comments