@atlaskit/editor-plugin-interactivity
Version:
Interactivity plugin for @atlaskit/editor-core
63 lines (62 loc) • 1.93 kB
JavaScript
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
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.
*/
var INITIAL_OFFSETS_MS = [10000, 30000, 60000];
var INTERVAL_MS = 60000;
/**
* 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 var SnapshotScheduler = /*#__PURE__*/function () {
function SnapshotScheduler(onTick) {
_classCallCheck(this, SnapshotScheduler);
_defineProperty(this, "tickIndex", 0);
this.onTick = onTick;
}
return _createClass(SnapshotScheduler, [{
key: "start",
value: function start() {
this.tickIndex = 0;
this.scheduleNext();
}
}, {
key: "restart",
value: function restart() {
this.stop();
this.start();
}
}, {
key: "stop",
value: function stop() {
if (this.timeoutId !== undefined) {
window.clearTimeout(this.timeoutId);
this.timeoutId = undefined;
}
}
}, {
key: "scheduleNext",
value: function scheduleNext() {
var _this = this;
this.timeoutId = window.setTimeout(function () {
_this.tickIndex += 1;
_this.onTick();
_this.scheduleNext();
}, this.delayForNextTick());
}
}, {
key: "delayForNextTick",
value: function delayForNextTick() {
var offset = INITIAL_OFFSETS_MS[this.tickIndex];
if (offset === undefined) {
return INTERVAL_MS;
}
var previousOffset = this.tickIndex === 0 ? 0 : INITIAL_OFFSETS_MS[this.tickIndex - 1];
return offset - previousOffset;
}
}]);
}();