@just-in/core
Version:
A TypeScript-first framework for building adaptive digital health interventions.
59 lines • 2.43 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.IntervalTimerEventGenerator = void 0;
const event_queue_1 = require("./event-queue");
class IntervalTimerEventGenerator {
constructor(intervalInMs, eventTypeName, options = {}) {
this.intervalInMs = intervalInMs;
this.eventTypeName = eventTypeName;
this.options = options;
this.intervalId = null;
this.simulatedStartDate = null;
this.useSimulatedStartDate = false;
this.simulatedTickDurationInMs = 10;
this.simulatedTickCountMax = 10;
this.simulatedTickCount = 0;
if (intervalInMs <= 0) {
throw new Error('Interval must be greater than 0');
}
if (!eventTypeName || eventTypeName.trim() === '') {
throw new Error('Event type name is required');
}
this.intervalInMs = intervalInMs;
this.eventTypeName = eventTypeName;
if (options.simulatedStartDate) {
this.simulatedStartDate = new Date(options.simulatedStartDate);
this.useSimulatedStartDate = true;
this.simulatedTickDurationInMs = options.simulatedTickDurationInMs || intervalInMs;
this.simulatedTickCountMax = options.simulatedTickCountMax || 10;
}
}
start() {
const startDate = new Date();
const timerInterval = this.useSimulatedStartDate ? this.simulatedTickDurationInMs : this.intervalInMs;
this.intervalId = setInterval(() => {
let eventTimestamp;
if (this.useSimulatedStartDate) {
eventTimestamp = new Date(this.simulatedStartDate.getTime()
+ this.simulatedTickCount
* this.intervalInMs);
this.simulatedTickCount++;
if (this.simulatedTickCountMax && this.simulatedTickCount >= this.simulatedTickCountMax) {
this.stop();
}
}
else {
eventTimestamp = new Date();
}
(0, event_queue_1.publishEvent)(this.eventTypeName, eventTimestamp);
}, timerInterval);
}
stop() {
if (this.intervalId) {
clearInterval(this.intervalId);
this.intervalId = null;
}
}
}
exports.IntervalTimerEventGenerator = IntervalTimerEventGenerator;
//# sourceMappingURL=interval-timer-event-generator.js.map