@@ -87,199 +87,209 @@ export class Proxifier {
8787 return this . tree . master . mutationTree || ( this . tree as IMutationTree < any > )
8888 }
8989 private createArrayProxy ( value , path ) {
90- var proxifier = this
91-
92- return ( value [ this . CACHED_PROXY ] =
93- String ( value [ PATH ] ) === String ( path )
94- ? value [ this . CACHED_PROXY ]
95- : new Proxy ( value , {
96- get ( target , prop ) {
97- if ( prop === IS_PROXY ) return true
98- if ( prop === PATH ) return path
99- if ( prop === VALUE ) return value
100-
101- if (
102- prop === 'length' ||
103- ( typeof target [ prop ] === 'function' &&
104- ! arrayMutations . has ( String ( prop ) ) ) ||
105- typeof prop === 'symbol'
106- ) {
107- return target [ prop ]
108- }
109-
110- const trackingTree = proxifier . getTrackingTree ( )
111- const nestedPath = proxifier . concat ( path , prop )
112-
113- trackingTree && trackingTree . proxifier . trackPath ( nestedPath )
114-
115- const method = String ( prop )
116-
117- if ( arrayMutations . has ( method ) ) {
118- proxifier . ensureMutationTrackingIsEnabled ( nestedPath )
119- return ( ...args ) => {
120- const mutationTree = proxifier . getMutationTree ( )
121-
122- mutationTree . addMutation ( {
123- method,
124- path : path ,
125- args : args ,
126- hasChangedValue : true ,
127- } )
128-
129- return target [ prop ] ( ...args )
130- }
131- }
132-
133- if ( target [ prop ] === undefined ) {
134- return undefined
135- }
136-
137- return proxifier . proxify ( target [ prop ] , nestedPath )
138- } ,
139- set ( target , prop , value ) {
140- const nestedPath = proxifier . concat ( path , prop )
141-
142- proxifier . ensureMutationTrackingIsEnabled ( nestedPath )
143- proxifier . ensureValueDosntExistInStateTreeElsewhere ( value )
144-
145- const mutationTree = proxifier . getMutationTree ( )
146-
147- mutationTree . addMutation ( {
148- method : 'set' ,
149- path : nestedPath ,
150- args : [ value ] ,
151- hasChangedValue : true ,
152- } )
153-
154- return Reflect . set ( target , prop , value )
155- } ,
156- } ) )
90+ if ( value [ this . CACHED_PROXY ] && String ( value [ PATH ] ) === String ( path ) ) {
91+ return value [ this . CACHED_PROXY ]
92+ }
93+
94+ const proxifier = this
95+
96+ const proxy = new Proxy ( value , {
97+ get ( target , prop ) {
98+ if ( prop === IS_PROXY ) return true
99+ if ( prop === PATH ) return path
100+ if ( prop === VALUE ) return value
101+
102+ if (
103+ prop === 'length' ||
104+ ( typeof target [ prop ] === 'function' &&
105+ ! arrayMutations . has ( String ( prop ) ) ) ||
106+ typeof prop === 'symbol'
107+ ) {
108+ return target [ prop ]
109+ }
110+
111+ const trackingTree = proxifier . getTrackingTree ( )
112+ const nestedPath = proxifier . concat ( path , prop )
113+
114+ trackingTree && trackingTree . proxifier . trackPath ( nestedPath )
115+
116+ const method = String ( prop )
117+
118+ if ( arrayMutations . has ( method ) ) {
119+ proxifier . ensureMutationTrackingIsEnabled ( nestedPath )
120+ return ( ...args ) => {
121+ const mutationTree = proxifier . getMutationTree ( )
122+
123+ mutationTree . addMutation ( {
124+ method,
125+ path : path ,
126+ args : args ,
127+ hasChangedValue : true ,
128+ } )
129+
130+ return target [ prop ] ( ...args )
131+ }
132+ }
133+
134+ if ( target [ prop ] === undefined ) {
135+ return undefined
136+ }
137+
138+ return proxifier . proxify ( target [ prop ] , nestedPath )
139+ } ,
140+ set ( target , prop , value ) {
141+ const nestedPath = proxifier . concat ( path , prop )
142+
143+ proxifier . ensureMutationTrackingIsEnabled ( nestedPath )
144+ proxifier . ensureValueDosntExistInStateTreeElsewhere ( value )
145+
146+ const mutationTree = proxifier . getMutationTree ( )
147+
148+ mutationTree . addMutation ( {
149+ method : 'set' ,
150+ path : nestedPath ,
151+ args : [ value ] ,
152+ hasChangedValue : true ,
153+ } )
154+
155+ return Reflect . set ( target , prop , value )
156+ } ,
157+ } )
158+
159+ Object . defineProperty ( value , this . CACHED_PROXY , {
160+ value : proxy ,
161+ configurable : true ,
162+ } )
163+
164+ return proxy
157165 }
158166
159167 private createObjectProxy ( object , path ) {
168+ if ( object [ this . CACHED_PROXY ] && String ( object [ PATH ] ) === String ( path ) ) {
169+ return object [ this . CACHED_PROXY ]
170+ }
171+
160172 const proxifier = this
161173
162- return ( object [ this . CACHED_PROXY ] =
163- String ( object [ PATH ] ) === String ( path )
164- ? object [ this . CACHED_PROXY ]
165- : new Proxy ( object , {
166- get ( target , prop ) {
167- if ( prop === IS_PROXY ) return true
168- if ( prop === PATH ) return path
169- if ( prop === VALUE ) return object
170-
171- if ( typeof prop === 'symbol' || prop in Object . prototype )
172- return target [ prop ]
173-
174- const descriptor = Object . getOwnPropertyDescriptor ( target , prop )
175-
176- if ( descriptor && 'get' in descriptor ) {
177- const value = descriptor . get . call (
178- object [ proxifier . CACHED_PROXY ]
179- )
180-
181- if (
182- proxifier . tree . master . options . devmode &&
183- proxifier . tree . master . options . onGetter
184- ) {
185- proxifier . tree . master . options . onGetter (
186- proxifier . concat ( path , prop ) ,
187- value
188- )
189- }
190-
191- return value
192- }
193-
194- const trackingTree = proxifier . getTrackingTree ( )
195- const targetValue = target [ prop ]
196- const nestedPath = proxifier . concat ( path , prop )
197-
198- trackingTree && trackingTree . proxifier . trackPath ( nestedPath )
199-
200- if ( typeof targetValue === 'function' ) {
201- const dynamicValue = proxifier . tree . master . options
202- . dynamicWrapper
203- ? proxifier . tree . master . options . dynamicWrapper (
204- proxifier . tree ,
205- nestedPath ,
206- targetValue
207- )
208- : targetValue ( proxifier . tree , nestedPath )
209-
210- if ( dynamicValue && dynamicValue [ IS_PROXY ] ) {
211- return proxifier . proxify (
212- dynamicValue [ VALUE ] ,
213- dynamicValue [ PATH ]
214- )
215- }
216-
217- return dynamicValue
218- }
219-
220- if ( targetValue === undefined ) {
221- return undefined
222- }
223-
224- return proxifier . proxify ( targetValue , nestedPath )
225- } ,
226- set ( target , prop , value ) {
227- const nestedPath = proxifier . concat ( path , prop )
228-
229- proxifier . ensureMutationTrackingIsEnabled ( nestedPath )
230- proxifier . ensureValueDosntExistInStateTreeElsewhere ( value )
231-
232- let objectChangePath
233-
234- if ( ! ( prop in target ) ) {
235- objectChangePath = path
236- }
237-
238- const mutationTree = proxifier . getMutationTree ( )
239-
240- mutationTree . addMutation (
241- {
242- method : 'set' ,
243- path : nestedPath ,
244- args : [ value ] ,
245- hasChangedValue : value !== target [ prop ] ,
246- } ,
247- objectChangePath
174+ const proxy = new Proxy ( object , {
175+ get ( target , prop ) {
176+ if ( prop === IS_PROXY ) return true
177+ if ( prop === PATH ) return path
178+ if ( prop === VALUE ) return object
179+
180+ if ( typeof prop === 'symbol' || prop in Object . prototype )
181+ return target [ prop ]
182+
183+ const descriptor = Object . getOwnPropertyDescriptor ( target , prop )
184+
185+ if ( descriptor && 'get' in descriptor ) {
186+ const value = descriptor . get . call ( object [ proxifier . CACHED_PROXY ] )
187+
188+ if (
189+ proxifier . tree . master . options . devmode &&
190+ proxifier . tree . master . options . onGetter
191+ ) {
192+ proxifier . tree . master . options . onGetter (
193+ proxifier . concat ( path , prop ) ,
194+ value
195+ )
196+ }
197+
198+ return value
199+ }
200+
201+ const trackingTree = proxifier . getTrackingTree ( )
202+ const targetValue = target [ prop ]
203+ const nestedPath = proxifier . concat ( path , prop )
204+
205+ trackingTree && trackingTree . proxifier . trackPath ( nestedPath )
206+
207+ if ( typeof targetValue === 'function' ) {
208+ const dynamicValue = proxifier . tree . master . options . dynamicWrapper
209+ ? proxifier . tree . master . options . dynamicWrapper (
210+ proxifier . tree ,
211+ nestedPath ,
212+ targetValue
248213 )
214+ : targetValue ( proxifier . tree , nestedPath )
249215
250- if ( typeof value === 'function' ) {
251- return Reflect . set ( target , prop , ( ) => value )
252- }
216+ if ( dynamicValue && dynamicValue [ IS_PROXY ] ) {
217+ return proxifier . proxify ( dynamicValue [ VALUE ] , dynamicValue [ PATH ] )
218+ }
253219
254- return Reflect . set ( target , prop , value )
255- } ,
256- deleteProperty ( target , prop ) {
257- const nestedPath = proxifier . concat ( path , prop )
220+ return dynamicValue
221+ }
258222
259- proxifier . ensureMutationTrackingIsEnabled ( nestedPath )
223+ if ( targetValue === undefined ) {
224+ return undefined
225+ }
260226
261- let objectChangePath
262- if ( prop in target ) {
263- objectChangePath = path
264- }
227+ return proxifier . proxify ( targetValue , nestedPath )
228+ } ,
229+ set ( target , prop , value ) {
230+ const nestedPath = proxifier . concat ( path , prop )
265231
266- const mutationTree = proxifier . getMutationTree ( )
232+ proxifier . ensureMutationTrackingIsEnabled ( nestedPath )
233+ proxifier . ensureValueDosntExistInStateTreeElsewhere ( value )
267234
268- mutationTree . addMutation (
269- {
270- method : 'unset' ,
271- path : nestedPath ,
272- args : [ ] ,
273- hasChangedValue : true ,
274- } ,
275- objectChangePath
276- )
235+ let objectChangePath
236+
237+ if ( ! ( prop in target ) ) {
238+ objectChangePath = path
239+ }
240+
241+ const mutationTree = proxifier . getMutationTree ( )
242+
243+ mutationTree . addMutation (
244+ {
245+ method : 'set' ,
246+ path : nestedPath ,
247+ args : [ value ] ,
248+ hasChangedValue : value !== target [ prop ] ,
249+ } ,
250+ objectChangePath
251+ )
252+
253+ if ( typeof value === 'function' ) {
254+ return Reflect . set ( target , prop , ( ) => value )
255+ }
256+
257+ return Reflect . set ( target , prop , value )
258+ } ,
259+ deleteProperty ( target , prop ) {
260+ const nestedPath = proxifier . concat ( path , prop )
261+
262+ proxifier . ensureMutationTrackingIsEnabled ( nestedPath )
263+
264+ let objectChangePath
265+ if ( prop in target ) {
266+ objectChangePath = path
267+ }
268+
269+ const mutationTree = proxifier . getMutationTree ( )
270+
271+ mutationTree . addMutation (
272+ {
273+ method : 'unset' ,
274+ path : nestedPath ,
275+ args : [ ] ,
276+ hasChangedValue : true ,
277+ } ,
278+ objectChangePath
279+ )
280+
281+ delete target [ prop ]
282+
283+ return true
284+ } ,
285+ } )
277286
278- delete target [ prop ]
287+ Object . defineProperty ( object , this . CACHED_PROXY , {
288+ value : proxy ,
289+ configurable : true ,
290+ } )
279291
280- return true
281- } ,
282- } ) )
292+ return proxy
283293 }
284294 proxify ( value : any , path : string ) {
285295 if ( value ) {
0 commit comments