Last active
November 13, 2020 10:24
-
-
Save petamoriken/97e29b6c07c8f9fbb7fa68fa9993f8ef to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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