/
Alex_Ural
/
opencode
Обзор
Документация
Войти
/
Alex_Ural
/
opencode
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
dev
packages/ui/src/context/helper.tsx
38 строк
1 KB
Luke Parker
feat(app): improve desktop multi-server support (#30678)
05 июн 2026, 09:30
Не верифицирован
05 июн 2026, 09:30
7f33576
Код
Авторство
О чём код?
import { createContext, createMemo, Show, useContext, type ParentProps, type Accessor } from "solid-js" export function createSimpleContext<T, Props extends Record<string, any>>( input: { name: string init: ((input: Props) => T) | (() => T) } & (T extends { ready: unknown } ? { gate: boolean } : { gate?: boolean }), ) { const ctx = createContext<T>() return { provider: (props: ParentProps<Props>) => { const init = input.init(props) const gate = input.gate ?? true if (!gate) { return <ctx.Provider value={init}>{props.children}</ctx.Provider> } // Access init.ready inside the memo to make it reactive for getter properties const isReady = createMemo(() => { // @ts-expect-error const ready = init.ready as Accessor<boolean> | boolean | undefined return ready === undefined || (typeof ready === "function" ? ready() : ready) }) return ( <Show when={isReady()}> <ctx.Provider value={init}>{props.children}</ctx.Provider> </Show> ) }, use() { const value = useContext(ctx) if (!value) throw new Error(`${input.name} context must be used within a context provider`) return value }, } }