Skip to content

Commit

Permalink
Use custom event target instead of window
Browse files Browse the repository at this point in the history
  • Loading branch information
bgrgicak committed Mar 12, 2024
1 parent f447ca9 commit f8694b8
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions packages/php-wasm/logger/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand All @@ -39,6 +39,7 @@ export class Logger {
private errorLogPath = '/wordpress/wp-content/debug.log';

constructor(errorLogPath?: string) {
super();
if (errorLogPath) {
this.errorLogPath = errorLogPath;
}
Expand Down Expand Up @@ -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(),
},
})
);
}

/**
Expand Down Expand Up @@ -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);
}

0 comments on commit f8694b8

Please sign in to comment.