11import {
2- DailyEventObject ,
3- DailyEventObjectWaitingParticipant ,
42 DailyParticipant ,
53 DailyParticipantsObject ,
64 DailyWaitingParticipant ,
@@ -10,8 +8,15 @@ import { atom, atomFamily, selector, useRecoilCallback } from 'recoil';
108
119import { useDaily } from './hooks/useDaily' ;
1210import { useDailyEvent } from './hooks/useDailyEvent' ;
11+ import {
12+ participantPropertyPathsState ,
13+ participantPropertyState ,
14+ } from './hooks/useParticipantProperty' ;
1315import { useThrottledDailyEvent } from './hooks/useThrottledDailyEvent' ;
1416import { RECOIL_PREFIX } from './lib/constants' ;
17+ import { customDeepEqual } from './lib/customDeepEqual' ;
18+ import { getParticipantPaths } from './utils/getParticipantPaths' ;
19+ import { resolveParticipantPaths } from './utils/resolveParticipantPaths' ;
1520
1621/**
1722 * Extends DailyParticipant with convenient additional properties.
@@ -116,6 +121,23 @@ export const DailyParticipants: React.FC<React.PropsWithChildren<{}>> = ({
116121 set ( participantIdsState , ids ) ;
117122 participantsArray . forEach ( ( p ) => {
118123 set ( participantState ( p . session_id ) , p ) ;
124+ const paths = getParticipantPaths ( p ) ;
125+ // Set list of property paths
126+ set ( participantPropertyPathsState ( p . session_id ) , paths ) ;
127+ // Set all property path values
128+ paths . forEach ( ( property ) => {
129+ const [ value ] = resolveParticipantPaths (
130+ p as ExtendedDailyParticipant ,
131+ [ property ]
132+ ) ;
133+ set (
134+ participantPropertyState ( {
135+ id : p . session_id ,
136+ property,
137+ } ) ,
138+ value
139+ ) ;
140+ } ) ;
119141 } ) ;
120142 setInitialized ( true ) ;
121143 } ) ;
@@ -178,16 +200,7 @@ export const DailyParticipants: React.FC<React.PropsWithChildren<{}>> = ({
178200 ] ,
179201 useRecoilCallback (
180202 ( { transact_UNSTABLE } ) =>
181- (
182- evts : DailyEventObject <
183- | 'active-speaker-change'
184- | 'participant-joined'
185- | 'participant-updated'
186- | 'participant-left'
187- | 'left-meeting'
188- | 'call-instance-destroyed'
189- > [ ]
190- ) => {
203+ ( evts ) => {
191204 transact_UNSTABLE ( ( { get, reset, set } ) => {
192205 evts . forEach ( ( ev ) => {
193206 switch ( ev . action ) {
@@ -203,31 +216,94 @@ export const DailyParticipants: React.FC<React.PropsWithChildren<{}>> = ({
203216 } ) ;
204217 break ;
205218 }
206- case 'participant-joined' :
219+ case 'participant-joined' : {
220+ // Update list of ids
207221 set ( participantIdsState , ( prevIds ) =>
208222 prevIds . includes ( ev . participant . session_id )
209223 ? prevIds
210224 : [ ...prevIds , ev . participant . session_id ]
211225 ) ;
226+ // Store entire object
212227 set (
213228 participantState ( ev . participant . session_id ) ,
214229 ev . participant
215230 ) ;
231+
232+ const paths = getParticipantPaths ( ev . participant ) ;
233+ // Set list of property paths
234+ set (
235+ participantPropertyPathsState ( ev . participant . session_id ) ,
236+ paths
237+ ) ;
238+ // Set all property path values
239+ paths . forEach ( ( property ) => {
240+ const [ value ] = resolveParticipantPaths (
241+ ev . participant as ExtendedDailyParticipant ,
242+ [ property ]
243+ ) ;
244+ set (
245+ participantPropertyState ( {
246+ id : ev . participant . session_id ,
247+ property,
248+ } ) ,
249+ value
250+ ) ;
251+ } ) ;
216252 break ;
217- case 'participant-updated' :
253+ }
254+ case 'participant-updated' : {
255+ // Update entire object
218256 set ( participantState ( ev . participant . session_id ) , ( prev ) => ( {
219257 ...prev ,
220258 ...ev . participant ,
221259 } ) ) ;
260+ // Update local session_id
222261 if ( ev . participant . local ) {
223262 set ( localIdState , ( prevId ) =>
224263 prevId !== ev . participant . session_id
225264 ? ev . participant . session_id
226265 : prevId
227266 ) ;
228267 }
268+
269+ const paths = getParticipantPaths ( ev . participant ) ;
270+ const oldPaths = get (
271+ participantPropertyPathsState ( ev . participant . session_id )
272+ ) ;
273+ // Set list of property paths
274+ set (
275+ participantPropertyPathsState ( ev . participant . session_id ) ,
276+ ( prev ) => ( customDeepEqual ( prev , paths ) ? prev : paths )
277+ ) ;
278+ // Reset old path values
279+ oldPaths
280+ . filter ( ( p ) => ! paths . includes ( p ) )
281+ . forEach ( ( property ) => {
282+ reset (
283+ participantPropertyState ( {
284+ id : ev . participant . session_id ,
285+ property,
286+ } )
287+ ) ;
288+ } ) ;
289+ // Set all property path values
290+ paths . forEach ( ( property ) => {
291+ const [ value ] = resolveParticipantPaths (
292+ ev . participant as ExtendedDailyParticipant ,
293+ [ property ]
294+ ) ;
295+ set (
296+ participantPropertyState ( {
297+ id : ev . participant . session_id ,
298+ property,
299+ } ) ,
300+ ( prev ) => ( customDeepEqual ( prev , value ) ? prev : value )
301+ ) ;
302+ } ) ;
229303 break ;
230- case 'participant-left' :
304+ }
305+ case 'participant-left' : {
306+ // Remove from list of ids
231307 set ( participantIdsState , ( prevIds ) =>
232308 prevIds . includes ( ev . participant . session_id )
233309 ? [
@@ -237,8 +313,27 @@ export const DailyParticipants: React.FC<React.PropsWithChildren<{}>> = ({
237313 ]
238314 : prevIds
239315 ) ;
316+ // Remove entire object
240317 reset ( participantState ( ev . participant . session_id ) ) ;
318+
319+ const oldPaths = get (
320+ participantPropertyPathsState ( ev . participant . session_id )
321+ ) ;
322+ // Remove property path values
323+ oldPaths . forEach ( ( property ) => {
324+ reset (
325+ participantPropertyState ( {
326+ id : ev . participant . session_id ,
327+ property,
328+ } )
329+ ) ;
330+ } ) ;
331+ // Remove all property paths
332+ reset (
333+ participantPropertyPathsState ( ev . participant . session_id )
334+ ) ;
241335 break ;
336+ }
242337 /**
243338 * Reset stored participants, when meeting has ended.
244339 */
@@ -266,7 +361,7 @@ export const DailyParticipants: React.FC<React.PropsWithChildren<{}>> = ({
266361 ] ,
267362 useRecoilCallback (
268363 ( { transact_UNSTABLE } ) =>
269- ( evts : DailyEventObjectWaitingParticipant [ ] ) => {
364+ ( evts ) => {
270365 transact_UNSTABLE ( ( { reset, set } ) => {
271366 evts . forEach ( ( ev ) => {
272367 switch ( ev . action ) {
0 commit comments