-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add peerlist iterator API and integration
- Loading branch information
1 parent
acfec6f
commit 189bbb3
Showing
25 changed files
with
402 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,62 @@ | ||
import { HMSRemotePeer } from './models/peer'; | ||
import { IStore } from './store'; | ||
import { HMSPeerUpdate, HMSUpdateListener } from '../interfaces'; | ||
import { HMSPeerListIteratorOptions } from '../interfaces/peer-list-iterator'; | ||
import { PeerNotificationInfo } from '../notification-manager'; | ||
import { createRemotePeer } from '../notification-manager/managers/utils'; | ||
import { PeersIterationResponse } from '../signal/interfaces'; | ||
import ITransport from '../transport/ITransport'; | ||
|
||
export class HMSPeerListIterator { | ||
private isEnd = false; | ||
private iteratorID: string | null = null; | ||
private DEFAULT_LIMIT = 10; | ||
constructor( | ||
private transport: ITransport, | ||
private store: IStore, | ||
private listener?: HMSUpdateListener, | ||
private options?: HMSPeerListIteratorOptions, | ||
) {} | ||
private iterator: string | null = null; | ||
private total = 0; | ||
private defaultPaginationLimit = 10; | ||
constructor(private transport: ITransport, private store: IStore, private options?: HMSPeerListIteratorOptions) {} | ||
|
||
hasNext(): boolean { | ||
return !this.isEnd; | ||
} | ||
|
||
getTotal(): number { | ||
return this.total; | ||
} | ||
|
||
async findPeers() { | ||
const response = await this.transport.findPeers({ | ||
...(this.options || {}), | ||
limit: this.options?.limit || this.defaultPaginationLimit, | ||
}); | ||
this.updateState(response); | ||
return this.processPeers(response.peers); | ||
} | ||
|
||
async next() { | ||
let response: PeersIterationResponse; | ||
if (!this.iteratorID) { | ||
response = await this.transport.findPeer({ | ||
...(this.options || {}), | ||
limit: this.options?.limit || this.DEFAULT_LIMIT, | ||
}); | ||
} else { | ||
if (!this.iterator && !this.isEnd) { | ||
return await this.findPeers(); | ||
} else if (this.iterator) { | ||
response = await this.transport.peerIterNext({ | ||
iterator: this.iteratorID, | ||
limit: this.options?.limit || this.DEFAULT_LIMIT, | ||
iterator: this.iterator, | ||
limit: this.options?.limit || this.defaultPaginationLimit, | ||
}); | ||
this.updateState(response); | ||
return this.processPeers(response.peers); | ||
} | ||
this.isEnd = response.eof; | ||
this.iteratorID = response.iterator; | ||
return []; | ||
} | ||
|
||
private processPeers(peers: PeerNotificationInfo[]) { | ||
const hmsPeers: HMSRemotePeer[] = []; | ||
response.peers.forEach(peer => { | ||
const storeHasPeer = this.store.getPeerById(peer.peer_id); | ||
if (!storeHasPeer) { | ||
const hmsPeer = createRemotePeer(peer, this.store); | ||
hmsPeers.push(hmsPeer); | ||
this.store.addPeer(hmsPeer); | ||
} | ||
peers.forEach(peer => { | ||
const hmsPeer = createRemotePeer(peer, this.store); | ||
hmsPeers.push(hmsPeer); | ||
}); | ||
this.listener?.onPeerUpdate(HMSPeerUpdate.PEER_ITERATOR_UPDATED, hmsPeers); | ||
return hmsPeers; | ||
} | ||
|
||
private updateState(response: PeersIterationResponse) { | ||
this.isEnd = response.eof; | ||
this.total = response.total; | ||
this.iterator = response.iterator; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { useRef, useState } from 'react'; | ||
import { HMSPeer, HMSPeerListIteratorOptions } from '@100mslive/hms-video-store'; | ||
import { useHMSActions } from '../primitives/HmsRoomProvider'; | ||
|
||
export interface usePaginatedParticipantsResult { | ||
/** | ||
* call this function to load initial peers and also when you want to poll the peers information | ||
*/ | ||
loadPeers: Promise<void>; | ||
/** | ||
* this function is to be called when loadPeers is called atleast once. This will fetch the next batch of peers | ||
*/ | ||
loadMorePeers: Promise<void>; | ||
// list of peers loaded | ||
peers: HMSPeer[]; | ||
// total number of peers matching the input options at the time of the request | ||
total: number; | ||
} | ||
|
||
export type usePaginatedParticipantsInput = HMSPeerListIteratorOptions; | ||
|
||
const processPeers = (peers: HMSPeer[]) => { | ||
return peers.reduce<Record<string, HMSPeer>>((acc, peer) => { | ||
acc[peer.id] = peer; | ||
return acc; | ||
}, {}); | ||
}; | ||
|
||
export const usePaginatedParticipants = (options: HMSPeerListIteratorOptions) => { | ||
const actions = useHMSActions(); | ||
const iterator = useRef(actions.getPeerListIterator(options)); | ||
const [peers, setPeers] = useState<Record<string, HMSPeer>>({}); | ||
const [total, setTotal] = useState(0); | ||
|
||
return { | ||
loadPeers: () => | ||
iterator.current.findPeers().then(peers => { | ||
setPeers(processPeers(peers)); | ||
setTotal(iterator.current.getTotal()); | ||
}), | ||
loadMorePeers: () => | ||
iterator.current.next().then(peers => { | ||
setPeers(prevPeers => { | ||
return { | ||
...prevPeers, | ||
...processPeers(peers), | ||
}; | ||
}); | ||
setTotal(iterator.current.getTotal()); | ||
}), | ||
total, | ||
peers: Object.values(peers), | ||
}; | ||
}; |
Oops, something went wrong.