From c629e2ca8d37efcc9f46cec71438fad324f1c627 Mon Sep 17 00:00:00 2001 From: Carlos Santos Date: Mon, 11 Mar 2024 14:08:30 -0300 Subject: [PATCH] feat: perform slot assignment on old io too --- src/core/launcher/index.ts | 2 +- src/services/realtime/ably/index.ts | 94 ----------------------------- src/services/slot/index.test.ts | 8 +-- src/services/slot/index.ts | 34 ++++++++++- 4 files changed, 37 insertions(+), 101 deletions(-) diff --git a/src/core/launcher/index.ts b/src/core/launcher/index.ts index 90adbdab..f188ed72 100644 --- a/src/core/launcher/index.ts +++ b/src/core/launcher/index.ts @@ -382,7 +382,7 @@ export class Launcher extends Observable implements DefaultLauncher { private onParticipantJoinedIOC = (presence: Socket.PresenceEvent): void => { if (presence.id === this.participant.value.id) { // Assign a slot to the participant - const _ = new SlotService(this.LaucherRealtimeRoom, this.participant.value); + SlotService.register(this.LaucherRealtimeRoom, this.realtime, this.participant.value); this.LaucherRealtimeRoom.presence.update(this.participant.value); } diff --git a/src/services/realtime/ably/index.ts b/src/services/realtime/ably/index.ts index 5a6c8a55..6cdb0350 100644 --- a/src/services/realtime/ably/index.ts +++ b/src/services/realtime/ably/index.ts @@ -830,97 +830,6 @@ export default class AblyRealtimeService extends RealtimeService implements Ably } } - /** - * @function findSlotIndex - * @description Finds an available slot index for the participant and confirms it. - * @returns {void} - */ - private findSlotIndex = async (): Promise => { - const slot = Math.floor(Math.random() * 16); - - const hasAnyOneUsingMySlot = await new Promise((resolve) => { - this.supervizChannel.presence.get((error, presences) => { - if (error) { - resolve(true); - return; - } - - presences.forEach((presence) => { - if (presence.clientId === this.myParticipant.clientId) return; - - if (presence.data.slotIndex === slot) resolve(true); - }); - - resolve(false); - }); - }); - - if (hasAnyOneUsingMySlot) { - this.logger.log( - 'slot already taken by someone else, trying again', - this.myParticipant.clientId, - ); - this.findSlotIndex(); - return; - } - - this.updateMyProperties({ slotIndex: slot }); - }; - - /** - * @function validateSlots - * @description Validates the slot index of all participants and resolves conflicts. - * @returns {void} - */ - private async validateSlots(): Promise { - const slots = []; - await new Promise((resolve) => { - this.supervizChannel.presence.get((_, presences) => { - presences.forEach((presence) => { - const hasValidSlot = - presence.data.slotIndex !== undefined && presence.data.slotIndex !== null; - - if (hasValidSlot) { - slots.push({ - slotIndex: presence.data.slotIndex, - clientId: presence.clientId, - timestamp: presence.timestamp, - }); - } - }); - resolve(true); - }); - }); - - const duplicatesMap: Record< - string, - { - slotIndex: number; - clientId: string; - timestamp: number; - }[] - > = {}; - - slots.forEach((a) => { - if (!duplicatesMap[a.slotIndex]) { - duplicatesMap[a.slotIndex] = []; - } - - duplicatesMap[a.slotIndex].push(a); - }); - - Object.values(duplicatesMap).forEach((arr) => { - const ordered = arr.sort((a, b) => a.timestamp - b.timestamp); - ordered.shift(); - - ordered.forEach((slot) => { - if (slot.clientId !== this.myParticipant.clientId) return; - - this.findSlotIndex(); - }); - }); - } - /** * @function onStateChange * @description Translates connection state and channel state into realtime state @@ -1011,8 +920,6 @@ export default class AblyRealtimeService extends RealtimeService implements Ably this.localRoomProperties = await this.fetchRoomProperties(); this.myParticipant = myPresence; - if (this.enableSync) this.findSlotIndex(); - if (!this.localRoomProperties) { this.initializeRoomProperties(); } else { @@ -1037,7 +944,6 @@ export default class AblyRealtimeService extends RealtimeService implements Ably this.updateParticipants(); this.participantJoinedObserver.publish(presence); this.updateMyProperties(); // send a sync - this.validateSlots(); }; /** diff --git a/src/services/slot/index.test.ts b/src/services/slot/index.test.ts index 1c8ef002..f0a21b4a 100644 --- a/src/services/slot/index.test.ts +++ b/src/services/slot/index.test.ts @@ -16,7 +16,7 @@ describe('slot service', () => { id: '123', } as any; - const instance = new SlotService(room, participant); + const instance = new SlotService(room, { updateMyProperties: jest.fn() } as any, participant); await instance['assignSlot'](); expect(instance['slotIndex']).toBeDefined(); @@ -47,7 +47,7 @@ describe('slot service', () => { id: '123', } as any; - const instance = new SlotService(room, participant); + const instance = new SlotService(room, { updateMyProperties: jest.fn() } as any, participant); await instance['assignSlot'](); expect(instance['slotIndex']).toBeDefined(); @@ -80,7 +80,7 @@ describe('slot service', () => { id: '123', } as any; - const instance = new SlotService(room, participant); + const instance = new SlotService(room, { updateMyProperties: jest.fn() } as any, participant); await instance['assignSlot'](); expect(instance['slotIndex']).toBeUndefined(); @@ -114,7 +114,7 @@ describe('slot service', () => { id: '123', } as any; - const instance = new SlotService(room, participant); + const instance = new SlotService(room, { updateMyProperties: jest.fn() } as any, participant); await instance['assignSlot'](); expect(instance['slotIndex']).toBeDefined(); diff --git a/src/services/slot/index.ts b/src/services/slot/index.ts index 3a36d549..0b50794e 100644 --- a/src/services/slot/index.ts +++ b/src/services/slot/index.ts @@ -6,20 +6,37 @@ import { MeetingColorsHex, } from '../../common/types/meeting-colors.types'; import { Participant } from '../../common/types/participant.types'; +import { AblyRealtimeService } from '../realtime'; export class SlotService { private room: Socket.Room; private participant: Participant; private slotIndex: number; + private realtime: AblyRealtimeService; + private static instance: SlotService; - constructor(room: Socket.Room, participant: Participant) { + // @NOTE - reciving old realtime service instance until we migrate to new IO + constructor(room: Socket.Room, realtime: AblyRealtimeService, participant: Participant) { this.room = room; this.participant = participant; + this.realtime = realtime; this.assignSlot(); this.room.presence.on(Socket.PresenceEvents.UPDATE, this.onPresenceUpdate); } + public static register( + room: Socket.Room, + realtime: AblyRealtimeService, + participant: Participant, + ) { + if (!SlotService.instance) { + SlotService.instance = new SlotService(room, realtime, participant); + } + + return SlotService.instance; + } + /** * @function assignSlot * @description Assigns a slot to the participant @@ -46,7 +63,7 @@ export class SlotService { resolve(false); }); }) - .then((isUsing) => { + .then(async (isUsing) => { if (isUsing) { this.assignSlot(); return; @@ -69,6 +86,19 @@ export class SlotService { this.room.presence.update({ slot: slotData, }); + + // @NOTE - this is a temporary fix for the issue where the slot is not being updated in the presence + // @TODO - remove this once we remove the colors from the old io + if (!this.realtime.isJoinedRoom) { + await new Promise((resolve) => { + setTimeout(resolve, 1500); + }); + } + + this.realtime.updateMyProperties({ + slotIndex: slot, + slot: slotData, + }); }) .catch((error) => { this.room.presence.update({