0

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?

skjindal93
  • 706
  • 1
  • 16
  • 34

1 Answers1

1

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.

JKillian
  • 18,061
  • 8
  • 41
  • 74
  • I am getting this error "Specialized overload signature is not assignable to any non-specialized signature" – skjindal93 Jan 03 '17 at 07:06
  • @skjindal93 that's an old TypeScript bug, it'll be fixed if you upgrade to TS 2.0 or newer. I also updated my answer to include a fix for older versions of TS in case you can't update – JKillian Jan 03 '17 at 07:40