@atlaskit/editor-plugin-interactivity
Version:
Interactivity plugin for @atlaskit/editor-core
42 lines (41 loc) • 1.53 kB
JavaScript
/**
* Reports the frames the browser took longer than 50 ms to render to `onFrames`. They are what says
* where the time of a slow interaction went — which script ran the longest while the user waited,
* and how much of the wait was style and layout — which Event Timing cannot answer.
*
* What they say about an interaction is `SlowInteractionList`'s to work out; this only observes.
*/
export class LongAnimationFrameObserver {
static isSupported() {
return typeof PerformanceObserver !== 'undefined' && PerformanceObserver.supportedEntryTypes.includes('long-animation-frame');
}
constructor(onFrames) {
this.onFrames = onFrames;
}
start() {
if (this.observer || !LongAnimationFrameObserver.isSupported()) {
return;
}
this.observer = new PerformanceObserver(list => {
this.onFrames(list.getEntries());
});
// Buffered, as `web-vitals` observes them: a frame reported before the editor mounted can
// still be the one an interaction right after it ran in.
this.observer.observe({
type: 'long-animation-frame',
buffered: true
});
}
drain() {
var _this$observer;
const frames = (_this$observer = this.observer) === null || _this$observer === void 0 ? void 0 : _this$observer.takeRecords();
if (frames) {
this.onFrames(frames);
}
}
stop() {
var _this$observer2;
(_this$observer2 = this.observer) === null || _this$observer2 === void 0 ? void 0 : _this$observer2.disconnect();
this.observer = undefined;
}
}