diff --git a/src/services/NotificationService.ts b/src/services/NotificationService.ts index 3613803..76651bd 100644 --- a/src/services/NotificationService.ts +++ b/src/services/NotificationService.ts @@ -19,7 +19,9 @@ export class NotificationService { return this._lastRefreshedTimeMs; } - constructor(private repository: GetterRepository) {} + constructor(private repository: GetterRepository) { + this.etaSubscriberCallback = this.etaSubscriberCallback.bind(this); + } /** * An object of device ID arrays to deliver notifications to. @@ -79,13 +81,19 @@ export class NotificationService { return; } - await Promise.all(this.deviceIdsToDeliverTo[tuple.toString()].map(async (deviceId) => { - await this.sendEtaNotificationImmediately({ + const indicesToRemove = new Set(); + await Promise.all(this.deviceIdsToDeliverTo[tuple.toString()].map(async (deviceId, index) => { + const deliveredSuccessfully = await this.sendEtaNotificationImmediately({ deviceId, shuttleId: eta.shuttleId, stopId: eta.stopId, }); + if (deliveredSuccessfully) { + indicesToRemove.add(index); + } })); + + this.deviceIdsToDeliverTo[tuple.toString()] = this.deviceIdsToDeliverTo[tuple.toString()].filter((_, index) => !indicesToRemove.has(index)); } /** @@ -102,7 +110,6 @@ export class NotificationService { this.deviceIdsToDeliverTo[tuple.toString()].push(deviceId); } - // Refresh the subscriber with the updated array if needed this.repository.unsubscribeFromEtaUpdates(this.etaSubscriberCallback); this.repository.subscribeToEtaUpdates(this.etaSubscriberCallback);