UNPKG

performance-event-timing-polyfill

Version:
2 lines (1 loc) 5.92 kB
var performanceEventTimingPolyfill=function(e){"use strict";const t={pointer:["pointerdown","pointerup","click"],keyboard:["keydown","keyup"]},n=["click","auxclick","keyup","pointercancel","contextmenu"],r=[],i={},o=new MessageChannel,s=["pointerdown","mousedown","pointerup","mouseup","click","auxclick","contextmenu","pointercancel","keydown","keypress","keyup"],a={},c=new Set;let p=!1,f=0,l=[],u=0,d=0,h=null,y=performance.now();const m=e=>{r.push(e)},v=e=>{window.addEventListener(e,(t=>{const n=performance.now();switch(e){case"pointerdown":case"keydown":u++}if(c.has(u))return;const r={startTime:n,duration:0,target:t.target,eventType:t.type,eventTime:t.timeStamp,interactionId:u,paintEnd:0,endTime:0,inputDelay:n-t.timeStamp,processingDuration:0,presentationDelay:0};E(t,r),T(r)}),!0)},w=()=>{y=performance.now(),requestAnimationFrame(w)},T=e=>{clearTimeout(f);const t=i[y];if(t||(i[y]=[]),i[y].push(e),t)return;const n=y;requestAnimationFrame((()=>{const e=y;g((t=>{const r=t-e;i[n].forEach((e=>{e.presentationDelay=r,e.paintEnd=t})),f=setTimeout(b,20),delete i[n]}))}))},g=e=>{let t=!1;requestAnimationFrame((n=>{t||(e(y,"animationFrame"),t=!0)})),o.port2.postMessage((e=>(a[d]=e,d++))((n=>{t||(e(performance.now(),"channelMessage"),t=!0)}))),h=n=>{t||(e(performance.now(),"eventFired"),t=!0)}},E=(e,t)=>{h&&h(e);const n=e.type;new Promise((e=>{g((t=>{e({evName:n,timeStamp:t})}))})).then((({timeStamp:e})=>{const n=e-t.startTime;t.duration=n,t.endTime=e,t.processingDuration=n,l.push(t)}))},b=()=>{if(!l.length)return;const e=(()=>{if(!l.length)return[];const e={};return l.forEach((t=>{e[t.interactionId]||(e[t.interactionId]=[]),e[t.interactionId].push(t)})),Object.values(e).filter((e=>e[e.length-1].paintEnd>0&&n.includes(e[e.length-1].eventType))).flat()})();if(e.length){r.forEach((t=>{t(e)}));for(const t of e)c.add(t.interactionId),l.splice(l.indexOf(t),1)}};var O=this&&this.__classPrivateFieldSet||function(e,t,n,r,i){if("m"===r)throw new TypeError("Private method is not writable");if("a"===r&&!i)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof t?e!==t||!i:!t.has(e))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===r?i.call(e,n):i?i.value=n:t.set(e,n),n},k=this&&this.__classPrivateFieldGet||function(e,t,n,r){if("a"===n&&!r)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof t?e!==t||!r:!t.has(e))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===n?r:"a"===n?r.call(e):r?r.value:t.get(e)};let P=null;const I=["event","first-input"],x="PerformanceEventTiming"in self&&"interactionId"in PerformanceEventTiming.prototype,j=()=>{var e,n,r,i,c;if((()=>{if(!p){p=!0,o.port1.onmessage=e=>{a[e.data](e),delete a[e.data]};for(const e of s)v(e);document.addEventListener("click",(()=>{}),!0),requestAnimationFrame(w)}})(),!("PerformanceObserver"in self))return;if(!window.forceEventTimingPolyfill&&x)return;class f{static fromObject(e){const t=new f;return Object.assign(t,e),t}}f.prototype.startTime=0,f.prototype.duration=0,f.prototype.entryType="event",f.prototype.name="event",f.prototype.cancelable=!0,f.prototype.interactionId=0,f.prototype.processingEnd=0,f.prototype.processingStart=0,f.prototype.target=null,f.prototype.toJSON=function(){const e={};for(const t of Object.keys(this))e[t]=this[t];return JSON.stringify(e)},f.prototype.intoFirstInputEntry=function(){const e={};for(const t of Object.keys(this))e[t]=this[t];return e.entryType="first-input",f.fromObject(e)},self.PerformanceEventTiming=f;class l{constructor(t,n){e.set(this,void 0),O(this,e,t.filter((e=>e.duration>=(n||16))),"f")}getEntries(){return k(this,e,"f")}getEntriesByName(t,n){return k(this,e,"f").filter((e=>e.name===t&&(!n||e.entryType===n)))}getEntriesByType(t){return k(this,e,"f").filter((e=>e.entryType===t))}}e=new WeakMap;class u extends PerformanceObserver{constructor(e){super(e),c.set(this,void 0),this.threshold=104,O(this,c,e,"f")}observe(e){null==e.entryTypes||null==e.durationThreshold&&null==e.buffered&&null==e.type||(e.entryTypes=void 0);let t=!1;if(!e.entryTypes||(e.entryTypes.some((e=>-1==I.indexOf(e)))&&(super.observe(e),t=!0),e.entryTypes.some((e=>I.indexOf(e)>-1))))if(e.type&&-1!=I.indexOf(e.type.toLowerCase()))if(window.forceEventTimingPolyfill||!x||t){if(h.push([k(this,c,"f"),this]),null==e.durationThreshold||e.entryTypes||(this.threshold=Math.max(e.durationThreshold,16)),e.buffered&&!e.entryTypes){if("first-input"==e.type)return void(P&&k(this,c,"f").call(this,new l([P],this.threshold),this));k(this,c,"f").call(this,new l(d,this.threshold),this)}}else super.observe(e);else super.observe(e)}disconnect(){super.disconnect();const e=h.indexOf([k(this,c,"f"),this]);-1!==e&&h.splice(e,1)}static get supportedEntryTypes(){return null==k(n,n,"f",i)&&(O(n,n,Array.from(k(n,n,"f",r).supportedEntryTypes),"f",i),-1==k(n,n,"f",i).indexOf("event")&&(k(n,n,"f",i).push("event"),k(n,n,"f",i).push("first-input"))),k(n,n,"f",i)}}n=u,c=new WeakMap,r={value:PerformanceObserver},i={value:void 0},self.PerformanceObserver=u;const d=[],h=[],y=e=>{const t=e%8;return e+=t>0?8-(8-t<4?-(8-t):8-t):0};m((e=>{const n={},r=Object.values(t).flat();e.forEach((e=>{n[e.interactionId]||(n[e.interactionId]=[]),n[e.interactionId].push(f.fromObject({startTime:e.eventTime,duration:y(Math.round(e.paintEnd-e.eventTime)),interactionId:r.indexOf(e.eventType)>-1?e.interactionId:0,processingStart:e.startTime,processingEnd:e.startTime+e.processingDuration,entryType:"event",name:e.eventType,target:e.target}))}));const i=Object.values(n).flat();d.push(...i);for(const[e,t]of h){const n=new l(i,t.threshold);n.getEntries().length&&e(n,t)}if(!P&&d.length){P=d[0].intoFirstInputEntry();for(const[e,t]of h){const n=new l([P],t.threshold);n.getEntries().length&&e(n,t)}}}))};return j(),e.initPolyfill=j,e.onInteraction=m,e}({});