Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

Commit

Permalink
fix: update slot on ably when it's changed on new io
Browse files Browse the repository at this point in the history
  • Loading branch information
carlossantos74 committed Mar 20, 2024
1 parent f3779bd commit 62e0222
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 24 deletions.
9 changes: 8 additions & 1 deletion src/core/launcher/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ export class Launcher extends Observable implements DefaultLauncher {
private onParticipantJoinedIOC = (presence: Socket.PresenceEvent<Participant>): void => {
if (presence.id === this.participant.value.id) {
// Assign a slot to the participant
SlotService.register(this.LauncherRealtimeRoom, this.realtime, this.participant.value);
SlotService.register(this.LauncherRealtimeRoom, this.participant.value);
this.LauncherRealtimeRoom.presence.update<Participant>(this.participant.value);
}

Expand Down Expand Up @@ -420,6 +420,13 @@ export class Launcher extends Observable implements DefaultLauncher {
this.publish(ParticipantEvent.LOCAL_UPDATED, presence.data);

this.logger.log('Publishing ParticipantEvent.UPDATED', presence.data);

if (
presence.data?.slot?.index !== undefined &&
presence.data?.slot?.index !== this.realtime.participant.data.slotIndex
) {
this.realtime.updateMyProperties({ slotIndex: presence.data.slot.index });
}
}

this.participants.value.set(presence.id, presence.data);
Expand Down
8 changes: 4 additions & 4 deletions src/services/slot/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('slot service', () => {
id: '123',
} as any;

const instance = new SlotService(room, { updateMyProperties: jest.fn() } as any, participant);
const instance = new SlotService(room, participant);
await instance['assignSlot']();

expect(instance['slotIndex']).toBeDefined();
Expand Down Expand Up @@ -47,7 +47,7 @@ describe('slot service', () => {
id: '123',
} as any;

const instance = new SlotService(room, { updateMyProperties: jest.fn() } as any, participant);
const instance = new SlotService(room, participant);
await instance['assignSlot']();

expect(instance['slotIndex']).toBeDefined();
Expand Down Expand Up @@ -80,7 +80,7 @@ describe('slot service', () => {
id: '123',
} as any;

const instance = new SlotService(room, { updateMyProperties: jest.fn() } as any, participant);
const instance = new SlotService(room, participant);
await instance['assignSlot']();

expect(instance['slotIndex']).toBeUndefined();
Expand Down Expand Up @@ -114,7 +114,7 @@ describe('slot service', () => {
id: '123',
} as any;

const instance = new SlotService(room, { updateMyProperties: jest.fn() } as any, participant);
const instance = new SlotService(room, participant);
await instance['assignSlot']();

expect(instance['slotIndex']).toBeDefined();
Expand Down
32 changes: 13 additions & 19 deletions src/services/slot/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,20 @@ export class SlotService {
private room: Socket.Room;
private participant: Participant;
private slotIndex: number;
private realtime: AblyRealtimeService;
private static instance: SlotService;

// @NOTE - reciving old realtime service instance until we migrate to new IO
constructor(room: Socket.Room, realtime: AblyRealtimeService, participant: Participant) {
constructor(room: Socket.Room, 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,
) {
public static register(room: Socket.Room, participant: Participant) {
if (!SlotService.instance) {
SlotService.instance = new SlotService(room, realtime, participant);
SlotService.instance = new SlotService(room, participant);
}

return SlotService.instance;
Expand Down Expand Up @@ -89,16 +83,16 @@ export class SlotService {

// @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,
});
// if (!this.realtime.isJoinedRoom) {
// await new Promise((resolve) => {
// setTimeout(resolve, 1500);
// });
// }

// this.realtime.updateMyProperties({
// slotIndex: slot,
// slot: slotData,
// });
})
.catch((error) => {
this.room.presence.update({
Expand Down

0 comments on commit 62e0222

Please sign in to comment.