The kernel is no longer a blind refetch pipe. Each context entry has:
{ data, status: idle|loading|success|error, error }
registerContext() returns { getState, subscribe, refetch, unregister }.
Adapters subscribe to state changes via callbacks. The kernel does
the fetch and notifies subscribers with the new state.
React adapter uses useSyncExternalStore for tear-free reads.
Vue adapter uses ref + subscribe callback.
Svelte adapter uses readable store backed by kernel subscription.
All three adapters also get:
- Mutation hooks with { mutate, isPending, error } (fixes H5)
- Vue: onServerPrefetch for Nuxt SSR (fixes M9)
- Svelte: readable store auto-cleans up on unsubscribe (fixes H9)
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
230 lines
8.0 KiB
TypeScript
230 lines
8.0 KiB
TypeScript
// AUTO-GENERATED by mizan — do not edit
|
|
|
|
import { ref, computed, onMounted, onUnmounted, onServerPrefetch, type ComputedRef } from 'vue'
|
|
import { registerContext, type ContextState } from '@mizan/runtime'
|
|
|
|
import { fetchGlobalContext, type GlobalContextData, type GlobalContextParams, fetchLocalContext, type LocalContextData, type LocalContextParams, callEcho, callAdd, callWhoami, callHttpOnlyEcho, callStaffOnly, callSuperuserOnly, callVerifiedOnly, callMultiply, callNotImplementedFn, callBuggyFn, callPermissionCheckFn, callWsWhoami, callJwtObtain, callJwtRefresh } from '../index'
|
|
|
|
export function useGlobalContext() {
|
|
const state = ref<ContextState<GlobalContextData>>({ data: null, status: 'idle', error: null })
|
|
let handle: ReturnType<typeof registerContext> | null = null
|
|
|
|
onMounted(() => {
|
|
handle = registerContext('global', {} as any, () => fetchGlobalContext({} as any))
|
|
handle.subscribe(() => { state.value = handle!.getState() })
|
|
handle.refetch()
|
|
})
|
|
|
|
onServerPrefetch(async () => {
|
|
handle = registerContext('global', {} as any, () => fetchGlobalContext({} as any))
|
|
await handle.refetch()
|
|
state.value = handle.getState()
|
|
})
|
|
|
|
onUnmounted(() => { handle?.unregister() })
|
|
|
|
return {
|
|
state,
|
|
currentUser: computed(() => state.value.data?.current_user ?? null) as ComputedRef<currentUserOutput | null>,
|
|
loading: computed(() => state.value.status === 'loading'),
|
|
error: computed(() => state.value.error),
|
|
}
|
|
}
|
|
|
|
export function useLocalContext(params: LocalContextParams) {
|
|
const state = ref<ContextState<LocalContextData>>({ data: null, status: 'idle', error: null })
|
|
let handle: ReturnType<typeof registerContext> | null = null
|
|
|
|
onMounted(() => {
|
|
handle = registerContext('local', params, () => fetchLocalContext(params))
|
|
handle.subscribe(() => { state.value = handle!.getState() })
|
|
handle.refetch()
|
|
})
|
|
|
|
onServerPrefetch(async () => {
|
|
handle = registerContext('local', params, () => fetchLocalContext(params))
|
|
await handle.refetch()
|
|
state.value = handle.getState()
|
|
})
|
|
|
|
onUnmounted(() => { handle?.unregister() })
|
|
|
|
return {
|
|
state,
|
|
greet: computed(() => state.value.data?.greet ?? null) as ComputedRef<greetOutput | null>,
|
|
loading: computed(() => state.value.status === 'loading'),
|
|
error: computed(() => state.value.error),
|
|
}
|
|
}
|
|
|
|
export function useEcho() {
|
|
const isPending = ref(false)
|
|
const error = ref<Error | null>(null)
|
|
async function mutate(args: Parameters<typeof callEcho>[0]) {
|
|
isPending.value = true; error.value = null
|
|
try { return await callEcho(args) }
|
|
catch (e) { error.value = e as Error; throw e }
|
|
finally { isPending.value = false }
|
|
}
|
|
return { mutate, isPending, error }
|
|
}
|
|
|
|
export function useAdd() {
|
|
const isPending = ref(false)
|
|
const error = ref<Error | null>(null)
|
|
async function mutate(args: Parameters<typeof callAdd>[0]) {
|
|
isPending.value = true; error.value = null
|
|
try { return await callAdd(args) }
|
|
catch (e) { error.value = e as Error; throw e }
|
|
finally { isPending.value = false }
|
|
}
|
|
return { mutate, isPending, error }
|
|
}
|
|
|
|
export function useWhoami() {
|
|
const isPending = ref(false)
|
|
const error = ref<Error | null>(null)
|
|
async function mutate() {
|
|
isPending.value = true; error.value = null
|
|
try { return await callWhoami() }
|
|
catch (e) { error.value = e as Error; throw e }
|
|
finally { isPending.value = false }
|
|
}
|
|
return { mutate, isPending, error }
|
|
}
|
|
|
|
export function useHttpOnlyEcho() {
|
|
const isPending = ref(false)
|
|
const error = ref<Error | null>(null)
|
|
async function mutate(args: Parameters<typeof callHttpOnlyEcho>[0]) {
|
|
isPending.value = true; error.value = null
|
|
try { return await callHttpOnlyEcho(args) }
|
|
catch (e) { error.value = e as Error; throw e }
|
|
finally { isPending.value = false }
|
|
}
|
|
return { mutate, isPending, error }
|
|
}
|
|
|
|
export function useStaffOnly() {
|
|
const isPending = ref(false)
|
|
const error = ref<Error | null>(null)
|
|
async function mutate() {
|
|
isPending.value = true; error.value = null
|
|
try { return await callStaffOnly() }
|
|
catch (e) { error.value = e as Error; throw e }
|
|
finally { isPending.value = false }
|
|
}
|
|
return { mutate, isPending, error }
|
|
}
|
|
|
|
export function useSuperuserOnly() {
|
|
const isPending = ref(false)
|
|
const error = ref<Error | null>(null)
|
|
async function mutate() {
|
|
isPending.value = true; error.value = null
|
|
try { return await callSuperuserOnly() }
|
|
catch (e) { error.value = e as Error; throw e }
|
|
finally { isPending.value = false }
|
|
}
|
|
return { mutate, isPending, error }
|
|
}
|
|
|
|
export function useVerifiedOnly() {
|
|
const isPending = ref(false)
|
|
const error = ref<Error | null>(null)
|
|
async function mutate() {
|
|
isPending.value = true; error.value = null
|
|
try { return await callVerifiedOnly() }
|
|
catch (e) { error.value = e as Error; throw e }
|
|
finally { isPending.value = false }
|
|
}
|
|
return { mutate, isPending, error }
|
|
}
|
|
|
|
export function useMultiply() {
|
|
const isPending = ref(false)
|
|
const error = ref<Error | null>(null)
|
|
async function mutate(args: Parameters<typeof callMultiply>[0]) {
|
|
isPending.value = true; error.value = null
|
|
try { return await callMultiply(args) }
|
|
catch (e) { error.value = e as Error; throw e }
|
|
finally { isPending.value = false }
|
|
}
|
|
return { mutate, isPending, error }
|
|
}
|
|
|
|
export function useNotImplementedFn() {
|
|
const isPending = ref(false)
|
|
const error = ref<Error | null>(null)
|
|
async function mutate() {
|
|
isPending.value = true; error.value = null
|
|
try { return await callNotImplementedFn() }
|
|
catch (e) { error.value = e as Error; throw e }
|
|
finally { isPending.value = false }
|
|
}
|
|
return { mutate, isPending, error }
|
|
}
|
|
|
|
export function useBuggyFn() {
|
|
const isPending = ref(false)
|
|
const error = ref<Error | null>(null)
|
|
async function mutate() {
|
|
isPending.value = true; error.value = null
|
|
try { return await callBuggyFn() }
|
|
catch (e) { error.value = e as Error; throw e }
|
|
finally { isPending.value = false }
|
|
}
|
|
return { mutate, isPending, error }
|
|
}
|
|
|
|
export function usePermissionCheckFn() {
|
|
const isPending = ref(false)
|
|
const error = ref<Error | null>(null)
|
|
async function mutate(args: Parameters<typeof callPermissionCheckFn>[0]) {
|
|
isPending.value = true; error.value = null
|
|
try { return await callPermissionCheckFn(args) }
|
|
catch (e) { error.value = e as Error; throw e }
|
|
finally { isPending.value = false }
|
|
}
|
|
return { mutate, isPending, error }
|
|
}
|
|
|
|
export function useWsWhoami() {
|
|
const isPending = ref(false)
|
|
const error = ref<Error | null>(null)
|
|
async function mutate() {
|
|
isPending.value = true; error.value = null
|
|
try { return await callWsWhoami() }
|
|
catch (e) { error.value = e as Error; throw e }
|
|
finally { isPending.value = false }
|
|
}
|
|
return { mutate, isPending, error }
|
|
}
|
|
|
|
export function useJwtObtain() {
|
|
const isPending = ref(false)
|
|
const error = ref<Error | null>(null)
|
|
async function mutate() {
|
|
isPending.value = true; error.value = null
|
|
try { return await callJwtObtain() }
|
|
catch (e) { error.value = e as Error; throw e }
|
|
finally { isPending.value = false }
|
|
}
|
|
return { mutate, isPending, error }
|
|
}
|
|
|
|
export function useJwtRefresh() {
|
|
const isPending = ref(false)
|
|
const error = ref<Error | null>(null)
|
|
async function mutate(args: Parameters<typeof callJwtRefresh>[0]) {
|
|
isPending.value = true; error.value = null
|
|
try { return await callJwtRefresh(args) }
|
|
catch (e) { error.value = e as Error; throw e }
|
|
finally { isPending.value = false }
|
|
}
|
|
return { mutate, isPending, error }
|
|
}
|
|
|
|
export type { ContextState } from '@mizan/runtime'
|
|
export { configure, initSession, MizanError } from '@mizan/runtime'
|