/
runtimeContext.ts
320 lines (265 loc) · 7.12 KB
/
runtimeContext.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import type { VueConstructor, VNode } from 'vue'
import { bindCurrentScopeToVM, EffectScope } from './apis/effectScope'
import { ComponentInstance, Data } from './component'
import {
assert,
hasOwn,
warn,
proxy,
UnionToIntersection,
isFunction,
} from './utils'
import Vue$1 from 'vue'
let vueDependency: VueConstructor | undefined = undefined
try {
const requiredVue = require('vue')
if (requiredVue && isVue(requiredVue)) {
vueDependency = requiredVue
} else if (
requiredVue &&
'default' in requiredVue &&
isVue(requiredVue.default)
) {
vueDependency = requiredVue.default
}
} catch {
// not available
}
let vueConstructor: VueConstructor | null = null
let currentInstance: ComponentInternalInstance | null = null
let currentInstanceTracking = true
const PluginInstalledFlag = '__composition_api_installed__'
function isVue(obj: any): obj is VueConstructor {
return obj && isFunction(obj) && obj.name === 'Vue'
}
export function isPluginInstalled() {
return !!vueConstructor
}
export function isVueRegistered(Vue: VueConstructor) {
// resolve issue: https://github.com/vuejs/composition-api/issues/876#issue-1087619365
return vueConstructor && hasOwn(Vue, PluginInstalledFlag)
}
export function getVueConstructor(): VueConstructor {
if (__DEV__) {
assert(
vueConstructor,
`must call Vue.use(VueCompositionAPI) before using any function.`
)
}
return vueConstructor!
}
// returns registered vue or `vue` dependency
export function getRegisteredVueOrDefault(): VueConstructor {
let constructor = vueConstructor || vueDependency
if (__DEV__) {
assert(constructor, `No vue dependency found.`)
}
return constructor!
}
export function setVueConstructor(Vue: VueConstructor) {
// @ts-ignore
if (__DEV__ && vueConstructor && Vue.__proto__ !== vueConstructor.__proto__) {
warn('[vue-composition-api] another instance of Vue installed')
}
vueConstructor = Vue
Object.defineProperty(Vue, PluginInstalledFlag, {
configurable: true,
writable: true,
value: true,
})
}
/**
* For `effectScope` to create instance without populate the current instance
* @internal
**/
export function withCurrentInstanceTrackingDisabled(fn: () => void) {
const prev = currentInstanceTracking
currentInstanceTracking = false
try {
fn()
} finally {
currentInstanceTracking = prev
}
}
export function setCurrentVue2Instance(vm: ComponentInstance | null) {
if (!currentInstanceTracking) return
setCurrentInstance(vm ? toVue3ComponentInstance(vm) : vm)
}
export function setCurrentInstance(instance: ComponentInternalInstance | null) {
if (!currentInstanceTracking) return
const prev = currentInstance
prev?.scope.off()
currentInstance = instance
currentInstance?.scope.on()
}
export type Slot = (...args: any[]) => VNode[]
export type InternalSlots = {
[name: string]: Slot | undefined
}
export type ObjectEmitsOptions = Record<
string,
((...args: any[]) => any) | null
>
export type EmitsOptions = ObjectEmitsOptions | string[]
export type EmitFn<
Options = ObjectEmitsOptions,
Event extends keyof Options = keyof Options,
ReturnType extends void | Vue$1 = void
> = Options extends Array<infer V>
? (event: V, ...args: any[]) => ReturnType
: {} extends Options // if the emit is empty object (usually the default value for emit) should be converted to function
? (event: string, ...args: any[]) => ReturnType
: UnionToIntersection<
{
[key in Event]: Options[key] extends (...args: infer Args) => any
? (event: key, ...args: Args) => ReturnType
: (event: key, ...args: any[]) => ReturnType
}[Event]
>
export type ComponentRenderEmitFn<
Options = ObjectEmitsOptions,
Event extends keyof Options = keyof Options,
T extends Vue$1 | void = void
> = EmitFn<Options, Event, T>
export type Slots = Readonly<InternalSlots>
export interface SetupContext<E = EmitsOptions> {
attrs: Data
slots: Slots
emit: EmitFn<E>
/**
* @deprecated not available in Vue 2
*/
expose: (exposed?: Record<string, any>) => void
/**
* @deprecated not available in Vue 3
*/
readonly parent: ComponentInstance | null
/**
* @deprecated not available in Vue 3
*/
readonly root: ComponentInstance
/**
* @deprecated not available in Vue 3
*/
readonly listeners: { [key in string]?: Function }
/**
* @deprecated not available in Vue 3
*/
readonly refs: { [key: string]: Vue | Element | Vue[] | Element[] }
}
export interface ComponentPublicInstance {}
/**
* We expose a subset of properties on the internal instance as they are
* useful for advanced external libraries and tools.
*/
export declare interface ComponentInternalInstance {
uid: number
type: Record<string, unknown> // ConcreteComponent
parent: ComponentInternalInstance | null
root: ComponentInternalInstance
//appContext: AppContext
/**
* Vnode representing this component in its parent's vdom tree
*/
vnode: VNode
/**
* Root vnode of this component's own vdom tree
*/
// subTree: VNode // does not exist in Vue 2
/**
* The reactive effect for rendering and patching the component. Callable.
*/
update: Function
data: Data
props: Data
attrs: Data
refs: Data
emit: EmitFn
slots: InternalSlots
emitted: Record<string, boolean> | null
proxy: ComponentInstance
isMounted: boolean
isUnmounted: boolean
isDeactivated: boolean
/**
* @internal
*/
scope: EffectScope
/**
* @internal
*/
setupContext: SetupContext | null
}
export function getCurrentInstance() {
return currentInstance
}
const instanceMapCache = new WeakMap<
ComponentInstance,
ComponentInternalInstance
>()
export function toVue3ComponentInstance(
vm: ComponentInstance
): ComponentInternalInstance {
if (instanceMapCache.has(vm)) {
return instanceMapCache.get(vm)!
}
const instance: ComponentInternalInstance = {
proxy: vm,
update: vm.$forceUpdate,
type: vm.$options,
uid: vm._uid,
// $emit is defined on prototype and it expected to be bound
emit: vm.$emit.bind(vm),
parent: null,
root: null!, // to be immediately set
} as unknown as ComponentInternalInstance
bindCurrentScopeToVM(instance)
// map vm.$props =
const instanceProps = [
'data',
'props',
'attrs',
'refs',
'vnode',
'slots',
] as const
instanceProps.forEach((prop) => {
proxy(instance, prop, {
get() {
return (vm as any)[`$${prop}`]
},
})
})
proxy(instance, 'isMounted', {
get() {
// @ts-expect-error private api
return vm._isMounted
},
})
proxy(instance, 'isUnmounted', {
get() {
// @ts-expect-error private api
return vm._isDestroyed
},
})
proxy(instance, 'isDeactivated', {
get() {
// @ts-expect-error private api
return vm._inactive
},
})
proxy(instance, 'emitted', {
get() {
// @ts-expect-error private api
return vm._events
},
})
instanceMapCache.set(vm, instance)
if (vm.$parent) {
instance.parent = toVue3ComponentInstance(vm.$parent)
}
if (vm.$root) {
instance.root = toVue3ComponentInstance(vm.$root)
}
return instance
}