Repository navigation
Create a Timer Class to monitor and emit events during specified interval #1196
Copy link
Copy link
Closed
Copy link
Description
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
Timerclass with properties:id(ObjectId),start(Date),stop(Date), and a private RxJs-basedtimerObservable in milliseconds. - Provide the following methods:
start(): Public. Starts the timer and emits astartevent only once per countdown.stop(): Private. Stops the timer and emits astopevent 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
stopDate. - Allow only a single timer per instance (milliseconds based).
- Add specs for
on,off,oncecustomization (typed for inbuilt/custom events), and event listener management.
Acceptance criteria
Timerclass supports creation withid,start, andstopproperties, and sets up a single milliseconds-based Observable timer internally.- Emits
tickevents at specified frequency/interval during countdown, with proper duration types (ms, s, min, hr). - Emits
startevent once per countdown, andstopevent once when timer finishes or is stopped manually. - Emits
pauseandresumeevents 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
- Use EventEmitter2, RxJs, and support Observable-based intervals
- See Compass documentation: https://docs.compasscalendar.com/docs
- Roadmap: https://github.com/orgs/SwitchbackTech/projects/4
- Reference implementation details in other timer/event classes as needed.
// 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);Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
webFrontend/web related issueFrontend/web related issue
Type
Projects
- StatusShow more project fieldsDone
Activity