Skip to content

Commit a43783a

Browse files
committed
app: initialize command catalog more efficiently
cuts down load times by like 30-50%
1 parent 37c5295 commit a43783a

1 file changed

Lines changed: 19 additions & 15 deletions

File tree

packages/app/src/context/command.tsx

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { createEffect, createMemo, onCleanup, onMount, type Accessor } from "solid-js"
2-
import { createStore } from "solid-js/store"
31
import { createSimpleContext } from "@opencode-ai/ui/context"
42
import { useDialog } from "@opencode-ai/ui/context/dialog"
5-
import { dict as en } from "@/i18n/en"
3+
import { type Accessor, createEffect, createMemo, onCleanup, onMount } from "solid-js"
4+
import { createStore } from "solid-js/store"
65
import { useLanguage } from "@/context/language"
76
import { useSettings } from "@/context/settings"
7+
import { dict as en } from "@/i18n/en"
88
import { Persist, persisted } from "@/utils/persist"
99

1010
const IS_MAC = typeof navigator === "object" && /(Mac|iPod|iPhone|iPad)/.test(navigator.platform)
@@ -238,9 +238,10 @@ export const { use: useCommand, provider: CommandProvider } = createSimpleContex
238238
})
239239
const warnedDuplicates = new Set<string>()
240240

241+
type CommandCatalog = Record<string, CommandCatalogItem>
241242
const [catalog, setCatalog, _, catalogReady] = persisted(
242243
Persist.global("command.catalog.v1"),
243-
createStore<Record<string, CommandCatalogItem>>({}),
244+
createStore<CommandCatalog>({}),
244245
)
245246

246247
const bind = (id: string, def: KeybindConfig | undefined) => {
@@ -259,7 +260,7 @@ export const { use: useCommand, provider: CommandProvider } = createSimpleContex
259260
if (seen.has(opt.id)) {
260261
if (import.meta.env.DEV && !warnedDuplicates.has(opt.id)) {
261262
warnedDuplicates.add(opt.id)
262-
console.warn(`[command] duplicate command id \"${opt.id}\" registered; keeping first entry`)
263+
console.warn(`[command] duplicate command id "${opt.id}" registered; keeping first entry`)
263264
}
264265
continue
265266
}
@@ -274,16 +275,19 @@ export const { use: useCommand, provider: CommandProvider } = createSimpleContex
274275
createEffect(() => {
275276
if (!catalogReady()) return
276277

277-
for (const opt of registered()) {
278-
const id = actionId(opt.id)
279-
setCatalog(id, {
280-
title: opt.title,
281-
description: opt.description,
282-
category: opt.category,
283-
keybind: opt.keybind,
284-
slash: opt.slash,
285-
})
286-
}
278+
setCatalog(
279+
registered().reduce((acc, opt) => {
280+
const id = actionId(opt.id)
281+
acc[id] = {
282+
title: opt.title,
283+
description: opt.description,
284+
category: opt.category,
285+
keybind: opt.keybind,
286+
slash: opt.slash,
287+
}
288+
return acc
289+
}, {} as CommandCatalog),
290+
)
287291
})
288292

289293
const catalogOptions = createMemo(() => Object.entries(catalog).map(([id, meta]) => ({ id, ...meta })))

0 commit comments

Comments
 (0)