mirror of
https://github.com/LuanRT/YouTube.js.git
synced 2026-06-19 04:21:35 +00:00
refactor(shim): Move node CustomEvent polyfill to Platform.shim (#357)
This commit is contained in:
@@ -1,22 +1,4 @@
|
||||
// Polyfill CustomEvents on node
|
||||
if (!Reflect.has(globalThis, 'CustomEvent')) {
|
||||
|
||||
// See https://github.com/nodejs/node/issues/40678#issuecomment-1126944677
|
||||
class CustomEvent extends Event {
|
||||
#detail;
|
||||
|
||||
constructor(type: string, options?: CustomEventInit<any[]>) {
|
||||
super(type, options);
|
||||
this.#detail = options?.detail ?? null;
|
||||
}
|
||||
|
||||
get detail() {
|
||||
return this.#detail;
|
||||
}
|
||||
}
|
||||
|
||||
Reflect.set(globalThis, 'CustomEvent', CustomEvent);
|
||||
}
|
||||
import { Platform } from './Utils.js';
|
||||
|
||||
export default class EventEmitterLike extends EventTarget {
|
||||
#legacy_listeners = new Map<(...args: any[]) => void, EventListener>();
|
||||
@@ -26,13 +8,13 @@ export default class EventEmitterLike extends EventTarget {
|
||||
}
|
||||
|
||||
emit(type: string, ...args: any[]) {
|
||||
const event = new CustomEvent(type, { detail: args });
|
||||
const event = new Platform.shim.CustomEvent(type, { detail: args });
|
||||
this.dispatchEvent(event);
|
||||
}
|
||||
|
||||
on(type: string, listener: (...args: any[]) => void) {
|
||||
const wrapper: EventListener = (ev) => {
|
||||
if (ev instanceof CustomEvent) {
|
||||
if (ev instanceof Platform.shim.CustomEvent) {
|
||||
listener(...ev.detail);
|
||||
} else {
|
||||
listener(ev);
|
||||
@@ -44,7 +26,7 @@ export default class EventEmitterLike extends EventTarget {
|
||||
|
||||
once(type: string, listener: (...args: any[]) => void) {
|
||||
const wrapper: EventListener = (ev) => {
|
||||
if (ev instanceof CustomEvent) {
|
||||
if (ev instanceof Platform.shim.CustomEvent) {
|
||||
listener(...ev.detail);
|
||||
} else {
|
||||
listener(ev);
|
||||
|
||||
Reference in New Issue
Block a user