fix(watcher.node): Defer events during initial scan, track full event sequences, and harden watcher shutdown

This commit is contained in:
2025-12-11 02:39:38 +00:00
parent ea57de06dd
commit 6f6868f2ad
5 changed files with 207 additions and 97 deletions

View File

@@ -107,8 +107,27 @@ The Node.js watcher includes automatic recovery mechanisms based on learnings fr
- Files created after initial scan are properly detected
- Untracked file deletions emit `unlink` events instead of being silently dropped
**Event Deferral During Initial Scan (v6.2.2+):**
- Events are queued until initial scan completes
- Prevents race conditions where events arrive before `watchedFiles` is populated
- Deferred events are processed after scan completes
**Event Sequence Tracking (v6.2.2+):**
- Debounce now tracks ALL events in sequence, not just the last one
- Prevents losing intermediate events (e.g., add→change→delete no longer loses add)
- Intelligent processing of event sequences:
- Delete+recreate with inode change → emits `unlink` then `add`
- Rapid create+delete → emits both events
- Multiple changes → single `change` event (debouncing)
**Post-Stop Event Guards (v6.2.2+):**
- `handleFsEvent()` returns early if watcher is stopped
- Pending emits are cleared BEFORE setting `_isWatching = false`
- Prevents orphaned timeouts and events after `stop()`
**Verbose logging:**
- All lifecycle events logged with `[smartwatch]` prefix
- Event sequences logged for debugging complex scenarios
- Helps debug watcher issues in production
Example log output:
@@ -118,9 +137,9 @@ Example log output:
[smartwatch] Starting health check (every 30s)
[smartwatch] Watcher started with 1 active watcher(s)
[smartwatch] Health check: 1 watchers active
[smartwatch] Inode changed for ./src: 12345 -> 67890
[smartwatch] fs.watch watches inode, not path - restarting watcher
[smartwatch] Processing event sequence for ./src/file.ts: [rename, rename, change]
[smartwatch] File inode changed (delete+recreate): ./src/file.ts
[smartwatch] Previous inode: 12345, current: 67890
```
### Known fs.watch Limitations