From f8694b8d16733fad22608eeda527b5b619bc720d Mon Sep 17 00:00:00 2001 From: Bero Date: Tue, 12 Mar 2024 10:31:19 +0100 Subject: [PATCH] Use custom event target instead of window --- packages/php-wasm/logger/src/logger.ts | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/packages/php-wasm/logger/src/logger.ts b/packages/php-wasm/logger/src/logger.ts index 1dc3c31c57..3c21d33e47 100644 --- a/packages/php-wasm/logger/src/logger.ts +++ b/packages/php-wasm/logger/src/logger.ts @@ -15,7 +15,7 @@ export type LogPrefix = 'Playground' | 'PHP-WASM'; /** * A logger for Playground. */ -export class Logger { +export class Logger extends EventTarget { public readonly fatalErrorEvent = 'playground-fatal-error'; /** @@ -39,6 +39,7 @@ export class Logger { private errorLogPath = '/wordpress/wp-content/debug.log'; constructor(errorLogPath?: string) { + super(); if (errorLogPath) { this.errorLogPath = errorLogPath; } @@ -79,15 +80,13 @@ export class Logger { } public maybeDispatchFatalErrorEvent() { - if (typeof window !== 'undefined') { - window.dispatchEvent( - new CustomEvent(this.fatalErrorEvent, { - detail: { - logs: this.getLogs(), - }, - }) - ); - } + this.dispatchEvent( + new CustomEvent(this.fatalErrorEvent, { + detail: { + logs: this.getLogs(), + }, + }) + ); } /** @@ -255,8 +254,5 @@ export function addFatalErrorListener( loggerInstance: Logger, callback: EventListenerOrEventListenerObject ) { - if (typeof window === 'undefined') { - return; - } - window.addEventListener(loggerInstance.fatalErrorEvent, callback); + loggerInstance.addEventListener(loggerInstance.fatalErrorEvent, callback); }