-
Notifications
You must be signed in to change notification settings - Fork 344
/
mixin.ts
253 lines (228 loc) · 6.57 KB
/
mixin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import type { VueConstructor } from 'vue'
import { ComponentInstance, SetupFunction, Data } from './component'
import { isRef, isReactive, toRefs, isRaw } from './reactivity'
import {
isPlainObject,
assert,
proxy,
warn,
isFunction,
isObject,
def,
isArray,
} from './utils'
import { ref } from './apis'
import vmStateManager from './utils/vmStateManager'
import {
updateTemplateRef,
activateCurrentInstance,
resolveScopedSlots,
asVmProperty,
updateVmAttrs,
} from './utils/instance'
import {
getVueConstructor,
SetupContext,
toVue3ComponentInstance,
} from './runtimeContext'
import { createObserver } from './reactivity/reactive'
export function mixin(Vue: VueConstructor) {
Vue.mixin({
beforeCreate: functionApiInit,
mounted(this: ComponentInstance) {
updateTemplateRef(this)
},
beforeUpdate() {
updateVmAttrs(this as ComponentInstance)
},
updated(this: ComponentInstance) {
updateTemplateRef(this)
if (this.$vnode?.context) {
updateTemplateRef(this.$vnode.context)
}
},
})
/**
* Vuex init hook, injected into each instances init hooks list.
*/
function functionApiInit(this: ComponentInstance) {
const vm = this
const $options = vm.$options
const { setup, render } = $options
if (render) {
// keep currentInstance accessible for createElement
$options.render = function (...args: any): any {
return activateCurrentInstance(toVue3ComponentInstance(vm), () =>
render.apply(this, args)
)
}
}
if (!setup) {
return
}
if (!isFunction(setup)) {
if (__DEV__) {
warn(
'The "setup" option should be a function that returns a object in component definitions.',
vm
)
}
return
}
const { data } = $options
// wrapper the data option, so we can invoke setup before data get resolved
$options.data = function wrappedData() {
initSetup(vm, vm.$props)
return isFunction(data)
? (
data as (this: ComponentInstance, x: ComponentInstance) => object
).call(vm, vm)
: data || {}
}
}
function initSetup(vm: ComponentInstance, props: Record<any, any> = {}) {
const setup = vm.$options.setup!
const ctx = createSetupContext(vm)
const instance = toVue3ComponentInstance(vm)
instance.setupContext = ctx
// fake reactive for `toRefs(props)`
def(props, '__ob__', createObserver())
// resolve scopedSlots and slots to functions
resolveScopedSlots(vm, ctx.slots)
let binding: ReturnType<SetupFunction<Data, Data>> | undefined | null
activateCurrentInstance(instance, () => {
// make props to be fake reactive, this is for `toRefs(props)`
binding = setup(props, ctx)
})
if (!binding) return
if (isFunction(binding)) {
// keep typescript happy with the binding type.
const bindingFunc = binding
// keep currentInstance accessible for createElement
vm.$options.render = () => {
resolveScopedSlots(vm, ctx.slots)
return activateCurrentInstance(instance, () => bindingFunc())
}
return
} else if (isPlainObject(binding)) {
if (isReactive(binding)) {
binding = toRefs(binding) as Data
}
vmStateManager.set(vm, 'rawBindings', binding)
const bindingObj = binding
Object.keys(bindingObj).forEach((name) => {
let bindingValue: any = bindingObj[name]
if (!isRef(bindingValue)) {
if (!isReactive(bindingValue)) {
if (isFunction(bindingValue)) {
const copy = bindingValue
bindingValue = bindingValue.bind(vm)
Object.keys(copy).forEach(function (ele) {
bindingValue[ele] = copy[ele]
})
} else if (!isObject(bindingValue)) {
bindingValue = ref(bindingValue)
} else if (hasReactiveArrayChild(bindingValue)) {
// creates a custom reactive properties without make the object explicitly reactive
// NOTE we should try to avoid this, better implementation needed
customReactive(bindingValue)
}
} else if (isArray(bindingValue)) {
bindingValue = ref(bindingValue)
}
}
asVmProperty(vm, name, bindingValue)
})
return
}
if (__DEV__) {
assert(
false,
`"setup" must return a "Object" or a "Function", got "${Object.prototype.toString
.call(binding)
.slice(8, -1)}"`
)
}
}
function customReactive(target: object, seen = new Set()) {
if (seen.has(target)) return
if (
!isPlainObject(target) ||
isRef(target) ||
isReactive(target) ||
isRaw(target)
)
return
const Vue = getVueConstructor()
// @ts-expect-error https://github.com/vuejs/vue/pull/12132
const defineReactive = Vue.util.defineReactive
Object.keys(target).forEach((k) => {
const val = target[k]
defineReactive(target, k, val)
if (val) {
seen.add(val)
customReactive(val, seen)
}
return
})
}
function hasReactiveArrayChild(target: object, visited = new Map()): boolean {
if (visited.has(target)) {
return visited.get(target)
}
visited.set(target, false)
if (isArray(target) && isReactive(target)) {
visited.set(target, true)
return true
}
if (!isPlainObject(target) || isRaw(target) || isRef(target)) {
return false
}
return Object.keys(target).some((x) =>
hasReactiveArrayChild(target[x], visited)
)
}
function createSetupContext(
vm: ComponentInstance & { [x: string]: any }
): SetupContext {
const ctx = { slots: {} } as SetupContext
const propsPlain = [
'root',
'parent',
'refs',
'listeners',
'isServer',
'ssrContext',
]
const methodReturnVoid = ['emit']
propsPlain.forEach((key) => {
let srcKey = `$${key}`
proxy(ctx, key, {
get: () => vm[srcKey],
set() {
__DEV__ &&
warn(
`Cannot assign to '${key}' because it is a read-only property`,
vm
)
},
})
})
updateVmAttrs(vm, ctx)
methodReturnVoid.forEach((key) => {
const srcKey = `$${key}`
proxy(ctx, key, {
get() {
return (...args: any[]) => {
const fn: Function = vm[srcKey]
fn.apply(vm, args)
}
},
})
})
if (process.env.NODE_ENV === 'test') {
;(ctx as any)._vm = vm
}
return ctx
}
}