performance-event-timing-polyfill
Version:
Provide a polyfill for the PerformanceEventTiming interface
223 lines (220 loc) • 11 kB
JavaScript
/*
Copyright 2024 Uxify Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
import { initObserver, onInteraction, interactionEvents, } from './eventObserver.js';
let firstInteraction = null;
const supportedTypes = ['event', 'first-input'];
const hasNativeEventTimingSupport = 'PerformanceEventTiming' in self &&
'interactionId' in PerformanceEventTiming.prototype;
export const initPolyfill = () => {
var _PerformanceObserverEntryListPolyfill_entries, _a, _PerformanceObserverPolyfill_nativePO, _PerformanceObserverPolyfill_types, _PerformanceObserverPolyfill_callback;
initObserver();
if (!('PerformanceObserver' in self)) {
return;
}
if (
// @ts-ignore
!window.forceEventTimingPolyfill &&
hasNativeEventTimingSupport) {
return;
}
class PerformanceEventTimingPolyfill {
//constructor(init: PerformanceEventTimingInitPolyfill) {
// Object.assign(this, init);
//}
static fromObject(init) {
const entry = new PerformanceEventTimingPolyfill();
Object.assign(entry, init);
return entry;
}
}
PerformanceEventTimingPolyfill.prototype.startTime = 0;
PerformanceEventTimingPolyfill.prototype.duration = 0;
PerformanceEventTimingPolyfill.prototype.entryType = 'event';
PerformanceEventTimingPolyfill.prototype.name = 'event';
PerformanceEventTimingPolyfill.prototype.cancelable = true;
PerformanceEventTimingPolyfill.prototype.interactionId = 0;
PerformanceEventTimingPolyfill.prototype.processingEnd = 0;
PerformanceEventTimingPolyfill.prototype.processingStart = 0;
PerformanceEventTimingPolyfill.prototype.target = null;
PerformanceEventTimingPolyfill.prototype.toJSON = function () {
const result = {};
for (const key of Object.keys(this)) {
result[key] = this[key];
}
return JSON.stringify(result);
};
PerformanceEventTimingPolyfill.prototype.intoFirstInputEntry = function () {
const clone = {};
for (const key of Object.keys(this)) {
clone[key] = this[key];
}
clone.entryType = 'first-input';
return PerformanceEventTimingPolyfill.fromObject(clone);
};
self.PerformanceEventTiming =
PerformanceEventTimingPolyfill;
class PerformanceObserverEntryListPolyfill {
constructor(entries, threshold) {
_PerformanceObserverEntryListPolyfill_entries.set(this, void 0);
__classPrivateFieldSet(this, _PerformanceObserverEntryListPolyfill_entries, entries.filter((entry) => entry.duration >= (threshold || 16)), "f");
}
getEntries() {
return __classPrivateFieldGet(this, _PerformanceObserverEntryListPolyfill_entries, "f");
}
getEntriesByName(name, type) {
return __classPrivateFieldGet(this, _PerformanceObserverEntryListPolyfill_entries, "f").filter((entry) => entry.name === name && (!type || entry.entryType === type));
}
getEntriesByType(type) {
return __classPrivateFieldGet(this, _PerformanceObserverEntryListPolyfill_entries, "f").filter((entry) => entry.entryType === type);
}
}
_PerformanceObserverEntryListPolyfill_entries = new WeakMap();
// @ts-ignore
class PerformanceObserverPolyfill extends PerformanceObserver {
constructor(callback) {
super(callback);
_PerformanceObserverPolyfill_callback.set(this, void 0);
this.threshold = 104;
__classPrivateFieldSet(this, _PerformanceObserverPolyfill_callback, callback, "f");
}
observe(options) {
if (options.entryTypes != undefined &&
(options.durationThreshold != undefined ||
options.buffered != undefined ||
options.type != undefined)) {
// Not allowed so we just ignore the options
options.entryTypes = undefined;
}
let nativeObserveCalled = false;
if (options.entryTypes) {
if (options.entryTypes.some((type) => supportedTypes.indexOf(type) == -1)) {
super.observe(options);
nativeObserveCalled = true;
}
if (!options.entryTypes.some((type) => supportedTypes.indexOf(type) > -1)) {
return;
}
}
if (!options.type ||
supportedTypes.indexOf(options.type.toLowerCase()) == -1) {
super.observe(options);
return;
}
if (
// @ts-ignore
!window.forceEventTimingPolyfill &&
hasNativeEventTimingSupport &&
!nativeObserveCalled) {
super.observe(options);
return;
}
callbacks.push([__classPrivateFieldGet(this, _PerformanceObserverPolyfill_callback, "f"), this]);
if (options.durationThreshold != undefined && !options.entryTypes) {
this.threshold = Math.max(options.durationThreshold, 16);
}
if (options.buffered && !options.entryTypes) {
if (options.type == 'first-input') {
if (firstInteraction) {
__classPrivateFieldGet(this, _PerformanceObserverPolyfill_callback, "f").call(this, new PerformanceObserverEntryListPolyfill([firstInteraction], this.threshold), this);
}
return;
}
__classPrivateFieldGet(this, _PerformanceObserverPolyfill_callback, "f").call(this, new PerformanceObserverEntryListPolyfill(buffer, this.threshold), this);
}
}
disconnect() {
super.disconnect();
const index = callbacks.indexOf([__classPrivateFieldGet(this, _PerformanceObserverPolyfill_callback, "f"), this]);
if (index !== -1) {
callbacks.splice(index, 1);
}
}
static get supportedEntryTypes() {
if (__classPrivateFieldGet(_a, _a, "f", _PerformanceObserverPolyfill_types) == undefined) {
__classPrivateFieldSet(_a, _a, Array.from(__classPrivateFieldGet(_a, _a, "f", _PerformanceObserverPolyfill_nativePO).supportedEntryTypes), "f", _PerformanceObserverPolyfill_types);
if (__classPrivateFieldGet(_a, _a, "f", _PerformanceObserverPolyfill_types).indexOf('event') == -1) {
__classPrivateFieldGet(_a, _a, "f", _PerformanceObserverPolyfill_types).push('event');
__classPrivateFieldGet(_a, _a, "f", _PerformanceObserverPolyfill_types).push('first-input');
}
}
return __classPrivateFieldGet(_a, _a, "f", _PerformanceObserverPolyfill_types);
}
}
_a = PerformanceObserverPolyfill, _PerformanceObserverPolyfill_callback = new WeakMap();
_PerformanceObserverPolyfill_nativePO = { value: PerformanceObserver };
_PerformanceObserverPolyfill_types = { value: undefined };
// @ts-ignore
self.PerformanceObserver = PerformanceObserverPolyfill;
const buffer = [];
const callbacks = [];
const roundToNearestMultipleOfEight = (num) => {
const r = num % 8;
const adder = 8 - r < 4 ? -(8 - r) : 8 - r;
num += r > 0 ? 8 - adder : 0;
return num;
};
onInteraction((entries) => {
const interactions = {};
const interactionEventsFlat = Object.values(interactionEvents).flat();
entries.forEach((m) => {
if (!interactions[m.interactionId]) {
interactions[m.interactionId] = [];
}
interactions[m.interactionId].push(PerformanceEventTimingPolyfill.fromObject({
startTime: m.eventTime,
//duration: Math.round(m.inputDelay + m.duration + m.presentationDelay),
duration: roundToNearestMultipleOfEight(
//Math.round(m.endTime + m.presentationDelay - m.eventTime),
Math.round(m.paintEnd - m.eventTime)),
interactionId: interactionEventsFlat.indexOf(m.eventType) > -1
? m.interactionId
: 0,
processingStart: m.startTime,
processingEnd: m.startTime + m.processingDuration,
entryType: 'event',
name: m.eventType,
target: m.target,
}));
});
const interactionsListFlat = Object.values(interactions).flat();
buffer.push(...interactionsListFlat);
for (const [callback, observer] of callbacks) {
const interactionsList = new PerformanceObserverEntryListPolyfill(interactionsListFlat, observer.threshold);
if (!interactionsList.getEntries().length) {
continue;
}
callback(interactionsList, observer);
}
if (!firstInteraction && buffer.length) {
firstInteraction = buffer[0].intoFirstInputEntry();
for (const [callback, observer] of callbacks) {
const interactionsList = new PerformanceObserverEntryListPolyfill([firstInteraction], observer.threshold);
if (!interactionsList.getEntries().length) {
continue;
}
callback(interactionsList, observer);
}
}
});
};