Skip to content

Commit 2d85914

Browse files
committed
Refactor file processing logic to use procedural loops instead of promise-based mapping
- make sure files are processed sequentially to avoid race conditions
1 parent 50594e9 commit 2d85914

2 files changed

Lines changed: 28 additions & 37 deletions

File tree

apps/editor/src/commands/apply-chat-response-command/response-processor.ts

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -119,41 +119,32 @@ export const process_chat_response = async (
119119
const workspace_roots = workspace_provider.get_workspace_roots()
120120
const all_paths_to_process = new Set<string>(relevant_files_item.file_paths)
121121

122-
const files_for_modal = (
123-
await Promise.all(
124-
Array.from(all_paths_to_process).map(async (rel_path) => {
125-
let absolute_path: string | undefined
126-
for (const root of workspace_roots) {
127-
const potential = path.join(root, rel_path)
128-
if (fs.existsSync(potential)) {
129-
absolute_path = potential
130-
break
131-
}
132-
}
122+
const files_for_modal: {
123+
file_path: string
124+
relative_path: string
125+
token_count: number
126+
}[] = []
133127

134-
let token_count: number | undefined
135-
if (absolute_path) {
136-
const count =
137-
await workspace_provider.calculate_file_tokens(absolute_path)
138-
token_count = count.total
139-
}
128+
for (const rel_path of Array.from(all_paths_to_process)) {
129+
let absolute_path: string | undefined
130+
for (const root of workspace_roots) {
131+
const potential = path.join(root, rel_path)
132+
if (fs.existsSync(potential)) {
133+
absolute_path = potential
134+
break
135+
}
136+
}
140137

141-
return {
142-
file_path: absolute_path,
143-
relative_path: rel_path,
144-
token_count
145-
}
138+
if (absolute_path) {
139+
const count =
140+
await workspace_provider.calculate_file_tokens(absolute_path)
141+
files_for_modal.push({
142+
file_path: absolute_path,
143+
relative_path: rel_path,
144+
token_count: count.total
146145
})
147-
)
148-
).filter(
149-
(
150-
f
151-
): f is {
152-
file_path: string
153-
relative_path: string
154-
token_count: number
155-
} => !!f.file_path
156-
)
146+
}
147+
}
157148

158149
files_for_modal.sort((a, b) =>
159150
natural_sort(a.relative_path, b.relative_path)

apps/editor/src/commands/apply-chat-response-command/utils/preview/workspace-listener.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,17 @@ export const setup_workspace_listeners = (params: {
8181

8282
const file_will_delete_listener = vscode.workspace.onWillDeleteFiles(
8383
(event) => {
84-
const promise = Promise.all(
85-
event.files.map(async (uri) => {
86-
if (uri.scheme != 'file') return
84+
const promise = (async () => {
85+
for (const uri of event.files) {
86+
if (uri.scheme != 'file') continue
8787
try {
8888
const content = (await vscode.workspace.fs.readFile(uri)).toString()
8989
deleted_files_content_cache.set(uri.fsPath, content)
9090
} catch (e) {
9191
// Ignore, e.g. for directories
9292
}
93-
})
94-
)
93+
}
94+
})()
9595
event.waitUntil(promise)
9696
}
9797
)

0 commit comments

Comments
 (0)