Skip to content

Commit

Permalink
fix(commons): wait to register hosts listener
Browse files Browse the repository at this point in the history
  • Loading branch information
blakebyrnes committed Feb 25, 2023
1 parent cbd982b commit 02fcddb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
19 changes: 11 additions & 8 deletions commons/config/hosts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@ export default class UlixeeHostsConfig extends TypedEventEmitter<{ change: void
Fs.mkdirSync(this.configPath, { recursive: true });
}

// note supported on linux!
if (process.platform === 'win32' || process.platform === 'darwin') {
this.#watchHandle = Fs.watch(
this.configPath,
{ recursive: true, persistent: false },
this.reload.bind(this, true),
);
}
// not supported on linux!
this.onEventListenerAdded = event => {
if (event !== 'change') return;
if (process.platform === 'win32' || process.platform === 'darwin') {
this.#watchHandle = Fs.watch(
this.configPath,
{ recursive: true, persistent: false },
this.reload.bind(this, true),
);
}
};
this.reload();
}

Expand Down
4 changes: 4 additions & 0 deletions commons/lib/TypedEventEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export default class TypedEventEmitter<T> extends EventEmitter implements ITyped
public storeEventsWithoutListeners = false;
public EventTypes: T;

public onEventListenerAdded?: <K extends keyof T & (string | symbol)>(event: K) => void;

#logger?: IBoundLog;

private pendingIdCounter = 0;
Expand Down Expand Up @@ -103,6 +105,7 @@ export default class TypedEventEmitter<T> extends EventEmitter implements ITyped
includeUnhandledEvents = false,
): this {
super.on(eventType, listenerFn);
this.onListener?.(eventType);
return this.replayOrClearMissedEvents(includeUnhandledEvents, eventType);
}

Expand All @@ -119,6 +122,7 @@ export default class TypedEventEmitter<T> extends EventEmitter implements ITyped
includeUnhandledEvents = false,
): this {
super.once(eventType, listenerFn);
this.onListener?.(eventType);
return this.replayOrClearMissedEvents(includeUnhandledEvents, eventType);
}

Expand Down

0 comments on commit 02fcddb

Please sign in to comment.