@atlaskit/editor-plugin-limited-mode
Version:
LimitedMode plugin for @atlaskit/editor-core
102 lines (100 loc) • 5.12 kB
JavaScript
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t.return || t.return(); } finally { if (u) throw o; } } }; }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
import { isPerformanceAPIAvailable, isPerformanceObserverLongTaskAvailable } from '@atlaskit/editor-common/is-performance-api-available';
/**
* Wires browser performance signals into the supplied {@link LatchPolicy}.
*
* Holds no configuration and makes no decisions: it measures, and forwards. Everything tunable lives
* on the policy, including the clock it reads.
*
* The measurement mirrors `editor-plugin-base/src/pm-plugins/frozen-editor.ts`, which already runs an
* identical `longtask` observer and per-keystroke rAF measurement for every session to feed
* `ACTION.SLOW_INPUT` / `ACTION.BROWSER_FREEZE`. It is duplicated rather than shared so that this
* experiment stays self-contained inside the limited-mode plugin — no new cross-plugin dependency,
* and deleting it is a single-directory revert if the experiment does not ship. The marginal cost is
* one `performance.now()` and one `requestAnimationFrame` per keystroke.
*/
export var createLatchDetector = function createLatchDetector(_ref) {
var onLatchCriteriaMet = _ref.onLatchCriteriaMet,
policy = _ref.policy;
// One clock for the whole subsystem, so measured durations and the policy's windows agree.
var now = policy.now;
var observer;
var destroyed = false;
var handleEvaluation = function handleEvaluation(evaluation) {
if (evaluation !== 'latched') {
return;
}
// One-way latch: the policy will never evaluate again, so stop paying for the observers.
teardownObservers();
var details = policy.getLatchDetails();
if (details) {
onLatchCriteriaMet(details);
}
};
function teardownObservers() {
var _observer;
(_observer = observer) === null || _observer === void 0 || _observer.disconnect();
observer = undefined;
}
if (isPerformanceObserverLongTaskAvailable()) {
try {
observer = new PerformanceObserver(function (list) {
var _iterator = _createForOfIteratorHelper(list.getEntries()),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var entry = _step.value;
if (policy.isLatched()) {
return;
}
handleEvaluation(policy.recordLongTask(entry.duration));
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
});
observer.observe({
entryTypes: ['longtask']
});
} catch (_unused) {
// `longtask` is unsupported in some browsers even when PerformanceObserver exists. The
// latency criterion alone is still a valid trigger, so carry on without freeze detection.
observer = undefined;
}
}
return {
noteDocumentChange: function noteDocumentChange(_ref2) {
var nodeSizeDelta = _ref2.nodeSizeDelta,
isDocumentReplaced = _ref2.isDocumentReplaced;
if (destroyed || policy.isLatched()) {
return;
}
if (isDocumentReplaced || policy.isBulkChange(nodeSizeDelta)) {
policy.suppress();
}
},
measureInput: function measureInput() {
if (destroyed || policy.isLatched() || !isPerformanceAPIAvailable()) {
return;
}
var start = now();
// Runs after every handleTextInput and all resulting plugin work, but before paint — the
// same measurement point frozen-editor uses, so the numbers are comparable to existing
// SLOW_INPUT telemetry.
requestAnimationFrame(function () {
if (destroyed || policy.isLatched()) {
return;
}
handleEvaluation(policy.recordInputLatency(now() - start));
});
},
destroy: function destroy() {
destroyed = true;
teardownObservers();
}
};
};