Skip to content

Commit c9bbea4

Browse files
committed
chore: cleanup
1 parent 65e1186 commit c9bbea4

3 files changed

Lines changed: 51 additions & 28 deletions

File tree

packages/opencode/src/config/config.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,20 +1104,23 @@ export namespace Config {
11041104
mergeDeep(await loadFile(path.join(Global.Path.config, "opencode.jsonc"))),
11051105
)
11061106

1107-
await import(path.join(Global.Path.config, "config"), {
1108-
with: {
1109-
type: "toml",
1110-
},
1111-
})
1112-
.then(async (mod) => {
1113-
const { provider, model, ...rest } = mod.default
1114-
if (provider && model) result.model = `${provider}/${model}`
1115-
result["$schema"] = "https://opencode.ai/config.json"
1116-
result = mergeDeep(result, rest)
1117-
await Bun.write(path.join(Global.Path.config, "config.json"), JSON.stringify(result, null, 2))
1118-
await fs.unlink(path.join(Global.Path.config, "config"))
1107+
const legacy = path.join(Global.Path.config, "config")
1108+
if (existsSync(legacy)) {
1109+
await import(pathToFileURL(legacy).href, {
1110+
with: {
1111+
type: "toml",
1112+
},
11191113
})
1120-
.catch(() => {})
1114+
.then(async (mod) => {
1115+
const { provider, model, ...rest } = mod.default
1116+
if (provider && model) result.model = `${provider}/${model}`
1117+
result["$schema"] = "https://opencode.ai/config.json"
1118+
result = mergeDeep(result, rest)
1119+
await Bun.write(path.join(Global.Path.config, "config.json"), JSON.stringify(result, null, 2))
1120+
await fs.unlink(legacy)
1121+
})
1122+
.catch(() => {})
1123+
}
11211124

11221125
return result
11231126
})

packages/opencode/src/project/instance.ts

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ const cache = new Map<string, Promise<Context>>()
1717

1818
const DISPOSE_TIMEOUT_MS = 10_000
1919

20+
const disposal = {
21+
all: undefined as Promise<void> | undefined,
22+
}
23+
2024
export const Instance = {
2125
async provide<R>(input: { directory: string; init?: () => Promise<any>; fn: () => R }): Promise<R> {
2226
let existing = cache.get(input.directory)
@@ -80,20 +84,34 @@ export const Instance = {
8084
})
8185
},
8286
async disposeAll() {
83-
Log.Default.info("disposing all instances")
84-
for (const [key, value] of cache) {
85-
const ctx = await withTimeout(value, DISPOSE_TIMEOUT_MS).catch((error) => {
86-
Log.Default.warn("instance dispose timed out", { key, error })
87-
return undefined
88-
})
89-
if (!ctx) {
90-
cache.delete(key)
91-
continue
87+
if (disposal.all) return disposal.all
88+
89+
disposal.all = iife(async () => {
90+
Log.Default.info("disposing all instances")
91+
const entries = [...cache.entries()]
92+
for (const [key, value] of entries) {
93+
if (cache.get(key) !== value) continue
94+
95+
const ctx = await withTimeout(value, DISPOSE_TIMEOUT_MS).catch((error) => {
96+
Log.Default.warn("instance dispose timed out", { key, error })
97+
return undefined
98+
})
99+
100+
if (!ctx) {
101+
if (cache.get(key) === value) cache.delete(key)
102+
continue
103+
}
104+
105+
if (cache.get(key) !== value) continue
106+
107+
await context.provide(ctx, async () => {
108+
await Instance.dispose()
109+
})
92110
}
93-
await context.provide(ctx, async () => {
94-
await Instance.dispose()
95-
})
96-
}
97-
cache.clear()
111+
}).finally(() => {
112+
disposal.all = undefined
113+
})
114+
115+
return disposal.all
98116
},
99117
}

packages/opencode/src/project/state.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,11 @@ export namespace State {
6666

6767
tasks.push(task)
6868
}
69+
await Promise.all(tasks)
70+
6971
entries.clear()
7072
recordsByKey.delete(key)
71-
await Promise.all(tasks)
73+
7274
disposalFinished = true
7375
log.info("state disposal completed", { key })
7476
}

0 commit comments

Comments
 (0)