@@ -6,139 +6,21 @@ import {
6
6
Data ,
7
7
} from './component'
8
8
import {
9
- Ref ,
10
9
isRef ,
11
10
isReactive ,
12
11
markRaw ,
13
12
unwrapRefProxy ,
14
13
markReactive ,
15
14
} 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'
27
16
import { ref } from './apis'
28
17
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'
142
24
143
25
export function mixin ( Vue : VueConstructor ) {
144
26
Vue . mixin ( {
0 commit comments