feat(watchers): Improve write stabilization and ignore temporary editor files
This commit is contained in:
@@ -3,6 +3,7 @@ import * as fs from 'fs';
|
||||
interface IPendingWrite {
|
||||
lastSize: number;
|
||||
lastChange: number;
|
||||
startTime: number;
|
||||
timeoutId: ReturnType<typeof setTimeout>;
|
||||
resolve: (stats: fs.Stats) => void;
|
||||
reject: (error: Error) => void;
|
||||
@@ -16,8 +17,9 @@ export class WriteStabilizer {
|
||||
private pendingWrites = new Map<string, IPendingWrite>();
|
||||
|
||||
constructor(
|
||||
private stabilityThreshold: number = 300,
|
||||
private pollInterval: number = 100
|
||||
private stabilityThreshold: number = 100,
|
||||
private pollInterval: number = 100,
|
||||
private maxWaitTime: number = 1000
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -28,6 +30,8 @@ export class WriteStabilizer {
|
||||
this.cancel(filePath);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const startTime = Date.now();
|
||||
|
||||
const poll = async () => {
|
||||
try {
|
||||
const stats = await fs.promises.stat(filePath);
|
||||
@@ -40,6 +44,13 @@ export class WriteStabilizer {
|
||||
|
||||
const now = Date.now();
|
||||
|
||||
// Check if we've exceeded max wait time - emit with current stats
|
||||
if (now - pending.startTime >= this.maxWaitTime) {
|
||||
this.pendingWrites.delete(filePath);
|
||||
resolve(stats);
|
||||
return;
|
||||
}
|
||||
|
||||
if (stats.size !== pending.lastSize) {
|
||||
// Size changed - file is still being written, reset timer
|
||||
pending.lastSize = stats.size;
|
||||
@@ -66,7 +77,8 @@ export class WriteStabilizer {
|
||||
|
||||
this.pendingWrites.set(filePath, {
|
||||
lastSize: -1,
|
||||
lastChange: Date.now(),
|
||||
lastChange: startTime,
|
||||
startTime: startTime,
|
||||
timeoutId: setTimeout(poll, this.pollInterval),
|
||||
resolve,
|
||||
reject
|
||||
|
||||
Reference in New Issue
Block a user