diff --git a/src/services/NotificationService.ts b/src/services/NotificationService.ts index 1f52eef..6fee796 100644 --- a/src/services/NotificationService.ts +++ b/src/services/NotificationService.ts @@ -3,7 +3,7 @@ import jwt from "jsonwebtoken"; import fs from "fs"; import { TupleKey } from "../types/TupleKey"; import { IEta } from "../entities/entities"; -import * as http2 from "node:http2"; +import http2 from "http2"; export interface ScheduledNotificationData { deviceId: string; @@ -43,7 +43,7 @@ export class NotificationService { * stop ID, which can be generated using `TupleKey`. * @private */ - private deviceIdsToDeliverTo: { [key: string]: string[] } = {} + private deviceIdsToDeliverTo: { [key: string]: Set } = {} public reloadAPNsTokenIfTimePassed() { if (this.lastReloadedTimeForAPNsIsTooRecent()) { @@ -176,15 +176,17 @@ export class NotificationService { return; } - const indicesToRemove = new Set(); - await Promise.all(this.deviceIdsToDeliverTo[tuple.toString()].map(async (deviceId, index) => { + const deviceIdsToRemove = new Set(); + for (let deviceId of this.deviceIdsToDeliverTo[tuple.toString()].values()) { const deliveredSuccessfully = await this.sendEtaNotificationImmediatelyIfSecondsRemainingBelowThreshold(deviceId, eta); if (deliveredSuccessfully) { - indicesToRemove.add(index); + deviceIdsToRemove.add(deviceId); } - })); + } - this.deviceIdsToDeliverTo[tuple.toString()] = this.deviceIdsToDeliverTo[tuple.toString()].filter((_, index) => !indicesToRemove.has(index)); + deviceIdsToRemove.forEach((deviceId) => { + this.deviceIdsToDeliverTo[tuple.toString()].delete(deviceId); + }); } private async sendEtaNotificationImmediatelyIfSecondsRemainingBelowThreshold(deviceId: string, eta: IEta) { @@ -208,10 +210,9 @@ export class NotificationService { public async scheduleNotification({ deviceId, shuttleId, stopId }: ScheduledNotificationData) { const tuple = new TupleKey(shuttleId, stopId); if (this.deviceIdsToDeliverTo[tuple.toString()] === undefined) { - this.deviceIdsToDeliverTo[tuple.toString()] = [deviceId]; - } else { - this.deviceIdsToDeliverTo[tuple.toString()].push(deviceId); + this.deviceIdsToDeliverTo[tuple.toString()] = new Set(); } + this.deviceIdsToDeliverTo[tuple.toString()].add(deviceId); this.repository.unsubscribeFromEtaUpdates(this.etaSubscriberCallback); this.repository.subscribeToEtaUpdates(this.etaSubscriberCallback); @@ -227,15 +228,12 @@ export class NotificationService { const tupleKey = new TupleKey(shuttleId, stopId); if ( this.deviceIdsToDeliverTo[tupleKey.toString()] === undefined - || !this.deviceIdsToDeliverTo[tupleKey.toString()].includes(deviceId) + || !this.deviceIdsToDeliverTo[tupleKey.toString()].has(deviceId) ) { return; } - const index = this.deviceIdsToDeliverTo[tupleKey.toString()].findIndex(id => id === deviceId); - if (index !== -1) { - this.deviceIdsToDeliverTo[tupleKey.toString()].splice(index, 1); - } + this.deviceIdsToDeliverTo[tupleKey.toString()].delete(deviceId); } /** @@ -249,6 +247,6 @@ export class NotificationService { if (this.deviceIdsToDeliverTo[tuple.toString()] === undefined) { return false; } - return this.deviceIdsToDeliverTo[tuple.toString()].includes(deviceId); + return this.deviceIdsToDeliverTo[tuple.toString()].has(deviceId); } }