@@ -9,14 +9,6 @@ export enum STATUS {
99 TRACKING_MUTATIONS = 'TRACKING_MUTATIONS' ,
1010}
1111
12- function concat ( path , prop ) {
13- return path === undefined ? prop : path + '.' + prop
14- }
15-
16- function shouldTrackMutations ( tree , path ) {
17- return tree . options . devmode || ( path && tree . pathDependencies [ path ] )
18- }
19-
2012const arrayMutations = new Set ( [
2113 'push' ,
2214 'shift' ,
@@ -28,6 +20,38 @@ const arrayMutations = new Set([
2820 'copyWithin' ,
2921] )
3022
23+ function concat ( path , prop ) {
24+ return path === undefined ? prop : path + '.' + prop
25+ }
26+
27+ function shouldTrackMutations ( tree , path ) {
28+ return tree . options . devmode || ( path && tree . pathDependencies [ path ] )
29+ }
30+
31+ function ensureMutationTrackingIsEnabled ( tree , path ) {
32+ if ( ! tree . status . has ( STATUS . TRACKING_MUTATIONS ) ) {
33+ throw new Error (
34+ `proxy-state-tree - You are mutating the path "${ path } ", but it is not allowed`
35+ )
36+ }
37+ }
38+
39+ function ensureValueDosntExistInStateTreeElsewhere ( value ) {
40+ if ( value && value [ IS_PROXY ] === true ) {
41+ throw new Error (
42+ `proxy-state-tree - You are trying to insert a value that already exists in the state tree on path "${
43+ value [ PATH ]
44+ } "`
45+ )
46+ }
47+ }
48+
49+ function trackPath ( tree , path ) {
50+ if ( tree . status . has ( STATUS . TRACKING_PATHS ) ) {
51+ tree . paths [ tree . paths . length - 1 ] . add ( path )
52+ }
53+ }
54+
3155function createArrayProxy ( tree , value , path ) {
3256 return new Proxy ( value , {
3357 get ( target , prop ) {
@@ -45,19 +69,13 @@ function createArrayProxy(tree, value, path) {
4569 }
4670
4771 const nestedPath = concat ( path , prop )
48- if ( tree . status . has ( STATUS . TRACKING_PATHS ) ) {
49- tree . paths [ tree . paths . length - 1 ] . add ( nestedPath )
50- }
72+ trackPath ( tree , nestedPath )
5173
5274 if (
5375 arrayMutations . has ( String ( prop ) ) &&
5476 shouldTrackMutations ( tree , nestedPath )
5577 ) {
56- if ( ! tree . status . has ( STATUS . TRACKING_MUTATIONS ) ) {
57- throw new Error (
58- `proxy-state-tree - You are mutating the path "${ nestedPath } ", but it is not allowed`
59- )
60- }
78+ ensureMutationTrackingIsEnabled ( tree , nestedPath )
6179 return ( ...args ) => {
6280 tree . currentMutations . push ( {
6381 method : prop ,
@@ -78,23 +96,15 @@ function createArrayProxy(tree, value, path) {
7896 set ( target , prop , value ) {
7997 const nestedPath = concat ( path , prop )
8098
81- if ( ! tree . status . has ( STATUS . TRACKING_MUTATIONS ) ) {
82- throw new Error (
83- `proxy-state-tree - You are mutating the path "${ nestedPath } ", but it is not allowed`
84- )
85- }
86- if ( value && value [ IS_PROXY ] === true ) {
87- throw new Error (
88- `proxy-state-tree - You are trying to insert a value that already exists in the state tree on path "${
89- value [ PATH ]
90- } "`
91- )
92- }
99+ ensureMutationTrackingIsEnabled ( tree , nestedPath )
100+ ensureValueDosntExistInStateTreeElsewhere ( value )
101+
93102 tree . currentMutations . push ( {
94103 method : 'set' ,
95104 path : nestedPath ,
96105 args : [ value ] ,
97106 } )
107+
98108 return Reflect . set ( target , prop , value )
99109 } ,
100110 } )
@@ -112,9 +122,7 @@ function createObjectProxy(tree, value, path) {
112122
113123 const targetValue = target [ prop ]
114124 const nestedPath = concat ( path , prop )
115- if ( tree . status . has ( STATUS . TRACKING_PATHS ) ) {
116- tree . paths [ tree . paths . length - 1 ] . add ( nestedPath )
117- }
125+ trackPath ( tree , nestedPath )
118126
119127 if ( typeof targetValue === 'function' ) {
120128 return tree . options . dynamicWrapper
@@ -131,18 +139,9 @@ function createObjectProxy(tree, value, path) {
131139 set ( target , prop , value ) {
132140 const nestedPath = concat ( path , prop )
133141
134- if ( ! tree . status . has ( STATUS . TRACKING_MUTATIONS ) ) {
135- throw new Error (
136- `proxy-state-tree - You are mutating the path "${ nestedPath } ", but it is not allowed`
137- )
138- }
139- if ( value && value [ IS_PROXY ] === true ) {
140- throw new Error (
141- `proxy-state-tree - You are trying to insert a value that already exists in the state tree on path "${
142- value [ PATH ]
143- } "`
144- )
145- }
142+ ensureMutationTrackingIsEnabled ( tree , nestedPath )
143+ ensureValueDosntExistInStateTreeElsewhere ( value )
144+
146145 if ( shouldTrackMutations ( tree , nestedPath ) ) {
147146 if ( ! ( prop in target ) ) {
148147 tree . objectChanges . add ( path )
@@ -160,11 +159,8 @@ function createObjectProxy(tree, value, path) {
160159 deleteProperty ( target , prop ) {
161160 const nestedPath = concat ( path , prop )
162161
163- if ( ! tree . status . has ( STATUS . TRACKING_MUTATIONS ) ) {
164- throw new Error (
165- `proxy-state-tree - You are mutating the path "${ nestedPath } ", but it is not allowed`
166- )
167- }
162+ ensureMutationTrackingIsEnabled ( tree , nestedPath )
163+
168164 if ( shouldTrackMutations ( tree , nestedPath ) ) {
169165 if ( prop in target ) {
170166 tree . objectChanges . add ( path )
@@ -190,10 +186,10 @@ function proxify(tree, value, path?) {
190186 return proxify ( tree , value [ VALUE ] , path )
191187 } else if ( value [ IS_PROXY ] ) {
192188 return value
193- } else if ( Array . isArray ( value ) ) {
194- return createArrayProxy ( tree , value , path )
195189 } else if ( isPlainObject ( value ) ) {
196190 return createObjectProxy ( tree , value , path )
191+ } else if ( Array . isArray ( value ) ) {
192+ return createArrayProxy ( tree , value , path )
197193 }
198194 }
199195 return value
0 commit comments