refactor(server): canonicalize service API (#31049)

This commit is contained in:
Dax
2026-06-07 03:27:28 +00:00
committed by GitHub
parent 53ff1b57c9
commit fe0c4f8c74
388 changed files with 7103 additions and 4092 deletions
+59
View File
@@ -0,0 +1,59 @@
import { createSignal, type Setter } from "solid-js"
import { createStore, unwrap } from "solid-js/store"
import { createSimpleContext } from "./helper"
import { useOptionalTuiPlatform } from "../platform"
export const { use: useKV, provider: KVProvider } = createSimpleContext({
name: "KV",
init: () => {
const platform = useOptionalTuiPlatform()
const [ready, setReady] = createSignal(false)
const [store, setStore] = createStore<Record<string, any>>()
// Queue same-process writes so rapid updates persist in order.
let write = Promise.resolve()
;(platform?.state?.read() ?? Promise.resolve({}))
.then((x) => {
setStore(x)
})
.catch((error) => {
console.error("Failed to read KV state", { error })
})
.finally(() => {
setReady(true)
})
const result = {
get ready() {
return ready()
},
get store() {
return store
},
signal<T>(name: string, defaultValue: T) {
if (store[name] === undefined) setStore(name, defaultValue)
return [
function () {
return result.get(name)
},
function setter(next: Setter<T>) {
result.set(name, next)
},
] as const
},
get(key: string, defaultValue?: any) {
return store[key] ?? defaultValue
},
set(key: string, value: any) {
setStore(key, value)
const snapshot = structuredClone(unwrap(store))
write = write
.then(() => platform?.state?.write(snapshot))
.catch((error) => {
console.error("Failed to write KV state", { error })
})
},
}
return result
},
})