| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496 |
- import {
- ComponentInternalInstance,
- Data,
- Component,
- SetupContext,
- RenderFunction,
- SFCInternalOptions
- } from './component'
- import {
- isFunction,
- extend,
- isString,
- isObject,
- isArray,
- EMPTY_OBJ,
- NOOP
- } from '@vue/shared'
- import { computed } from './apiReactivity'
- import { watch, WatchOptions, WatchCallback } from './apiWatch'
- import { provide, inject } from './apiInject'
- import {
- onBeforeMount,
- onMounted,
- onBeforeUpdate,
- onUpdated,
- onErrorCaptured,
- onRenderTracked,
- onBeforeUnmount,
- onUnmounted,
- onActivated,
- onDeactivated,
- onRenderTriggered,
- DebuggerHook,
- ErrorCapturedHook
- } from './apiLifecycle'
- import {
- reactive,
- ComputedGetter,
- WritableComputedOptions
- } from '@vue/reactivity'
- import { ComponentObjectPropsOptions, ExtractPropTypes } from './componentProps'
- import { Directive } from './directives'
- import { ComponentPublicInstance } from './componentProxy'
- import { warn } from './warning'
- export interface ComponentOptionsBase<
- Props,
- RawBindings,
- D,
- C extends ComputedOptions,
- M extends MethodOptions
- > extends LegacyOptions<Props, RawBindings, D, C, M>, SFCInternalOptions {
- setup?: (
- this: void,
- props: Props,
- ctx: SetupContext
- ) => RawBindings | RenderFunction | void
- name?: string
- template?: string | object // can be a direct DOM node
- // Note: we are intentionally using the signature-less `Function` type here
- // since any type with signature will cause the whole inference to fail when
- // the return expression contains reference to `this`.
- // Luckily `render()` doesn't need any arguments nor does it care about return
- // type.
- render?: Function
- // SSR only. This is produced by compiler-ssr and attached in compiler-sfc
- // not user facing, so the typing is lax and for test only.
- ssrRender?: (
- ctx: any,
- push: (item: any) => void,
- parentInstance: ComponentInternalInstance
- ) => void
- components?: Record<
- string,
- Component | { new (): ComponentPublicInstance<any, any, any, any, any> }
- >
- directives?: Record<string, Directive>
- inheritAttrs?: boolean
- // type-only differentiator to separate OptionWithoutProps from a constructor
- // type returned by defineComponent() or FunctionalComponent
- call?: never
- // type-only differentiators for built-in Vnode types
- __isFragment?: never
- __isPortal?: never
- __isSuspense?: never
- }
- export type ComponentOptionsWithoutProps<
- Props = {},
- RawBindings = {},
- D = {},
- C extends ComputedOptions = {},
- M extends MethodOptions = {}
- > = ComponentOptionsBase<Readonly<Props>, RawBindings, D, C, M> & {
- props?: undefined
- } & ThisType<ComponentPublicInstance<{}, RawBindings, D, C, M, Readonly<Props>>>
- export type ComponentOptionsWithArrayProps<
- PropNames extends string = string,
- RawBindings = {},
- D = {},
- C extends ComputedOptions = {},
- M extends MethodOptions = {},
- Props = Readonly<{ [key in PropNames]?: any }>
- > = ComponentOptionsBase<Props, RawBindings, D, C, M> & {
- props: PropNames[]
- } & ThisType<ComponentPublicInstance<Props, RawBindings, D, C, M>>
- export type ComponentOptionsWithObjectProps<
- PropsOptions = ComponentObjectPropsOptions,
- RawBindings = {},
- D = {},
- C extends ComputedOptions = {},
- M extends MethodOptions = {},
- Props = Readonly<ExtractPropTypes<PropsOptions>>
- > = ComponentOptionsBase<Props, RawBindings, D, C, M> & {
- props: PropsOptions
- } & ThisType<ComponentPublicInstance<Props, RawBindings, D, C, M>>
- export type ComponentOptions =
- | ComponentOptionsWithoutProps
- | ComponentOptionsWithObjectProps
- | ComponentOptionsWithArrayProps
- // TODO legacy component definition also supports constructors with .options
- type LegacyComponent = ComponentOptions
- export type ComputedOptions = Record<
- string,
- ComputedGetter<any> | WritableComputedOptions<any>
- >
- export interface MethodOptions {
- [key: string]: Function
- }
- export type ExtractComputedReturns<T extends any> = {
- [key in keyof T]: T[key] extends { get: Function }
- ? ReturnType<T[key]['get']>
- : ReturnType<T[key]>
- }
- type WatchOptionItem =
- | string
- | WatchCallback
- | { handler: WatchCallback } & WatchOptions
- type ComponentWatchOptionItem = WatchOptionItem | WatchOptionItem[]
- type ComponentWatchOptions = Record<string, ComponentWatchOptionItem>
- type ComponentInjectOptions =
- | string[]
- | Record<
- string | symbol,
- string | symbol | { from: string | symbol; default?: unknown }
- >
- export interface LegacyOptions<
- Props,
- RawBindings,
- D,
- C extends ComputedOptions,
- M extends MethodOptions
- > {
- el?: any
- // state
- // Limitation: we cannot expose RawBindings on the `this` context for data
- // since that leads to some sort of circular inference and breaks ThisType
- // for the entire component.
- data?: D | ((this: ComponentPublicInstance<Props>) => D)
- computed?: C
- methods?: M
- watch?: ComponentWatchOptions
- provide?: Data | Function
- inject?: ComponentInjectOptions
- // composition
- mixins?: LegacyComponent[]
- extends?: LegacyComponent
- // lifecycle
- beforeCreate?(): void
- created?(): void
- beforeMount?(): void
- mounted?(): void
- beforeUpdate?(): void
- updated?(): void
- activated?(): void
- deactivated?(): void
- beforeUnmount?(): void
- unmounted?(): void
- renderTracked?: DebuggerHook
- renderTriggered?: DebuggerHook
- errorCaptured?: ErrorCapturedHook
- }
- const enum OptionTypes {
- PROPS = 'Props',
- DATA = 'Data',
- COMPUTED = 'Computed',
- METHODS = 'Methods',
- INJECT = 'Inject'
- }
- function createDuplicateChecker() {
- const cache = Object.create(null)
- return (type: OptionTypes, key: string) => {
- if (cache[key]) {
- warn(`${type} property "${key}" is already defined in ${cache[key]}.`)
- } else {
- cache[key] = type
- }
- }
- }
- export function applyOptions(
- instance: ComponentInternalInstance,
- options: ComponentOptions,
- asMixin: boolean = false
- ) {
- const ctx = instance.proxy!
- const {
- // composition
- mixins,
- extends: extendsOptions,
- // state
- props: propsOptions,
- data: dataOptions,
- computed: computedOptions,
- methods,
- watch: watchOptions,
- provide: provideOptions,
- inject: injectOptions,
- // assets
- components,
- directives,
- // lifecycle
- beforeMount,
- mounted,
- beforeUpdate,
- updated,
- activated,
- deactivated,
- beforeUnmount,
- unmounted,
- renderTracked,
- renderTriggered,
- errorCaptured
- } = options
- const renderContext =
- instance.renderContext === EMPTY_OBJ
- ? (instance.renderContext = {})
- : instance.renderContext
- const globalMixins = instance.appContext.mixins
- // call it only during dev
- const checkDuplicateProperties = __DEV__ ? createDuplicateChecker() : null
- // applyOptions is called non-as-mixin once per instance
- if (!asMixin) {
- callSyncHook('beforeCreate', options, ctx, globalMixins)
- // global mixins are applied first
- applyMixins(instance, globalMixins)
- }
- // extending a base component...
- if (extendsOptions) {
- applyOptions(instance, extendsOptions, true)
- }
- // local mixins
- if (mixins) {
- applyMixins(instance, mixins)
- }
- if (__DEV__ && propsOptions) {
- for (const key in propsOptions) {
- checkDuplicateProperties!(OptionTypes.PROPS, key)
- }
- }
- // state options
- if (dataOptions) {
- const data = isFunction(dataOptions) ? dataOptions.call(ctx) : dataOptions
- if (!isObject(data)) {
- __DEV__ && warn(`data() should return an object.`)
- } else if (instance.data === EMPTY_OBJ) {
- if (__DEV__) {
- for (const key in data) {
- checkDuplicateProperties!(OptionTypes.DATA, key)
- }
- }
- instance.data = reactive(data)
- } else {
- // existing data: this is a mixin or extends.
- extend(instance.data, data)
- }
- }
- if (computedOptions) {
- for (const key in computedOptions) {
- const opt = (computedOptions as ComputedOptions)[key]
- __DEV__ && checkDuplicateProperties!(OptionTypes.COMPUTED, key)
- if (isFunction(opt)) {
- renderContext[key] = computed(opt.bind(ctx))
- } else {
- const { get, set } = opt
- if (isFunction(get)) {
- renderContext[key] = computed({
- get: get.bind(ctx),
- set: isFunction(set)
- ? set.bind(ctx)
- : __DEV__
- ? () => {
- warn(
- `Computed property "${key}" was assigned to but it has no setter.`
- )
- }
- : NOOP
- })
- } else if (__DEV__) {
- warn(`Computed property "${key}" has no getter.`)
- }
- }
- }
- }
- if (methods) {
- for (const key in methods) {
- const methodHandler = (methods as MethodOptions)[key]
- if (isFunction(methodHandler)) {
- __DEV__ && checkDuplicateProperties!(OptionTypes.METHODS, key)
- renderContext[key] = methodHandler.bind(ctx)
- } else if (__DEV__) {
- warn(
- `Method "${key}" has type "${typeof methodHandler}" in the component definition. ` +
- `Did you reference the function correctly?`
- )
- }
- }
- }
- if (watchOptions) {
- for (const key in watchOptions) {
- createWatcher(watchOptions[key], renderContext, ctx, key)
- }
- }
- if (provideOptions) {
- const provides = isFunction(provideOptions)
- ? provideOptions.call(ctx)
- : provideOptions
- for (const key in provides) {
- provide(key, provides[key])
- }
- }
- if (injectOptions) {
- if (isArray(injectOptions)) {
- for (let i = 0; i < injectOptions.length; i++) {
- const key = injectOptions[i]
- __DEV__ && checkDuplicateProperties!(OptionTypes.INJECT, key)
- renderContext[key] = inject(key)
- }
- } else {
- for (const key in injectOptions) {
- __DEV__ && checkDuplicateProperties!(OptionTypes.INJECT, key)
- const opt = injectOptions[key]
- if (isObject(opt)) {
- renderContext[key] = inject(opt.from, opt.default)
- } else {
- renderContext[key] = inject(opt)
- }
- }
- }
- }
- // asset options
- if (components) {
- extend(instance.components, components)
- }
- if (directives) {
- extend(instance.directives, directives)
- }
- // lifecycle options
- if (!asMixin) {
- callSyncHook('created', options, ctx, globalMixins)
- }
- if (beforeMount) {
- onBeforeMount(beforeMount.bind(ctx))
- }
- if (mounted) {
- onMounted(mounted.bind(ctx))
- }
- if (beforeUpdate) {
- onBeforeUpdate(beforeUpdate.bind(ctx))
- }
- if (updated) {
- onUpdated(updated.bind(ctx))
- }
- if (activated) {
- onActivated(activated.bind(ctx))
- }
- if (deactivated) {
- onDeactivated(deactivated.bind(ctx))
- }
- if (errorCaptured) {
- onErrorCaptured(errorCaptured.bind(ctx))
- }
- if (renderTracked) {
- onRenderTracked(renderTracked.bind(ctx))
- }
- if (renderTriggered) {
- onRenderTriggered(renderTriggered.bind(ctx))
- }
- if (beforeUnmount) {
- onBeforeUnmount(beforeUnmount.bind(ctx))
- }
- if (unmounted) {
- onUnmounted(unmounted.bind(ctx))
- }
- }
- function callSyncHook(
- name: 'beforeCreate' | 'created',
- options: ComponentOptions,
- ctx: ComponentPublicInstance,
- globalMixins: ComponentOptions[]
- ) {
- callHookFromMixins(name, globalMixins, ctx)
- const baseHook = options.extends && options.extends[name]
- if (baseHook) {
- baseHook.call(ctx)
- }
- const mixins = options.mixins
- if (mixins) {
- callHookFromMixins(name, mixins, ctx)
- }
- const selfHook = options[name]
- if (selfHook) {
- selfHook.call(ctx)
- }
- }
- function callHookFromMixins(
- name: 'beforeCreate' | 'created',
- mixins: ComponentOptions[],
- ctx: ComponentPublicInstance
- ) {
- for (let i = 0; i < mixins.length; i++) {
- const fn = mixins[i][name]
- if (fn) {
- fn.call(ctx)
- }
- }
- }
- function applyMixins(
- instance: ComponentInternalInstance,
- mixins: ComponentOptions[]
- ) {
- for (let i = 0; i < mixins.length; i++) {
- applyOptions(instance, mixins[i], true)
- }
- }
- function createWatcher(
- raw: ComponentWatchOptionItem,
- renderContext: Data,
- ctx: ComponentPublicInstance,
- key: string
- ) {
- const getter = () => (ctx as Data)[key]
- if (isString(raw)) {
- const handler = renderContext[raw]
- if (isFunction(handler)) {
- watch(getter, handler as WatchCallback)
- } else if (__DEV__) {
- warn(`Invalid watch handler specified by key "${raw}"`, handler)
- }
- } else if (isFunction(raw)) {
- watch(getter, raw.bind(ctx))
- } else if (isObject(raw)) {
- if (isArray(raw)) {
- raw.forEach(r => createWatcher(r, renderContext, ctx, key))
- } else {
- watch(getter, raw.handler.bind(ctx), raw)
- }
- } else if (__DEV__) {
- warn(`Invalid watch option: "${key}"`)
- }
- }
|