Skip to content

Commit 9835edf

Browse files
committed
Add a grace period when resuming the workspace file watcher to prevent race conditions
- when applying response, file watcher starts prematurely, e.g. when we created many files └── apps └── editor └── src ├── commands │ ├── apply-response-command │ │ ├── apply-response-command.ts │ │ ├── index.ts │ │ ├── response-processor │ │ │ ├── handlers │ │ │ │ ├── diff-handler.ts │ │ │ │ ├── fast-replace-handler.ts │ │ │ │ └── search-replace-handler.ts │ │ │ └── response-processor.ts │ │ └── utils │ │ ├── preview │ │ │ └── workspace-listener.ts │ │ └── preview-handler.ts │ └── index.ts ├── context │ └── providers │ └── workspace │ └── workspace-provider.ts └── extension.ts
1 parent 0201b04 commit 9835edf

1 file changed

Lines changed: 31 additions & 5 deletions

File tree

‎apps/editor/src/context/providers/workspace/workspace-provider.ts‎

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ export class WorkspaceProvider
8080
private _workspace_view_collapsible_state: vscode.TreeItemCollapsibleState =
8181
vscode.TreeItemCollapsibleState.Collapsed
8282
private _is_file_watcher_paused = false
83+
private _resume_file_watcher_timeout: NodeJS.Timeout | null = null
8384

8485
public set_selected_files_view_collapsible_state(
8586
state: vscode.TreeItemCollapsibleState
@@ -273,6 +274,9 @@ export class WorkspaceProvider
273274
if (this._refresh_timeout) {
274275
clearTimeout(this._refresh_timeout)
275276
}
277+
if (this._resume_file_watcher_timeout) {
278+
clearTimeout(this._resume_file_watcher_timeout)
279+
}
276280

277281
this._token_calculator.dispose()
278282
}
@@ -366,20 +370,42 @@ export class WorkspaceProvider
366370
}
367371

368372
public pause_file_watcher(): void {
373+
if (this._resume_file_watcher_timeout) {
374+
clearTimeout(this._resume_file_watcher_timeout)
375+
this._resume_file_watcher_timeout = null
376+
}
369377
this._is_file_watcher_paused = true
370378
}
371379

372380
public resume_file_watcher(): void {
373-
this._is_file_watcher_paused = false
381+
if (this._resume_file_watcher_timeout) {
382+
clearTimeout(this._resume_file_watcher_timeout)
383+
}
384+
this._resume_file_watcher_timeout = setTimeout(() => {
385+
this._is_file_watcher_paused = false
386+
this._resume_file_watcher_timeout = null
387+
}, 500)
374388
this.refresh()
375389
}
376390

391+
private _check_file_watcher_paused(): boolean {
392+
if (!this._is_file_watcher_paused) return false
393+
if (this._resume_file_watcher_timeout) {
394+
clearTimeout(this._resume_file_watcher_timeout)
395+
this._resume_file_watcher_timeout = setTimeout(() => {
396+
this._is_file_watcher_paused = false
397+
this._resume_file_watcher_timeout = null
398+
}, 500)
399+
}
400+
return true
401+
}
402+
377403
public invalidate_token_counts_for_file(changed_file_path: string) {
378404
this._token_calculator.invalidate_token_counts_for_file(changed_file_path)
379405
}
380406

381407
private _on_file_system_changed(changed_file_path?: string) {
382-
if (this._is_file_watcher_paused) return
408+
if (this._check_file_watcher_paused()) return
383409

384410
if (!changed_file_path) return
385411

@@ -421,7 +447,7 @@ export class WorkspaceProvider
421447
}
422448

423449
private async _handle_file_create(created_file_path?: string): Promise<void> {
424-
if (this._is_file_watcher_paused) return
450+
if (this._check_file_watcher_paused()) return
425451

426452
if (!created_file_path) return
427453

@@ -735,7 +761,7 @@ export class WorkspaceProvider
735761
return items
736762
}
737763
public async load_all_ranges(): Promise<void> {
738-
if (this._is_file_watcher_paused) return
764+
if (this._check_file_watcher_paused()) return
739765

740766
this._file_ranges.clear()
741767
for (const workspace_root of this._workspace_roots) {
@@ -1328,7 +1354,7 @@ export class WorkspaceProvider
13281354
}
13291355

13301356
private async _load_all_gitignore_files(): Promise<void> {
1331-
if (this._is_file_watcher_paused) return
1357+
if (this._check_file_watcher_paused()) return
13321358

13331359
const gitignore_files: string[] = []
13341360
const visited = new Set<string>()

0 commit comments

Comments
 (0)