Skip to content

Commit fe4b44a

Browse files
committed
feat: app config & context
1 parent f0e959d commit fe4b44a

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

packages/runtime-vapor/src/apiCreateApp.ts renamed to packages/runtime-vapor/src/apiCreateVaporApp.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,24 @@ export function createVaporApp(
1818
rootProps = null
1919
}
2020

21+
const context = createAppContext()
2122
let instance: ComponentInternalInstance
2223

2324
const app: App = {
2425
version,
26+
27+
get config() {
28+
return context.config
29+
},
30+
31+
set config(v) {
32+
if (__DEV__) {
33+
warn(
34+
`app.config cannot be replaced. Modify individual options instead.`,
35+
)
36+
}
37+
},
38+
2539
mount(rootContainer): any {
2640
if (!instance) {
2741
instance = createComponentInstance(rootComponent, rootProps)
@@ -49,11 +63,41 @@ export function createVaporApp(
4963
return app
5064
}
5165

66+
function createAppContext(): AppContext {
67+
return {
68+
app: null as any,
69+
config: {
70+
errorHandler: undefined,
71+
warnHandler: undefined,
72+
},
73+
}
74+
}
75+
5276
export interface App {
5377
version: string
78+
config: AppConfig
79+
5480
mount(
5581
rootContainer: ParentNode | string,
5682
isHydrate?: boolean,
5783
): ComponentInternalInstance
5884
unmount(): void
5985
}
86+
87+
export interface AppConfig {
88+
errorHandler?: (
89+
err: unknown,
90+
instance: ComponentInternalInstance | null,
91+
info: string,
92+
) => void
93+
warnHandler?: (
94+
msg: string,
95+
instance: ComponentInternalInstance | null,
96+
trace: string,
97+
) => void
98+
}
99+
100+
export interface AppContext {
101+
app: App // for devtools
102+
config: AppConfig
103+
}

packages/runtime-vapor/src/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,12 @@ export {
100100
onErrorCaptured,
101101
// onServerPrefetch,
102102
} from './apiLifecycle'
103-
export { createVaporApp, type App } from './apiCreateApp'
103+
export {
104+
createVaporApp,
105+
type App,
106+
type AppConfig,
107+
type AppContext,
108+
} from './apiCreateVaporApp'
104109
export { createIf } from './apiCreateIf'
105110
export { createFor } from './apiCreateFor'
106111
export { createComponent } from './apiCreateComponent'

0 commit comments

Comments
 (0)