Im working on a video chat app, which is implemented using Chime SDK and React. Curently im working on a sorting algorithm to sort user tiles based on their video state and microphone activity.
The attendees are represented as an array of objects which look similar to this (simplified)
attendees = [{id: 1, name "User 1"}, {id: 2, name: "User 2"}, ...];
Then I have an array of user ids which represent user activity baset on the quantity of their activity:
activeSpeakers = [2, 1]
This array can be empty which means nobody is speaking, or if there are items, the first item is the id of the most active speaker and the last is the id of the least active speaker.
I also have an array of user Id which contains chronogicaly ordered id of users who have the camera on. This array also can be empty meaning that nobody has the camera active.
attendeeToTileId = [2]
My task is to sort the user tiles based theese condirions:
- Camera active and active speaker
- Camera active and inactive speaker
- Camera inactive and active speaker
- Camera inactive and inactive speaker
Also if you familiar with the Chime SDK, you know that theese states update frequent, and my idea is to throthle the sorting.
So i stared with something like this
useEffect(() => {
// sort logic here
// but should be throtthled
// or maybe debounced?
}, [attendees, attendeeToTileId, activeSpeakers])
I tryed custom throtling implementation as well as the lodash library, but nothing seems to work correctly.
So in summary:
- I need an advice for a sorting algorithm
- I need a throthling/debounce strategy for react
With either one, I would be happy. I just need a good starting point.
After a few weeks of tweaking and a few months of delay in writing the answer, I have found a solution that seems to work quite well.
I have created a
calculateSortingScorefunction that assigns a score to each tile based on the activity of the microphone and camera state.Here is the code snippet of the function:
Then, I created a custom sort function that is throttled due to the microphone activity. If there are 20 users speaking, it would be chaotic. Therefore, I throttled the function invocation.
In addition, I have defined a
throttleIntervalof 10000 ms, which seems suitable for my use case but can be adjusted as necessary. However, I would not set it to less than 5000 ms. Therefore, I defined it as follows:const throttleInterval = 10000.The only thing left is to loop over the
attendeesFinalarray and display the tiles. There might be a better solution to implement this, but this one works.