BREAKING CHANGE(watchers): Replace polling-based write stabilization with debounce-based event coalescing and simplify watcher options
This commit is contained in:
@@ -65,10 +65,9 @@ export class DenoWatcher implements IWatcher {
|
||||
private watcher: ReturnType<typeof Deno.watchFs> | null = null;
|
||||
private watchedFiles: Set<string> = new Set();
|
||||
private _isWatching = false;
|
||||
private abortController: AbortController | null = null;
|
||||
private recentEvents: Map<string, number> = new Map();
|
||||
private throttleMs = 50;
|
||||
private pendingWrites: Map<string, ReturnType<typeof setTimeout>> = new Map();
|
||||
|
||||
// Debounce: pending emits per file path
|
||||
private pendingEmits: Map<string, ReturnType<typeof setTimeout>> = new Map();
|
||||
|
||||
public readonly events$ = new smartrx.rxjs.Subject<IWatchEvent>();
|
||||
|
||||
@@ -97,8 +96,6 @@ export class DenoWatcher implements IWatcher {
|
||||
}
|
||||
|
||||
try {
|
||||
this.abortController = new AbortController();
|
||||
|
||||
// Start watching all base paths
|
||||
this.watcher = Deno.watchFs(this.options.basePaths, { recursive: true });
|
||||
this._isWatching = true;
|
||||
@@ -122,11 +119,11 @@ export class DenoWatcher implements IWatcher {
|
||||
async stop(): Promise<void> {
|
||||
this._isWatching = false;
|
||||
|
||||
// Cancel all pending write stabilizations
|
||||
for (const timeout of this.pendingWrites.values()) {
|
||||
// Cancel all pending debounced emits
|
||||
for (const timeout of this.pendingEmits.values()) {
|
||||
clearTimeout(timeout);
|
||||
}
|
||||
this.pendingWrites.clear();
|
||||
this.pendingEmits.clear();
|
||||
|
||||
// Close the watcher
|
||||
if (this.watcher) {
|
||||
@@ -135,7 +132,6 @@ export class DenoWatcher implements IWatcher {
|
||||
}
|
||||
|
||||
this.watchedFiles.clear();
|
||||
this.recentEvents.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -153,7 +149,7 @@ export class DenoWatcher implements IWatcher {
|
||||
}
|
||||
|
||||
for (const filePath of event.paths) {
|
||||
await this.handleDenoEvent(event.kind, filePath);
|
||||
this.handleDenoEvent(event.kind, filePath);
|
||||
}
|
||||
}
|
||||
} catch (error: any) {
|
||||
@@ -164,12 +160,12 @@ export class DenoWatcher implements IWatcher {
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle a Deno file system event
|
||||
* Handle a Deno file system event - debounce and normalize
|
||||
*/
|
||||
private async handleDenoEvent(
|
||||
private handleDenoEvent(
|
||||
kind: 'create' | 'modify' | 'remove' | 'access' | 'any' | 'other',
|
||||
filePath: string
|
||||
): Promise<void> {
|
||||
): void {
|
||||
// Ignore 'access' events (just reading the file)
|
||||
if (kind === 'access') {
|
||||
return;
|
||||
@@ -180,14 +176,30 @@ export class DenoWatcher implements IWatcher {
|
||||
return;
|
||||
}
|
||||
|
||||
// Throttle duplicate events
|
||||
if (!this.shouldEmit(filePath, kind)) {
|
||||
return;
|
||||
// Debounce: cancel any pending emit for this file
|
||||
const existing = this.pendingEmits.get(filePath);
|
||||
if (existing) {
|
||||
clearTimeout(existing);
|
||||
}
|
||||
|
||||
// Schedule debounced emit
|
||||
const timeout = setTimeout(() => {
|
||||
this.pendingEmits.delete(filePath);
|
||||
this.emitFileEvent(filePath, kind);
|
||||
}, this.options.debounceMs);
|
||||
|
||||
this.pendingEmits.set(filePath, timeout);
|
||||
}
|
||||
|
||||
/**
|
||||
* Emit the actual file event after debounce
|
||||
*/
|
||||
private async emitFileEvent(
|
||||
filePath: string,
|
||||
kind: 'create' | 'modify' | 'remove' | 'access' | 'any' | 'other'
|
||||
): Promise<void> {
|
||||
try {
|
||||
if (kind === 'create') {
|
||||
// Create events (atomic saves) don't need stabilization - file is already complete
|
||||
const stats = await this.statSafe(filePath);
|
||||
if (stats) {
|
||||
this.watchedFiles.add(filePath);
|
||||
@@ -195,16 +207,9 @@ export class DenoWatcher implements IWatcher {
|
||||
this.events$.next({ type: eventType, path: filePath, stats });
|
||||
}
|
||||
} else if (kind === 'modify') {
|
||||
// Modify events are in-place writes - use stabilization
|
||||
const stats = await this.statSafe(filePath);
|
||||
if (stats && !stats.isDirectory()) {
|
||||
// Wait for write to stabilize
|
||||
await this.waitForWriteFinish(filePath);
|
||||
const finalStats = await this.statSafe(filePath);
|
||||
|
||||
if (finalStats) {
|
||||
this.events$.next({ type: 'change', path: filePath, stats: finalStats });
|
||||
}
|
||||
this.events$.next({ type: 'change', path: filePath, stats });
|
||||
}
|
||||
} else if (kind === 'remove') {
|
||||
const wasDirectory = this.isKnownDirectory(filePath);
|
||||
@@ -219,52 +224,6 @@ export class DenoWatcher implements IWatcher {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Wait for file write to complete (polling-based)
|
||||
*/
|
||||
private async waitForWriteFinish(filePath: string): Promise<void> {
|
||||
return new Promise((resolve) => {
|
||||
let lastSize = -1;
|
||||
let lastChange = Date.now();
|
||||
const startTime = Date.now();
|
||||
|
||||
const poll = async () => {
|
||||
try {
|
||||
const stats = await this.statSafe(filePath);
|
||||
if (!stats) {
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
|
||||
const now = Date.now();
|
||||
|
||||
// Check if we've exceeded max wait time - resolve immediately
|
||||
if (now - startTime >= this.options.maxWaitTime) {
|
||||
this.pendingWrites.delete(filePath);
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
|
||||
if (stats.size !== lastSize) {
|
||||
lastSize = stats.size;
|
||||
lastChange = now;
|
||||
this.pendingWrites.set(filePath, setTimeout(poll, this.options.pollInterval));
|
||||
} else if (now - lastChange >= this.options.stabilityThreshold) {
|
||||
this.pendingWrites.delete(filePath);
|
||||
resolve();
|
||||
} else {
|
||||
this.pendingWrites.set(filePath, setTimeout(poll, this.options.pollInterval));
|
||||
}
|
||||
} catch {
|
||||
this.pendingWrites.delete(filePath);
|
||||
resolve();
|
||||
}
|
||||
};
|
||||
|
||||
this.pendingWrites.set(filePath, setTimeout(poll, this.options.pollInterval));
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Scan directory and emit 'add' events for existing files
|
||||
*/
|
||||
@@ -276,6 +235,12 @@ export class DenoWatcher implements IWatcher {
|
||||
try {
|
||||
for await (const entry of Deno.readDir(dirPath)) {
|
||||
const fullPath = `${dirPath}/${entry.name}`;
|
||||
|
||||
// Skip temp files during initial scan too
|
||||
if (this.isTemporaryFile(fullPath)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const stats = await this.statSafe(fullPath);
|
||||
|
||||
if (!stats) {
|
||||
@@ -322,31 +287,4 @@ export class DenoWatcher implements IWatcher {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Throttle duplicate events
|
||||
*/
|
||||
private shouldEmit(filePath: string, eventType: string): boolean {
|
||||
const key = `${filePath}:${eventType}`;
|
||||
const now = Date.now();
|
||||
const lastEmit = this.recentEvents.get(key);
|
||||
|
||||
if (lastEmit && now - lastEmit < this.throttleMs) {
|
||||
return false;
|
||||
}
|
||||
|
||||
this.recentEvents.set(key, now);
|
||||
|
||||
// Clean up old entries periodically
|
||||
if (this.recentEvents.size > 1000) {
|
||||
const cutoff = now - this.throttleMs * 2;
|
||||
for (const [k, time] of this.recentEvents) {
|
||||
if (time < cutoff) {
|
||||
this.recentEvents.delete(k);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user