Skip to content

Create a Timer Class to monitor and emit events during specified interval #1196

Description

@victor-enogwe

Title

Create a Timer Class to monitor and emit events during specified interval

Description

Implement a Timer class that extends EventEmitter2 to support countdowns based on a start and stop date. This timer will emit events at regular and custom intervals using RxJs, enabling external subscribers to monitor the status and remaining duration of the timer.

Scope

  • Implement Timer class with properties: id (ObjectId), start (Date), stop (Date), and a private RxJs-based timer Observable in milliseconds.
  • Provide the following methods:
    • start(): Public. Starts the timer and emits a start event only once per countdown.
    • stop(): Private. Stops the timer and emits a stop event only once, automatically called on timer expiry.
  • Emit the following events:
    • tick(Duration): Emits every interval with time tick info (millis/seconds/minutes/hours) as specified.
    • start: Emits at the first press of the start button.
    • stop: Emits at timer expiration or user-initiated stop.
    • pause: Emits when paused.
    • resume: Emits on resume.
  • Support custom event listeners (after, every) with typed events, but prevent adding/removing inbuilt event listeners via custom API.
  • Ensure timer can be stopped before stop Date.
  • Allow only a single timer per instance (milliseconds based).
  • Add specs for on, off, once customization (typed for inbuilt/custom events), and event listener management.

Acceptance criteria

  • Timer class supports creation with id, start, and stop properties, and sets up a single milliseconds-based Observable timer internally.
  • Emits tick events at specified frequency/interval during countdown, with proper duration types (ms, s, min, hr).
  • Emits start event once per countdown, and stop event once when timer finishes or is stopped manually.
  • Emits pause and resume events appropriately.
  • Custom event listeners can be registered only for custom events, not for inbuilt ones.
  • Timer can be stopped manually before the stop date.
  • Tests exist for correct timer initialization, event emitting (start/tick/stop/pause/resume), and event listener access restrictions.

Business use case

Accurate and customizable interval timers are critical for scheduling, reminders, and monitoring features of Compass. Providing a robust, event-driven timer enables real-time feedback and greater flexibility for application features relying on precise timing and user notifications.

Additional context

// Example timer
import { timer } from 'rxjs';
import dayjs from 'dayjs';
import { takeUntil } from 'rxjs/operators';

const startDate = dayjs().add(5, 'seconds');
const endDate = startDate.add(10, 'seconds');
const end = timer(endDate.toDate());
const begin = timer(startDate.toDate(), 1000);
const tick = begin.pipe(takeUntil(end));

tick.subscribe(console.log);

Metadata

Metadata

Labels

webFrontend/web related issue

Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions