UNPKG

rrweb

Version:
88 lines (85 loc) 2.9 kB
import { __spread } from '../../node_modules/tslib/tslib.es6.js'; import { EventType, IncrementalSource } from '../types.js'; var Timer = (function () { function Timer(actions, speed) { if (actions === void 0) { actions = []; } this.timeOffset = 0; this.actions = actions; this.speed = speed; } Timer.prototype.addAction = function (action) { var index = this.findActionIndex(action); this.actions.splice(index, 0, action); }; Timer.prototype.addActions = function (actions) { var _a; (_a = this.actions).push.apply(_a, __spread(actions)); }; Timer.prototype.start = function () { this.actions.sort(function (a1, a2) { return a1.delay - a2.delay; }); this.timeOffset = 0; var lastTimestamp = performance.now(); var actions = this.actions; var self = this; function check(time) { self.timeOffset += (time - lastTimestamp) * self.speed; lastTimestamp = time; while (actions.length) { var action = actions[0]; if (self.timeOffset >= action.delay) { actions.shift(); action.doAction(); } else { break; } } if (actions.length > 0 || self.liveMode) { self.raf = requestAnimationFrame(check); } } this.raf = requestAnimationFrame(check); }; Timer.prototype.clear = function () { if (this.raf) { cancelAnimationFrame(this.raf); } this.actions.length = 0; }; Timer.prototype.setSpeed = function (speed) { this.speed = speed; }; Timer.prototype.toggleLiveMode = function (mode) { this.liveMode = mode; }; Timer.prototype.findActionIndex = function (action) { var start = 0; var end = this.actions.length - 1; while (start <= end) { var mid = Math.floor((start + end) / 2); if (this.actions[mid].delay < action.delay) { start = mid + 1; } else if (this.actions[mid].delay > action.delay) { end = mid - 1; } else { return mid; } } return start; }; return Timer; }()); function addDelay(event, baselineTime) { if (event.type === EventType.IncrementalSnapshot && event.data.source === IncrementalSource.MouseMove) { var firstOffset = event.data.positions[0].timeOffset; var firstTimestamp = event.timestamp + firstOffset; event.delay = firstTimestamp - baselineTime; return firstTimestamp - baselineTime; } event.delay = event.timestamp - baselineTime; return event.delay; } export { Timer, addDelay };