Skip to content

Commit

Permalink
#10: moves update timer to service
Browse files Browse the repository at this point in the history
  • Loading branch information
Dirk Peter committed Jun 12, 2024
1 parent 70c71e3 commit fdd9409
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 24 deletions.
22 changes: 0 additions & 22 deletions plugin/components/reminder-drawer/reminder-drawer.component.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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));
}
}
24 changes: 22 additions & 2 deletions plugin/services/reminder.service.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -25,6 +25,7 @@ export class ReminderService {
private drawerRef: ComponentRef<unknown>;
private drawer: ReminderDrawerComponent;
private subscription = new Subscription();
private updateTimer: NodeJS.Timeout;
private _reminderCounter = 0;
private _reminders: Reminder[] = [];

Expand All @@ -35,6 +36,7 @@ export class ReminderService {
this._reminders = reminders;
this.reminders$.next(this._reminders);
this.updateCounter();
this.setUpdateTimer();
}

private get reminderCounter(): number {
Expand Down Expand Up @@ -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)
);
}
}

0 comments on commit fdd9409

Please sign in to comment.