MCPcopy
hub / github.com/vuejs/composition-api / initSetup

Function initSetup

src/mixin.ts:87–159  ·  view source on GitHub ↗
(vm: ComponentInstance, props: Record<any, any> = {})

Source from the content-addressed store, hash-verified

85 }
86
87 function initSetup(vm: ComponentInstance, props: Record<any, any> = {}) {
88 const setup = vm.$options.setup!
89 const ctx = createSetupContext(vm)
90 const instance = toVue3ComponentInstance(vm)
91 instance.setupContext = ctx
92
93 // fake reactive for `toRefs(props)`
94 def(props, '__ob__', createObserver())
95
96 // resolve scopedSlots and slots to functions
97 resolveScopedSlots(vm, ctx.slots)
98
99 let binding: ReturnType<SetupFunction<Data, Data>> | undefined | null
100 activateCurrentInstance(instance, () => {
101 // make props to be fake reactive, this is for `toRefs(props)`
102 binding = setup(props, ctx)
103 })
104
105 if (!binding) return
106 if (isFunction(binding)) {
107 // keep typescript happy with the binding type.
108 const bindingFunc = binding
109 // keep currentInstance accessible for createElement
110 vm.$options.render = () => {
111 resolveScopedSlots(vm, ctx.slots)
112 return activateCurrentInstance(instance, () => bindingFunc())
113 }
114 return
115 } else if (isObject(binding)) {
116 if (isReactive(binding)) {
117 binding = toRefs(binding) as Data
118 }
119
120 vmStateManager.set(vm, 'rawBindings', binding)
121 const bindingObj = binding
122
123 Object.keys(bindingObj).forEach((name) => {
124 let bindingValue: any = bindingObj[name]
125
126 if (!isRef(bindingValue)) {
127 if (!isReactive(bindingValue)) {
128 if (isFunction(bindingValue)) {
129 const copy = bindingValue
130 bindingValue = bindingValue.bind(vm)
131 Object.keys(copy).forEach(function (ele) {
132 bindingValue[ele] = copy[ele]
133 })
134 } else if (!isObject(bindingValue)) {
135 bindingValue = ref(bindingValue)
136 } else if (hasReactiveArrayChild(bindingValue)) {
137 // creates a custom reactive properties without make the object explicitly reactive
138 // NOTE we should try to avoid this, better implementation needed
139 customReactive(bindingValue)
140 }
141 } else if (isArray(bindingValue)) {
142 bindingValue = ref(bindingValue)
143 }
144 }

Callers 1

functionApiInitFunction · 0.85

Calls 15

toVue3ComponentInstanceFunction · 0.90
defFunction · 0.90
createObserverFunction · 0.90
resolveScopedSlotsFunction · 0.90
activateCurrentInstanceFunction · 0.90
isFunctionFunction · 0.90
isObjectFunction · 0.90
isReactiveFunction · 0.90
toRefsFunction · 0.90
isRefFunction · 0.90
isArrayFunction · 0.90
asVmPropertyFunction · 0.90

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…