Skip to content

Commit c757e32

Browse files
committed
refactor
1 parent 9468f72 commit c757e32

File tree

3 files changed

+126
-126
lines changed

3 files changed

+126
-126
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,6 @@ The following APIs introduced in Vue 3 are not available in this plugin.
399399
- `defineAsyncComponent`
400400
- `onRenderTracked`
401401
- `onRenderTriggered`
402-
- `customRef`
403402
- `isProxy`
404403
- `isReadonly`
405404
- `isVNode`

src/mixin.ts

Lines changed: 7 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -6,139 +6,21 @@ import {
66
Data,
77
} from './component'
88
import {
9-
Ref,
109
isRef,
1110
isReactive,
1211
markRaw,
1312
unwrapRefProxy,
1413
markReactive,
1514
} from './reactivity'
16-
import { getCurrentInstance, setCurrentInstance } from './runtimeContext'
17-
import {
18-
resolveSlots,
19-
createSlotProxy,
20-
hasOwn,
21-
isPlainObject,
22-
assert,
23-
proxy,
24-
warn,
25-
isFunction,
26-
} from './utils'
15+
import { isPlainObject, assert, proxy, warn, isFunction } from './utils'
2716
import { ref } from './apis'
2817
import vmStateManager from './utils/vmStateManager'
29-
30-
function asVmProperty(
31-
vm: ComponentInstance,
32-
propName: string,
33-
propValue: Ref<unknown>
34-
) {
35-
const props = vm.$options.props
36-
if (!(propName in vm) && !(props && hasOwn(props, propName))) {
37-
proxy(vm, propName, {
38-
get: () => propValue.value,
39-
set: (val: unknown) => {
40-
propValue.value = val
41-
},
42-
})
43-
44-
if (__DEV__) {
45-
// expose binding to Vue Devtool as a data property
46-
// delay this until state has been resolved to prevent repeated works
47-
vm.$nextTick(() => {
48-
proxy(vm._data, propName, {
49-
get: () => propValue.value,
50-
set: (val: unknown) => {
51-
propValue.value = val
52-
},
53-
})
54-
})
55-
}
56-
} else if (__DEV__) {
57-
if (props && hasOwn(props, propName)) {
58-
warn(
59-
`The setup binding property "${propName}" is already declared as a prop.`,
60-
vm
61-
)
62-
} else {
63-
warn(`The setup binding property "${propName}" is already declared.`, vm)
64-
}
65-
}
66-
}
67-
68-
function updateTemplateRef(vm: ComponentInstance) {
69-
const rawBindings = vmStateManager.get(vm, 'rawBindings') || {}
70-
if (!rawBindings || !Object.keys(rawBindings).length) return
71-
72-
const refs = vm.$refs
73-
const oldRefKeys = vmStateManager.get(vm, 'refs') || []
74-
for (let index = 0; index < oldRefKeys.length; index++) {
75-
const key = oldRefKeys[index]
76-
const setupValue = rawBindings[key]
77-
if (!refs[key] && setupValue && isRef(setupValue)) {
78-
setupValue.value = null
79-
}
80-
}
81-
82-
const newKeys = Object.keys(refs)
83-
const validNewKeys = []
84-
for (let index = 0; index < newKeys.length; index++) {
85-
const key = newKeys[index]
86-
const setupValue = rawBindings[key]
87-
if (refs[key] && setupValue && isRef(setupValue)) {
88-
setupValue.value = refs[key]
89-
validNewKeys.push(key)
90-
}
91-
}
92-
vmStateManager.set(vm, 'refs', validNewKeys)
93-
}
94-
95-
function resolveScopedSlots(
96-
vm: ComponentInstance,
97-
slotsProxy: { [x: string]: Function }
98-
): void {
99-
const parentVNode = (vm.$options as any)._parentVnode
100-
if (!parentVNode) return
101-
102-
const prevSlots = vmStateManager.get(vm, 'slots') || []
103-
const curSlots = resolveSlots(parentVNode.data.scopedSlots, vm.$slots)
104-
// remove staled slots
105-
for (let index = 0; index < prevSlots.length; index++) {
106-
const key = prevSlots[index]
107-
if (!curSlots[key]) {
108-
delete slotsProxy[key]
109-
}
110-
}
111-
112-
// proxy fresh slots
113-
const slotNames = Object.keys(curSlots)
114-
for (let index = 0; index < slotNames.length; index++) {
115-
const key = slotNames[index]
116-
if (!slotsProxy[key]) {
117-
slotsProxy[key] = createSlotProxy(vm, key)
118-
}
119-
}
120-
vmStateManager.set(vm, 'slots', slotNames)
121-
}
122-
123-
function activateCurrentInstance(
124-
vm: ComponentInstance,
125-
fn: (vm_: ComponentInstance) => any,
126-
onError?: (err: Error) => void
127-
) {
128-
let preVm = getCurrentInstance()
129-
setCurrentInstance(vm)
130-
try {
131-
return fn(vm)
132-
} catch (err) {
133-
if (onError) {
134-
onError(err)
135-
} else {
136-
throw err
137-
}
138-
} finally {
139-
setCurrentInstance(preVm)
140-
}
141-
}
18+
import {
19+
updateTemplateRef,
20+
activateCurrentInstance,
21+
resolveScopedSlots,
22+
asVmProperty,
23+
} from './utils/instance'
14224

