Skip to content

Instantly share code, notes, and snippets.

@petamoriken
Last active November 13, 2020 10:24
Show Gist options
  • Save petamoriken/97e29b6c07c8f9fbb7fa68fa9993f8ef to your computer and use it in GitHub Desktop.
Save petamoriken/97e29b6c07c8f9fbb7fa68fa9993f8ef to your computer and use it in GitHub Desktop.
let isSupportedAbortSignalOption = false;
try {
const options = {
get signal() {
isSupportedAbortSignalOption = true;
return;
},
};
(window.addEventListener as any)("test", null, options);
} catch (e) {}
if (!isSupportedAbortSignalOption) {
const EventTargetPrototype = EventTarget.prototype;
const original = EventTargetPrototype.addEventListener;
const wrapper: typeof original = function(type: string, callback: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions) {
const target = this as EventTarget;
original.call(target, type, callback, options);
if (options == null || typeof options === "boolean") {
return;
}
if (!((options as any).signal instanceof AbortSignal)) {
return;
}
const signal = (options as any).signal as AbortSignal;
signal.addEventListener("abort", () => {
target.removeEventListener(type, callback, options);
}, { once: true });
};
EventTargetPrototype.addEventListener = wrapper;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment