Skip to content

Commit

Permalink
fix: optimise sweep and purge
Browse files Browse the repository at this point in the history
Reduced obj.splice calls- instead of calling splice directly within the loop, which can cause array reindexing and performance issues, i've updated this to collect the elements to be deleted in a separate array - losToDelete - and remove them in a subsequent loop.

Moving the studentsOnlineList.set([...this.los]) call outside the loop ensures that we only update studentsOnlineList once after all the necessary changes have been made.
  • Loading branch information
jouwdan committed Jul 27, 2023
1 parent 4634f83 commit 8b87961
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions packages/tutors-reader-lib/src/services/presence-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,26 @@ export const presenceService = {
},

sweepAndPurge(): void {
this.los.forEach((lo, index, obj) => {
const losToDelete = [];
this.los.forEach((lo) => {
lo.timeout--;
if (lo.timeout == 0) {
obj.splice(index, 1);
this.updateListeners("leave", lo);
this.students.delete(lo.studentId);
studentsOnlineList.set([...this.los]);
studentsOnline.set(this.los.length);
if (lo.timeout === 0) {
losToDelete.push(lo);
}
});

losToDelete.forEach((lo) => {
const index = this.los.indexOf(lo);
if (index !== -1) {
this.los.splice(index, 1);
}

this.updateListeners("leave", lo);
this.students.delete(lo.studentId);
});

studentsOnlineList.set([...this.los]);
studentsOnline.set(this.los.length);
},

async visitUpdate(courseId: string) {
Expand Down

0 comments on commit 8b87961

Please sign in to comment.