14325
export function mixin(Vue: VueConstructor) {
14426
Vue.mixin({

src/utils/instance.ts

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import { ComponentInstance } from '../component'
2+
import vmStateManager from './vmStateManager'
3+
import { setCurrentInstance, getCurrentInstance } from '../runtimeContext'
4+
import { Ref, isRef } from '../apis'
5+
import { hasOwn, proxy, warn } from './utils'
6+
import { createSlotProxy, resolveSlots } from './helper'
7+
8+
export function asVmProperty(
9+
vm: ComponentInstance,
10+
propName: string,
11+
propValue: Ref<unknown>
12+
) {
13+
const props = vm.$options.props
14+
if (!(propName in vm) && !(props && hasOwn(props, propName))) {
15+
proxy(vm, propName, {
16+
get: () => propValue.value,
17+
set: (val: unknown) => {
18+
propValue.value = val
19+
},
20+
})
21+
22+
if (__DEV__) {
23+
// expose binding to Vue Devtool as a data property
24+
// delay this until state has been resolved to prevent repeated works
25+
vm.$nextTick(() => {
26+
proxy(vm._data, propName, {
27+
get: () => propValue.value,
28+
set: (val: unknown) => {
29+
propValue.value = val
30+
},
31+
})
32+
})
33+
}
34+
} else if (__DEV__) {
35+
if (props && hasOwn(props, propName)) {
36+
warn(
37+
`The setup binding property "${propName}" is already declared as a prop.`,
38+
vm
39+
)
40+
} else {
41+
warn(`The setup binding property "${propName}" is already declared.`, vm)
42+
}
43+
}
44+
}
45+
46+
export function updateTemplateRef(vm: ComponentInstance) {
47+
const rawBindings = vmStateManager.get(vm, 'rawBindings') || {}
48+
if (!rawBindings || !Object.keys(rawBindings).length) return
49+
50+
const refs = vm.$refs
51+
const oldRefKeys = vmStateManager.get(vm, 'refs') || []
52+
for (let index = 0; index < oldRefKeys.length; index++) {
53+
const key = oldRefKeys[index]
54+
const setupValue = rawBindings[key]
55+
if (!refs[key] && setupValue && isRef(setupValue)) {
56+
setupValue.value = null
57+
}
58+
}
59+
60+
const newKeys = Object.keys(refs)
61+
const validNewKeys = []
62+
for (let index = 0; index < newKeys.length; index++) {
63+
const key = newKeys[index]
64+
const setupValue = rawBindings[key]
65+
if (refs[key] && setupValue && isRef(setupValue)) {
66+
setupValue.value = refs[key]
67+
validNewKeys.push(key)
68+
}
69+
}
70+
vmStateManager.set(vm, 'refs', validNewKeys)
71+
}
72+
73+
export function resolveScopedSlots(
74+
vm: ComponentInstance,
75+
slotsProxy: { [x: string]: Function }
76+
): void {
77+
const parentVNode = (vm.$options as any)._parentVnode
78+
if (!parentVNode) return
79+
80+
const prevSlots = vmStateManager.get(vm, 'slots') || []
81+
const curSlots = resolveSlots(parentVNode.data.scopedSlots, vm.$slots)
82+
// remove staled slots
83+
for (let index = 0; index < prevSlots.length; index++) {
84+
const key = prevSlots[index]
85+
if (!curSlots[key]) {
86+
delete slotsProxy[key]
87+
}
88+
}
89+
90+
// proxy fresh slots
91+
const slotNames = Object.keys(curSlots)
92+
for (let index = 0; index < slotNames.length; index++) {
93+
const key = slotNames[index]
94+
if (!slotsProxy[key]) {
95+
slotsProxy[key] = createSlotProxy(vm, key)
96+
}
97+
}
98+
vmStateManager.set(vm, 'slots', slotNames)
99+
}
100+
101+
export function activateCurrentInstance(
102+
vm: ComponentInstance,
103+
fn: (vm_: ComponentInstance) => any,
104+
onError?: (err: Error) => void
105+
) {
106+
let preVm = getCurrentInstance()
107+
setCurrentInstance(vm)
108+
try {
109+
return fn(vm)
110+
} catch (err) {
111+
if (onError) {
112+
onError(err)
113+
} else {
114+
throw err
115+
}
116+
} finally {
117+
setCurrentInstance(preVm)
118+
}
119+
}

0 commit comments

Comments
 (0)