wip: gateway
This commit is contained in:
26
cloud/web/src/util/context.tsx
Normal file
26
cloud/web/src/util/context.tsx
Normal file
@@ -0,0 +1,26 @@
|
||||
import { ParentProps, Show, createContext, useContext } from "solid-js"
|
||||
|
||||
export function createInitializedContext<
|
||||
Name extends string,
|
||||
T extends { ready: boolean },
|
||||
>(name: Name, cb: () => T) {
|
||||
const ctx = createContext<T>()
|
||||
|
||||
return {
|
||||
use: () => {
|
||||
const context = useContext(ctx)
|
||||
if (!context) throw new Error(`No ${name} context`)
|
||||
return context
|
||||
},
|
||||
provider: (props: ParentProps) => {
|
||||
const value = cb()
|
||||
return (
|
||||
<Show when={value.ready}>
|
||||
<ctx.Provider value={value} {...props}>
|
||||
{props.children}
|
||||
</ctx.Provider>
|
||||
</Show>
|
||||
)
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user