Skip to content

Commit

Permalink
feat(add): api lifecycle init
Browse files Browse the repository at this point in the history
  • Loading branch information
gcclll committed Jan 21, 2021
1 parent 179d6f6 commit 071d868
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
40 changes: 40 additions & 0 deletions packages/runtime-core/src/apiLifecycle.ts
@@ -1,10 +1,50 @@
import { DebuggerEvent } from '@vue/reactivity'
import {
ComponentInternalInstance,
currentInstance,
isInSSRComponentSetup,
LifecycleHooks
} from './component'
import { ComponentPublicInstance } from './componentPublicInstance'

export function injectHook(
type: LifecycleHooks,
hook: Function & { __weh?: Function },
target: ComponentInternalInstance | null = currentInstance,
prepend: boolean = false
): Function | undefined {
// TODO
return
}

export const createHook = <T extends Function = () => any>(
lifecycle: LifecycleHooks
) => (hook: T, target: ComponentInternalInstance | null = currentInstance) =>
!isInSSRComponentSetup && injectHook(lifecycle, hook, target)

export const onBeforeMount = createHook(LifecycleHooks.BEFORE_MOUNT)
export const onMounted = createHook(LifecycleHooks.MOUNTED)
export const onBeforeUpdate = createHook(LifecycleHooks.BEFORE_UPDATE)
export const onUpdated = createHook(LifecycleHooks.UPDATED)
export const onUnMount = createHook(LifecycleHooks.UNMOUNTED)

export type DebuggerHook = (e: DebuggerEvent) => void
export const onRenderTriggered = createHook<DebuggerHook>(
LifecycleHooks.RENDER_TRIGGERED
)
export const onRenderTracked = createHook<DebuggerHook>(
LifecycleHooks.RENDER_TRACKED
)

export type ErrorCapturedHook = (
err: unknown,
instance: ComponentPublicInstance | null,
info: string
) => boolean | void

export const onErrorCaptured = (
hook: ErrorCapturedHook,
target: ComponentInternalInstance | null = currentInstance
) => {
injectHook(LifecycleHooks.ERROR_CAPTURED, hook, target)
}
2 changes: 2 additions & 0 deletions packages/runtime-core/src/component.ts
Expand Up @@ -375,6 +375,8 @@ export function validateComponentName(name: string, config: AppConfig) {
}
}

export let isInSSRComponentSetup = false

type CompileFunction = (
template: string | object,
options?: CompilerOptions
Expand Down

0 comments on commit 071d868

Please sign in to comment.