app: initialize command catalog more efficiently

cuts down load times by like 30-50%
This commit is contained in:
Brendan Allan
2026-03-18 14:46:53 +08:00
parent 37c5295111
commit a43783a6d4
+19 -15
View File
@@ -1,10 +1,10 @@
import { createEffect, createMemo, onCleanup, onMount, type Accessor } from "solid-js"
import { createStore } from "solid-js/store"
import { createSimpleContext } from "@opencode-ai/ui/context" import { createSimpleContext } from "@opencode-ai/ui/context"
import { useDialog } from "@opencode-ai/ui/context/dialog" import { useDialog } from "@opencode-ai/ui/context/dialog"
import { dict as en } from "@/i18n/en" import { type Accessor, createEffect, createMemo, onCleanup, onMount } from "solid-js"
import { createStore } from "solid-js/store"
import { useLanguage } from "@/context/language" import { useLanguage } from "@/context/language"
import { useSettings } from "@/context/settings" import { useSettings } from "@/context/settings"
import { dict as en } from "@/i18n/en"
import { Persist, persisted } from "@/utils/persist" import { Persist, persisted } from "@/utils/persist"
const IS_MAC = typeof navigator === "object" && /(Mac|iPod|iPhone|iPad)/.test(navigator.platform) const IS_MAC = typeof navigator === "object" && /(Mac|iPod|iPhone|iPad)/.test(navigator.platform)
@@ -238,9 +238,10 @@ export const { use: useCommand, provider: CommandProvider } = createSimpleContex
}) })
const warnedDuplicates = new Set<string>() const warnedDuplicates = new Set<string>()
type CommandCatalog = Record<string, CommandCatalogItem>
const [catalog, setCatalog, _, catalogReady] = persisted( const [catalog, setCatalog, _, catalogReady] = persisted(
Persist.global("command.catalog.v1"), Persist.global("command.catalog.v1"),
createStore<Record<string, CommandCatalogItem>>({}), createStore<CommandCatalog>({}),
) )
const bind = (id: string, def: KeybindConfig | undefined) => { const bind = (id: string, def: KeybindConfig | undefined) => {
@@ -259,7 +260,7 @@ export const { use: useCommand, provider: CommandProvider } = createSimpleContex
if (seen.has(opt.id)) { if (seen.has(opt.id)) {
if (import.meta.env.DEV && !warnedDuplicates.has(opt.id)) { if (import.meta.env.DEV && !warnedDuplicates.has(opt.id)) {
warnedDuplicates.add(opt.id) warnedDuplicates.add(opt.id)
console.warn(`[command] duplicate command id \"${opt.id}\" registered; keeping first entry`) console.warn(`[command] duplicate command id "${opt.id}" registered; keeping first entry`)
} }
continue continue
} }
@@ -274,16 +275,19 @@ export const { use: useCommand, provider: CommandProvider } = createSimpleContex
createEffect(() => { createEffect(() => {
if (!catalogReady()) return if (!catalogReady()) return
for (const opt of registered()) { setCatalog(
const id = actionId(opt.id) registered().reduce((acc, opt) => {
setCatalog(id, { const id = actionId(opt.id)
title: opt.title, acc[id] = {
description: opt.description, title: opt.title,
category: opt.category, description: opt.description,
keybind: opt.keybind, category: opt.category,
slash: opt.slash, keybind: opt.keybind,
}) slash: opt.slash,
} }
return acc
}, {} as CommandCatalog),
)
}) })
const catalogOptions = createMemo(() => Object.entries(catalog).map(([id, meta]) => ({ id, ...meta }))) const catalogOptions = createMemo(() => Object.entries(catalog).map(([id, meta]) => ({ id, ...meta })))