diff --git a/plugin/components/reminder-drawer/reminder-drawer.component.ts b/plugin/components/reminder-drawer/reminder-drawer.component.ts index 0a87462..f433ca2 100644 --- a/plugin/components/reminder-drawer/reminder-drawer.component.ts +++ b/plugin/components/reminder-drawer/reminder-drawer.component.ts @@ -1,7 +1,5 @@ import { Component, OnDestroy } from '@angular/core'; import { AlertService, HeaderService } from '@c8y/ngx-components'; -import { filter, sortBy } from 'lodash'; -import moment from 'moment'; import { BsModalService } from 'ngx-bootstrap/modal'; import { BehaviorSubject, Subscription } from 'rxjs'; import { @@ -119,25 +117,5 @@ export class ReminderDrawerComponent implements OnDestroy { this.reminders = reminders; this.reminderGroups = this.reminderService.groupReminders(reminders); this.lastUpdate = new Date(); - this.setUpdateTimer(); - } - - private setUpdateTimer(): void { - // TODO update indicator - // - move update timer to service? - const now = moment(); - - clearTimeout(this.updateTimer); - - if (!this.reminders.length) return; - - const closestReminder = sortBy( - filter(this.reminders, (r) => r.status !== ReminderStatus.cleared && moment(r.time) > now), - 'time' - )[0]; - - if (!closestReminder) return; - - this.updateTimer = setTimeout(() => this.digestReminders(this.reminders), moment(closestReminder.time).diff(now)); } } diff --git a/plugin/services/reminder.service.ts b/plugin/services/reminder.service.ts index 5d143d4..61018dd 100644 --- a/plugin/services/reminder.service.ts +++ b/plugin/services/reminder.service.ts @@ -1,10 +1,10 @@ import { ComponentRef, Injectable } from '@angular/core'; import { EventService, IEvent, IResult } from '@c8y/client'; import { EventRealtimeService, RealtimeMessage } from '@c8y/ngx-components'; -import { cloneDeep, has, sortBy } from 'lodash'; +import { cloneDeep, filter as _filter, has, sortBy } from 'lodash'; import moment from 'moment'; import { BehaviorSubject, Subscription } from 'rxjs'; -import { filter, map } from 'rxjs/operators'; +import { map, filter } from 'rxjs/operators'; import { ReminderDrawerComponent } from '../components/reminder-drawer/reminder-drawer.component'; import { Reminder, @@ -25,6 +25,7 @@ export class ReminderService { private drawerRef: ComponentRef; private drawer: ReminderDrawerComponent; private subscription = new Subscription(); + private updateTimer: NodeJS.Timeout; private _reminderCounter = 0; private _reminders: Reminder[] = []; @@ -35,6 +36,7 @@ export class ReminderService { this._reminders = reminders; this.reminders$.next(this._reminders); this.updateCounter(); + this.setUpdateTimer(); } private get reminderCounter(): number { @@ -264,4 +266,22 @@ export class ReminderService { this.reminderCounter = count; } + + private setUpdateTimer(): void { + const now = moment(); + + clearTimeout(this.updateTimer); + + if (!this.reminders || !this.reminders.length) return; + + const dueReminders = _filter(this.reminders, (r) => r.status !== ReminderStatus.cleared && moment(r.time) > now); + const closestReminder: Reminder = sortBy(dueReminders, 'time')[0]; + + if (!closestReminder) return; + + this.updateTimer = setTimeout( + () => (this.reminders = this.digestReminders(this.reminders)), + moment(closestReminder.time).diff(now) + ); + } }