@@ -26,7 +26,6 @@ import {
26
26
hasChanged ,
27
27
isArray ,
28
28
isIntegerKey ,
29
- extend ,
30
29
makeMap
31
30
} from '@vue/shared'
32
31
import { isRef } from './ref'
@@ -45,11 +44,6 @@ const builtInSymbols = new Set(
45
44
. filter ( isSymbol )
46
45
)
47
46
48
- const get = /*#__PURE__*/ createGetter ( )
49
- const shallowGet = /*#__PURE__*/ createGetter ( false , true )
50
- const readonlyGet = /*#__PURE__*/ createGetter ( true )
51
- const shallowReadonlyGet = /*#__PURE__*/ createGetter ( true , true )
52
-
53
47
const arrayInstrumentations = /*#__PURE__*/ createArrayInstrumentations ( )
54
48
55
49
function createArrayInstrumentations ( ) {
@@ -91,22 +85,27 @@ function hasOwnProperty(this: object, key: string) {
91
85
return obj . hasOwnProperty ( key )
92
86
}
93
87
94
- function createGetter ( isReadonly = false , shallow = false ) {
95
- return function get ( target : Target , key : string | symbol , receiver : object ) {
88
+ class BaseReactiveHandler implements ProxyHandler < Target > {
89
+ constructor (
90
+ protected readonly _isReadonly = false ,
91
+ protected readonly _shallow = false
92
+ ) { }
93
+
94
+ get ( target : Target , key : string | symbol , receiver : object ) {
96
95
if ( key === ReactiveFlags . IS_REACTIVE ) {
97
- return ! isReadonly
96
+ return ! this . _isReadonly
98
97
} else if ( key === ReactiveFlags . IS_READONLY ) {
99
- return isReadonly
98
+ return this . _isReadonly
100
99
} else if ( key === ReactiveFlags . IS_SHALLOW ) {
101
- return shallow
100
+ return this . _shallow
102
101
} else if (
103
102
key === ReactiveFlags . RAW &&
104
103
receiver ===
105
- ( isReadonly
106
- ? shallow
104
+ ( this . _isReadonly
105
+ ? this . _shallow
107
106
? shallowReadonlyMap
108
107
: readonlyMap
109
- : shallow
108
+ : this . _shallow
110
109
? shallowReactiveMap
111
110
: reactiveMap
112
111
) . get ( target )
@@ -116,7 +115,7 @@ function createGetter(isReadonly = false, shallow = false) {
116
115
117
116
const targetIsArray = isArray ( target )
118
117
119
- if ( ! isReadonly ) {
118
+ if ( ! this . _isReadonly ) {
120
119
if ( targetIsArray && hasOwn ( arrayInstrumentations , key ) ) {
121
120
return Reflect . get ( arrayInstrumentations , key , receiver )
122
121
}
@@ -131,11 +130,11 @@ function createGetter(isReadonly = false, shallow = false) {
131
130
return res
132
131
}
133
132
134
- if ( ! isReadonly ) {
133
+ if ( ! this . _isReadonly ) {
135
134
track ( target , TrackOpTypes . GET , key )
136
135
}
137
136
138
- if ( shallow ) {
137
+ if ( this . _shallow ) {
139
138
return res
140
139
}
141
140
@@ -148,18 +147,19 @@ function createGetter(isReadonly = false, shallow = false) {
148
147
// Convert returned value into a proxy as well. we do the isObject check
149
148
// here to avoid invalid value warning. Also need to lazy access readonly
150
149
// and reactive here to avoid circular dependency.
151
- return isReadonly ? readonly ( res ) : reactive ( res )
150
+ return this . _isReadonly ? readonly ( res ) : reactive ( res )
152
151
}
153
152
154
153
return res
155
154
}
156
155
}
157
156
158
- const set = /*#__PURE__*/ createSetter ( )
159
- const shallowSet = /*#__PURE__*/ createSetter ( true )
157
+ class MutableReactiveHandler extends BaseReactiveHandler {
158
+ constructor ( shallow = false ) {
159
+ super ( false , shallow )
160
+ }
160
161
161
- function createSetter ( shallow = false ) {
162
- return function set (
162
+ set (
163
163
target : object ,
164
164
key : string | symbol ,
165
165
value : unknown ,
@@ -169,7 +169,7 @@ function createSetter(shallow = false) {
169
169
if ( isReadonly ( oldValue ) && isRef ( oldValue ) && ! isRef ( value ) ) {
170
170
return false
171
171
}
172
- if ( ! shallow ) {
172
+ if ( ! this . _shallow ) {
173
173
if ( ! isShallow ( value ) && ! isReadonly ( value ) ) {
174
174
oldValue = toRaw ( oldValue )
175
175
value = toRaw ( value )
@@ -197,51 +197,50 @@ function createSetter(shallow = false) {
197
197
}
198
198
return result
199
199
}
200
- }
201
200
202
- function deleteProperty ( target : object , key : string | symbol ) : boolean {
203
- const hadKey = hasOwn ( target , key )
204
- const oldValue = ( target as any ) [ key ]
205
- const result = Reflect . deleteProperty ( target , key )
206
- if ( result && hadKey ) {
207
- trigger ( target , TriggerOpTypes . DELETE , key , undefined , oldValue )
201
+ deleteProperty ( target : object , key : string | symbol ) : boolean {
202
+ const hadKey = hasOwn ( target , key )
203
+ const oldValue = ( target as any ) [ key ]
204
+ const result = Reflect . deleteProperty ( target , key )
205
+ if ( result && hadKey ) {
206
+ trigger ( target , TriggerOpTypes . DELETE , key , undefined , oldValue )
207
+ }
208
+ return result
208
209
}
209
- return result
210
- }
211
210
212
- function has ( target : object , key : string | symbol ) : boolean {
213
- const result = Reflect . has ( target , key )
214
- if ( ! isSymbol ( key ) || ! builtInSymbols . has ( key ) ) {
215
- track ( target , TrackOpTypes . HAS , key )
211
+ has ( target : object , key : string | symbol ) : boolean {
212
+ const result = Reflect . has ( target , key )
213
+ if ( ! isSymbol ( key ) || ! builtInSymbols . has ( key ) ) {
214
+ track ( target , TrackOpTypes . HAS , key )
215
+ }
216
+ return result
217
+ }
218
+ ownKeys ( target : object ) : ( string | symbol ) [ ] {
219
+ track (
220
+ target ,
221
+ TrackOpTypes . ITERATE ,
222
+ isArray ( target ) ? 'length' : ITERATE_KEY
223
+ )
224
+ return Reflect . ownKeys ( target )
216
225
}
217
- return result
218
- }
219
-
220
- function ownKeys ( target : object ) : ( string | symbol ) [ ] {
221
- track ( target , TrackOpTypes . ITERATE , isArray ( target ) ? 'length' : ITERATE_KEY )
222
- return Reflect . ownKeys ( target )
223
226
}
224
227
225
- export const mutableHandlers : ProxyHandler < object > = {
226
- get,
227
- set,
228
- deleteProperty,
229
- has,
230
- ownKeys
231
- }
228
+ class ReadonlyReactiveHandler extends BaseReactiveHandler {
229
+ constructor ( shallow = false ) {
230
+ super ( true , shallow )
231
+ }
232
232
233
- export const readonlyHandlers : ProxyHandler < object > = {
234
- get : readonlyGet ,
235
- set ( target , key ) {
233
+ set ( target : object , key : string | symbol ) {
236
234
if ( __DEV__ ) {
237
235
warn (
238
236
`Set operation on key "${ String ( key ) } " failed: target is readonly.` ,
239
237
target
240
238
)
241
239
}
242
240
return true
243
- } ,
244
- deleteProperty ( target , key ) {
241
+ }
242
+
243
+ deleteProperty ( target : object , key : string | symbol ) {
245
244
if ( __DEV__ ) {
246
245
warn (
247
246
`Delete operation on key "${ String ( key ) } " failed: target is readonly.` ,
@@ -252,22 +251,18 @@ export const readonlyHandlers: ProxyHandler<object> = {
252
251
}
253
252
}
254
253
255
- export const shallowReactiveHandlers = /*#__PURE__*/ extend (
256
- { } ,
257
- mutableHandlers ,
258
- {
259
- get : shallowGet ,
260
- set : shallowSet
261
- }
254
+ export const mutableHandlers : ProxyHandler < object > =
255
+ /*#__PURE__*/ new MutableReactiveHandler ( )
256
+
257
+ export const readonlyHandlers : ProxyHandler < object > =
258
+ /*#__PURE__*/ new ReadonlyReactiveHandler ( )
259
+
260
+ export const shallowReactiveHandlers = /*#__PURE__*/ new MutableReactiveHandler (
261
+ true
262
262
)
263
263
264
264
// Props handlers are special in the sense that it should not unwrap top-level
265
265
// refs (in order to allow refs to be explicitly passed down), but should
266
266
// retain the reactivity of the normal readonly object.
267
- export const shallowReadonlyHandlers = /*#__PURE__*/ extend (
268
- { } ,
269
- readonlyHandlers ,
270
- {
271
- get : shallowReadonlyGet
272
- }
273
- )
267
+ export const shallowReadonlyHandlers =
268
+ /*#__PURE__*/ new ReadonlyReactiveHandler ( true )
0 commit comments