@atlaskit/editor-plugin-interactivity
Version:
Interactivity plugin for @atlaskit/editor-core
114 lines (109 loc) • 4.45 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.InteractionObserver = void 0;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _bucketBoundaries = require("./bucket-boundaries");
/** `performance.interactionCount` is Chromium-only and absent from the DOM typings. */
var interactionCount = function interactionCount() {
return performance.interactionCount;
};
/** `durationThreshold` is absent from the DOM typings for `PerformanceObserverInit`. */
/**
* Reports the interactions the browser observes to `onEntries`.
*
* `drain` and `stop` are safe to call before `start` and after each other, so a session
* that never started collecting needs no special handling.
*/
var InteractionObserver = exports.InteractionObserver = /*#__PURE__*/function () {
function InteractionObserver(onEntries) {
(0, _classCallCheck2.default)(this, InteractionObserver);
this.onEntries = onEntries;
}
/**
* Starts reporting interactions from this point on. Does nothing when already started, so
* a second call cannot leave an observer running with nobody to disconnect it.
*
* `buffered` is `false`: the entries the browser collected earlier are interactions with
* the page while the editor was still loading, and they belong to no session of ours.
*/
return (0, _createClass2.default)(InteractionObserver, [{
key: "start",
value: function start() {
var _this = this;
if (this.observer) {
return;
}
this.observer = new PerformanceObserver(function (list) {
// Delay by a microtask to work around a Safari bug where the callback is
// invoked synchronously rather than in a separate task.
// See: https://github.com/GoogleChrome/web-vitals/issues/277
Promise.resolve().then(function () {
_this.onEntries(list.getEntries());
});
});
var init = {
type: 'event',
buffered: false,
// 16 ms is also the smallest value the spec honours; lower values are clamped.
durationThreshold: _bucketBoundaries.REPORTING_THRESHOLD_MS
};
this.observer.observe(init);
}
/**
* Synchronously reports the entries the browser has produced but not yet dispatched to
* the callback. A snapshot taken because the page is going away has to include them,
* because there is no later chance to.
*/
}, {
key: "drain",
value: function drain() {
var _this$observer;
var entries = (_this$observer = this.observer) === null || _this$observer === void 0 ? void 0 : _this$observer.takeRecords();
if (entries) {
this.onEntries(entries);
}
}
}, {
key: "stop",
value: function stop() {
var _this$observer2;
(_this$observer2 = this.observer) === null || _this$observer2 === void 0 || _this$observer2.disconnect();
this.observer = undefined;
}
}], [{
key: "isSupported",
value:
/**
* Whether the browser reports both things a session needs: `interactionId`, which groups
* entries into interactions, and `performance.interactionCount`, which counts the ones
* below the reporting threshold. Both are Chromium-only, and without the count
* `totalCount` would be indistinguishable from `observedCount`.
*/
function isSupported() {
if (typeof window === 'undefined' || typeof PerformanceObserver === 'undefined') {
return false;
}
if (!('PerformanceEventTiming' in window) || !('interactionId' in PerformanceEventTiming.prototype)) {
return false;
}
if (!PerformanceObserver.supportedEntryTypes.includes('event')) {
return false;
}
return typeof interactionCount() === 'number';
}
/**
* Total interactions on the page since page load, including those below the reporting
* threshold that no observer ever sees. A property of the page, not of an observer.
*/
}, {
key: "readPageInteractionCount",
value: function readPageInteractionCount() {
var _interactionCount;
return (_interactionCount = interactionCount()) !== null && _interactionCount !== void 0 ? _interactionCount : 0;
}
}]);
}();