@atlaskit/editor-plugin-interactivity
Version:
Interactivity plugin for @atlaskit/editor-core
70 lines (68 loc) • 2.28 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.SnapshotScheduler = void 0;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _defineProperty2 = _interopRequireDefault(require("@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.
*/
var SnapshotScheduler = exports.SnapshotScheduler = /*#__PURE__*/function () {
function SnapshotScheduler(onTick) {
(0, _classCallCheck2.default)(this, SnapshotScheduler);
(0, _defineProperty2.default)(this, "tickIndex", 0);
this.onTick = onTick;
}
return (0, _createClass2.default)(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;
}
}]);
}();