(vm: ComponentInstance, props: Record<any, any> = {})
| 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 | } |
no test coverage detected
searching dependent graphs…