typescriptresource-timing-api

Missing onresourcetimingbufferfull event from Performance interface


The resource timing API have events like onresourcetimingbufferfull which gets fired when the limit of 150(by default) resources has been reached. https://www.w3.org/TR/resource-timing-1/#extensions-performance-interface

The Performance interface in lib.d.ts file https://github.com/Microsoft/TypeScript/blob/master/lib/lib.d.ts does not have onresourcetimingbufferfull event defined in it. Is there any way to achieve this?


Solution

  • Most certainly! You can simply augment the Performance interface in your own code:

    // this does not overwrite `Performance`, it just augments it
    interface Performance {
        onresourcetimingbufferfull: (e: Event) => any;
        // and/or perhaps something like
        addEventListener(type: "resourcetimingbufferfull", handler: (e: Event) => any): void;
        // in versions of TS before 2.0, you'll need to add this line also:
        addEventListener(type: string, handler: (e: Event) => any): void;
    }
    
    function bufferFull(e: Event) {
      console.log("WARNING: Resource Timing Buffer is FULL!");
      performance.setResourceTimingBufferSize(200);
    }
    
    performance.onresourcetimingbufferfull = bufferFull;
    performance.addEventListener("resourcetimingbufferfull", bufferFull);
    

    I couldn't find that the resourcetimingbufferfull event carried any special payload so it's easiest just to type it as a regular old Event.