1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2025-12-09 16:21:51 +00:00

Manually debounce force reload event

Remove chokidar built-in event debouncing/aggregating and replace it
with a basic custom implementation with a 250ms settle delay.
This commit is contained in:
Даниїл Григор'єв 2025-06-14 22:54:06 +03:00
parent 94b7c754ad
commit b3d0e30c07
No known key found for this signature in database
GPG Key ID: B890DF16341D8C1D
2 changed files with 11 additions and 3 deletions

View File

@ -63,7 +63,7 @@ export class ModLoader extends EventEmitter {
this.locators.set("dev", devLocator);
// If requested, restart automatically when dev mods are modified
devLocator.fsWatcher?.on("all", () => this.forceReload());
devLocator.fsWatcher?.on("all", this.delayedForceReload());
}
/**
@ -115,6 +115,16 @@ export class ModLoader extends EventEmitter {
return this.mods.find(mod => mod.metadata.id === id);
}
private delayedForceReload() {
// Debounce the force reload manually as chokidar won't aggregate events the way we want
// NOTE: The delay chosen here (250ms) is quite arbitrary!
let timeout: NodeJS.Timeout | undefined = undefined;
return () => {
clearTimeout(timeout);
timeout = setTimeout(() => this.forceReload(), 250);
};
}
private async locateAllMods(): Promise<ModLocation[]> {
// Sort locators by priority, lowest number is highest priority
const locators = [...this.locators.entries()].sort(([, a], [, b]) => a.priority - b.priority);

View File

@ -179,8 +179,6 @@ export class DevelopmentModLocator implements ModLocator {
this.fsWatcher = chokidar.watch(this.modFiles, {
persistent: false,
ignoreInitial: true,
awaitWriteFinish: true,
atomic: true,
});
}