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

Commit

Permalink
fix: validade if slot is assigned when is finding my slot
Browse files Browse the repository at this point in the history
  • Loading branch information
carlossantos74 committed Feb 23, 2024
1 parent a72814a commit d600fc3
Showing 1 changed file with 30 additions and 25 deletions.
55 changes: 30 additions & 25 deletions src/services/realtime/ably/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -845,31 +845,40 @@ export default class AblyRealtimeService extends RealtimeService implements Ably
}

presences.forEach((presence) => {
if (presence.data.slotIndex !== undefined && presence.data.slotIndex !== null) {
if (
slots[presence.data.slotIndex].clientId !== null &&
slots[presence.data.slotIndex].clientId !== presence.clientId &&
presence.clientId === this.myParticipant.clientId
) {
this.myParticipant.data.slotIndex = null;
return;
}

slots[presence.data.slotIndex].clientId = presence.clientId;
// if the slot is not defined, ignore it
if (presence.data.slotIndex === null || presence.data.slotIndex === undefined) return;

const isMe = presence.clientId === this.myParticipant.clientId;
const isDuplicated =
slots[presence.data.slotIndex].clientId !== null &&
slots[presence.data.slotIndex].clientId !== presence.clientId;

// if the slot is already taken by me, ignore it
if (isDuplicated && isMe) {
this.myParticipant.data.slotIndex = null;
this.logger.log('slot already taken by', presence.clientId, this.myParticipant.clientId);
return;
}

// if the slot is already taken by someone else, ignore it
if (isDuplicated) return;

slots[presence.data.slotIndex].clientId = presence.clientId;
});
});

// next available slot
const slotToUse = slots.find((slot) => slot.clientId === null);

if (!slotToUse) {
return;
}
// if there is no slot available, ignore it
if (!slotToUse) return;

// if the slot is already taken by me, ignore it
if (slots.find((slot) => slot.clientId === this.myParticipant.clientId)) {
return;
}

// set the slot index
this.myParticipant.data.slotIndex = slotToUse.slotIndex;
this.updateMyProperties({ slotIndex: slotToUse.slotIndex });
};
Expand All @@ -879,13 +888,17 @@ export default class AblyRealtimeService extends RealtimeService implements Ably

this.supervizChannel.presence.get((_, presences) => {
presences.forEach((presence) => {
if (presence.data.slotIndex !== undefined && presence.data.slotIndex !== null) {
const isMe = presence.clientId === this.myParticipant.clientId;
const hasValidSlot =
presence.data.slotIndex !== undefined && presence.data.slotIndex !== null;

if (hasValidSlot) {
slots.push({
slotIndex: presence.data.slotIndex,
clientId: presence.clientId,
timestamp: presence.timestamp,
});
} else if (presence.clientId === this.myParticipant.clientId) {
} else if (isMe) {
this.findSlotIndex();
}
});
Expand All @@ -900,9 +913,7 @@ export default class AblyRealtimeService extends RealtimeService implements Ably
}[]
> = {};

slots.forEach((a, index) => {
if (slots.findIndex((b) => b.slotIndex === a.slotIndex) === index) return;

slots.forEach((a) => {
if (!duplicatesMap[a.slotIndex]) {
duplicatesMap[a.slotIndex] = [];
}
Expand All @@ -911,12 +922,6 @@ export default class AblyRealtimeService extends RealtimeService implements Ably
});

Object.values(duplicatesMap).forEach((arr) => {
if (arr.length === 1 && arr[0].clientId === this.myParticipant.clientId) {
this.findSlotIndex();

return;
}

const ordered = arr.sort((a, b) => a.timestamp - b.timestamp);
ordered.shift();

Expand Down

0 comments on commit d600fc3

Please sign in to comment.