UNPKG

@atlaskit/editor-plugin-interactivity

Version:

Interactivity plugin for @atlaskit/editor-core

48 lines (47 loc) 1.36 kB
import _defineProperty from "@babel/runtime/helpers/defineProperty"; /** * Snapshots are taken 10 s, 30 s and 60 s after the session starts, then every 60 s. The * first three come close together so that short sessions, which are the common case, are * still reported. */ const INITIAL_OFFSETS_MS = [10_000, 30_000, 60_000]; const INTERVAL_MS = 60_000; /** * Fires `onTick` on the snapshot cadence. Timings are offsets from `start()`, so a * restart (a new session in the same editor) restarts the cadence too. */ export class SnapshotScheduler { constructor(onTick) { _defineProperty(this, "tickIndex", 0); this.onTick = onTick; } start() { this.tickIndex = 0; this.scheduleNext(); } restart() { this.stop(); this.start(); } stop() { if (this.timeoutId !== undefined) { window.clearTimeout(this.timeoutId); this.timeoutId = undefined; } } scheduleNext() { this.timeoutId = window.setTimeout(() => { this.tickIndex += 1; this.onTick(); this.scheduleNext(); }, this.delayForNextTick()); } delayForNextTick() { const offset = INITIAL_OFFSETS_MS[this.tickIndex]; if (offset === undefined) { return INTERVAL_MS; } const previousOffset = this.tickIndex === 0 ? 0 : INITIAL_OFFSETS_MS[this.tickIndex - 1]; return offset - previousOffset; } }