C6: Runtime kernel owns data, status, error — adapters subscribe
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>
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
/**
|
||||
* Svelte Stage 2 — Generates stores from Stage 1 output.
|
||||
*
|
||||
* Subscribes to the kernel for state. Returns readable stores.
|
||||
*/
|
||||
|
||||
function pascalCase(str) {
|
||||
@@ -15,12 +17,11 @@ export function generateSvelteAdapter(schema) {
|
||||
const lines = [
|
||||
'// AUTO-GENERATED by mizan — do not edit',
|
||||
'',
|
||||
"import { writable, derived, type Readable } from 'svelte/store'",
|
||||
"import { registerContext } from '@mizan/runtime'",
|
||||
"import { readable, type Readable } from 'svelte/store'",
|
||||
"import { registerContext, type ContextState } from '@mizan/runtime'",
|
||||
'',
|
||||
]
|
||||
|
||||
// Stage 1 imports
|
||||
const stage1Imports = []
|
||||
for (const [ctxName] of Object.entries(contextGroups)) {
|
||||
const p = pascalCase(ctxName)
|
||||
@@ -34,14 +35,11 @@ export function generateSvelteAdapter(schema) {
|
||||
lines.push('')
|
||||
}
|
||||
|
||||
// ── Context stores ──────────────────────────────────────────────────
|
||||
|
||||
for (const [ctxName, ctxMeta] of Object.entries(contextGroups)) {
|
||||
const p = pascalCase(ctxName)
|
||||
const ctxFunctions = functions.filter(fn => fn.isContext === ctxName)
|
||||
const paramEntries = Object.entries(ctxMeta.params || {})
|
||||
|
||||
lines.push(`// ${p} context`)
|
||||
const paramsArg = paramEntries.length > 0 ? 'params' : '{} as any'
|
||||
|
||||
if (paramEntries.length > 0) {
|
||||
lines.push(`export function create${p}Context(params: ${p}ContextParams) {`)
|
||||
@@ -49,49 +47,32 @@ export function generateSvelteAdapter(schema) {
|
||||
lines.push(`export function create${p}Context() {`)
|
||||
}
|
||||
|
||||
lines.push(` const data = writable<${p}ContextData | null>(null)`)
|
||||
lines.push(` const loading = writable(true)`)
|
||||
// Use readable store backed by kernel subscription
|
||||
lines.push(` const store = readable<ContextState<${p}ContextData>>(`)
|
||||
lines.push(` { data: null, status: 'idle', error: null },`)
|
||||
lines.push(` (set) => {`)
|
||||
lines.push(` const handle = registerContext('${ctxName}', ${paramsArg}, () => fetch${p}Context(${paramsArg}))`)
|
||||
lines.push(` const unsub = handle.subscribe(() => set(handle.getState()))`)
|
||||
lines.push(` handle.refetch()`)
|
||||
lines.push(` return () => { unsub(); handle.unregister() }`)
|
||||
lines.push(` },`)
|
||||
lines.push(` )`)
|
||||
lines.push('')
|
||||
lines.push(` const refetch = async () => {`)
|
||||
lines.push(` loading.set(true)`)
|
||||
if (paramEntries.length > 0) {
|
||||
lines.push(` const result = await fetch${p}Context(params)`)
|
||||
} else {
|
||||
lines.push(` const result = await fetch${p}Context({} as any)`)
|
||||
}
|
||||
lines.push(` data.set(result)`)
|
||||
lines.push(` loading.set(false)`)
|
||||
lines.push(` }`)
|
||||
lines.push('')
|
||||
lines.push(` refetch()`)
|
||||
if (paramEntries.length > 0) {
|
||||
lines.push(` const unregister = registerContext('${ctxName}', params, refetch)`)
|
||||
} else {
|
||||
lines.push(` const unregister = registerContext('${ctxName}', {}, refetch)`)
|
||||
}
|
||||
lines.push('')
|
||||
|
||||
// Derived stores for each function
|
||||
lines.push(` return {`)
|
||||
lines.push(` data,`)
|
||||
lines.push(` loading,`)
|
||||
for (const fn of ctxFunctions) {
|
||||
const camel = fn.camelName
|
||||
lines.push(` ${camel}: derived(data, $d => $d?.${fn.name} ?? null) as Readable<${fn.outputType} | null>,`)
|
||||
}
|
||||
lines.push(` destroy: unregister,`)
|
||||
lines.push(` }`)
|
||||
lines.push(` return store`)
|
||||
lines.push('}')
|
||||
lines.push('')
|
||||
}
|
||||
|
||||
// ── Mutation + function exports ─────────────────────────────────────
|
||||
|
||||
// Re-export mutations as-is from Stage 1
|
||||
for (const fn of [...mutations, ...plainFns]) {
|
||||
const p = pascalCase(fn.camelName)
|
||||
lines.push(`export { call${p} } from '../${fn.affects ? 'mutations' : 'functions'}/${fn.camelName}'`)
|
||||
lines.push(`export { call${p} } from '../index'`)
|
||||
}
|
||||
lines.push('')
|
||||
|
||||
lines.push("export type { ContextState } from '@mizan/runtime'")
|
||||
lines.push("export { configure, initSession, MizanError } from '@mizan/runtime'")
|
||||
lines.push('')
|
||||
|
||||
return lines.join('\n')
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user