UNPKG

@atlaskit/editor-common

Version:

A package that contains common classes and components for editor and renderer

80 lines 2.91 kB
import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; var _AnalyticsQueue; import memoizeOne from 'memoize-one'; export var AnalyticsQueue = /*#__PURE__*/function () { function AnalyticsQueue() { _classCallCheck(this, AnalyticsQueue); _defineProperty(this, "tasks", []); _defineProperty(this, "running", false); } return _createClass(AnalyticsQueue, [{ key: "request", value: function request(fn) { // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-explicit-any if (window.requestIdleCallback) { // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-explicit-any window.requestIdleCallback(fn); } else { var start = performance.now(); setTimeout(function () { fn({ didTimeout: false, timeRemaining: function timeRemaining() { return Math.max(0, 50 - (performance.now() - start)); } }); }, 0); } } }, { key: "pending", value: function pending() { var _window$navigator; // Defensive coding as navigator.scheduling.isInputPending is an experimental API // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-explicit-any if (typeof ((_window$navigator = window.navigator) === null || _window$navigator === void 0 || (_window$navigator = _window$navigator.scheduling) === null || _window$navigator === void 0 ? void 0 : _window$navigator.isInputPending) === 'function') { var _window$navigator2; // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-explicit-any return ((_window$navigator2 = window.navigator) === null || _window$navigator2 === void 0 || (_window$navigator2 = _window$navigator2.scheduling) === null || _window$navigator2 === void 0 ? void 0 : _window$navigator2.isInputPending()) === true; } return false; } }, { key: "process", value: function process() { var _this = this; if (this.running) { return; } this.running = true; this.request(function (deadline) { while (deadline.timeRemaining() > 0 && _this.tasks.length > 0 && !_this.pending()) { var task = _this.tasks.shift(); if (task) { task(); } } _this.running = false; if (_this.tasks.length > 0) { _this.process(); } }); } }, { key: "schedule", value: function schedule(task) { this.tasks.push(task); this.process(); } }]); }(); _AnalyticsQueue = AnalyticsQueue; _defineProperty(AnalyticsQueue, "get", memoizeOne(function () { return new _AnalyticsQueue(); }));