Skip to content

Commit

Permalink
fix: update the mager and stor
Browse files Browse the repository at this point in the history
  • Loading branch information
amar-1995 committed Mar 27, 2024
1 parent 1c0db8a commit e4cc945
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ import { CaptionQueue } from './CaptionQueue';
import { CaptionData, Captions } from '../schema/caption-data';

export class CaptionManager {
// peer_id: captionQueue
private storage: { [key: string]: CaptionQueue } = {};
private peerCapacity = 3;

constructor(private putCaptionInStore: (captions: Captions[]) => void) {}

add(data: CaptionData) {
const captionData = {
final: data.final,
Expand All @@ -19,6 +22,7 @@ export class CaptionManager {
}
this.storage[data.peer_id] = new CaptionQueue(3);
this.storage[data.peer_id].enqueue(captionData);
this.updateCaptions();
}
// map ordered to delete first key..
delete(): boolean {
Expand All @@ -30,16 +34,17 @@ export class CaptionManager {
return true;
}

getCaptions(): Captions[] {
// store update
updateCaptions() {
const keys = Object.keys(this.storage);
const data = keys.map((peerId: string) => {
const data: Captions[] = keys.map((peerId: string) => {
const word = this.storage[peerId].getCaption();
return { peerId, caption: word };
});
return data;
this.putCaptionInStore(data);
}

size(): number {
private size(): number {
return Object.getOwnPropertyNames(this.storage).length;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,10 @@ export class CaptionQueue implements ICaptionQueue {
if (this.size() === this.capacity && this.storage[this.size() - 1].final) {
this.dequeue();
}
// if (this.size() === 0 || item.final) {
// this.storage.push(item);
// return;
// }
// if (item.final) {
// this.storage.push(item);
// return;
// }
if (this.size() === 0 || item.final) {
this.storage.push(item);
return;
}
this.storage[this.size() - 1] = item;
}
dequeue(): QueueData | undefined {
Expand All @@ -42,9 +38,3 @@ export class CaptionQueue implements ICaptionQueue {
return this.storage.length;
}
}
// "123", "234" [] [] [] []
// 1 2 3 1 2 3

// "HI"
// "HI AMar"
// "HI AMAR or" final = true
14 changes: 10 additions & 4 deletions packages/hms-video-store/src/reactive-store/HMSSDKActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { HMSNotifications } from './HMSNotifications';
import { HMSPlaylist } from './HMSPlaylist';
import { HMSSessionStore } from './HMSSessionStore';
import { NamedSetState } from './internalTypes';
import { CaptionManager } from '../caption_manager/CaptionManager';
import { HMSLogger } from '../common/ui-logger';
import { BeamSpeakerLabelsLogger } from '../controller/beam/BeamSpeakerLabelsLogger';
import { IHMSActions } from '../IHMSActions';
Expand Down Expand Up @@ -65,6 +66,7 @@ import {
IHMSPlaylistActions,
IHMSSessionStoreActions,
} from '../schema';
import { Captions } from '../schema/caption-data';
import { HMSSdk } from '../sdk';
import {
HMSRoleChangeRequest,
Expand Down Expand Up @@ -123,6 +125,7 @@ export class HMSSDKActions<T extends HMSGenericTypes = { sessionStore: Record<st
audioPlaylist!: IHMSPlaylistActions;
videoPlaylist!: IHMSPlaylistActions;
sessionStore: IHMSSessionStoreActions<T['sessionStore']>;
private captionManager: CaptionManager;
private beamSpeakerLabelsLogger?: BeamSpeakerLabelsLogger<T>;

constructor(store: IHMSStore<T>, sdk: HMSSdk, notificationManager: HMSNotifications<T>) {
Expand All @@ -132,6 +135,7 @@ export class HMSSDKActions<T extends HMSGenericTypes = { sessionStore: Record<st

this.sessionStore = new HMSSessionStore<T['sessionStore']>(this.sdk, this.setSessionStoreValueLocally.bind(this));
this.actionBatcher = new ActionBatcher(store);
this.captionManager = new CaptionManager(this.putCaptionInStore);
}

getLocalTrack(trackID: string) {
Expand Down Expand Up @@ -1137,6 +1141,8 @@ export class HMSSDKActions<T extends HMSGenericTypes = { sessionStore: Record<st
protected onMessageReceived(message: MessageNotification) {
// check if type `captions`
if (message.info.type === 'captions') {
// add into the store
this.captionManager.add(message.info.message);
return;
}
const hmsMessage = SDKToHMS.convertMessage(message, this.store.getState(selectLocalPeerID)) as HMSMessage;
Expand All @@ -1160,10 +1166,10 @@ export class HMSSDKActions<T extends HMSGenericTypes = { sessionStore: Record<st
);
}

protected putCaptionInStore() {
// this.setState(store => {
// store.captions;
// });
protected putCaptionInStore(captions: Captions[]) {
this.setState(store => {
store.captions = captions;
}, 'captionUpdate');
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ export const makeFakeStore = (): HMSStore => {
polls: {},
whiteboards: {},
hideLocalPeer: false,
captions: [],
};

localPeer = fakeStore.peers['1'];
Expand Down

0 comments on commit e4cc945

Please sign in to comment.