6
6
*
7
7
*/
8
8
9
+ function hasMoreKeys ( okeys , nkeys ) {
10
+ const nkeysSet = new Set ( nkeys )
11
+
12
+ return okeys . some ( key => ! nkeysSet . has ( key ) )
13
+ }
14
+
9
15
function getObjectPathInner ( v , prefix , result ) {
10
16
const tv = typeof v
11
17
@@ -54,17 +60,17 @@ function getChangePathInner(newR, oldR, prefix, result) {
54
60
) {
55
61
getObjectPathInner ( newR , prefix , result )
56
62
} else if ( Array . isArray ( newR ) ) {
63
+ // 由于小程序 setData 设置为 undefined 会出问题。 所以这种情况直接设置对象
64
+ if ( newR . length < oldR . length ) {
65
+ result [ prefix ] = newR
66
+ return
67
+ }
68
+
57
69
for ( let i = 0 ; i < newR . length ; i ++ ) {
58
70
const v = newR [ i ]
59
71
const ov = oldR [ i ]
60
72
getChangePathInner ( v , ov , `${ prefix } [${ i } ]` , result )
61
73
}
62
-
63
- if ( newR . length < oldR . length ) {
64
- for ( let i = newR . length ; i < oldR . length ; i ++ ) {
65
- result [ `${ prefix } [${ i } ]` ] = null
66
- }
67
- }
68
74
} else if ( tn === 'object' && tn !== null ) {
69
75
if ( newR . __isAnimation__ ) {
70
76
result [ prefix ] = newR
@@ -73,18 +79,21 @@ function getChangePathInner(newR, oldR, prefix, result) {
73
79
74
80
75
81
const nkeys = Object . keys ( newR )
82
+ const okeys = Object . keys ( oldR )
83
+
84
+ // 由于小程序 setData 设置为 undefined 会出问题。 所以这种情况直接设置对象
85
+ // TODO 这种情况下, 是否依然可以减少数据的传递呢??
86
+ if ( hasMoreKeys ( okeys , nkeys ) ) {
87
+ result [ prefix ] = newR
88
+ return
89
+ }
90
+
76
91
for ( let i = 0 ; i < nkeys . length ; i ++ ) {
77
92
const k = nkeys [ i ]
78
93
const v = newR [ k ]
79
94
const ov = oldR [ k ]
80
95
getChangePathInner ( v , ov , `${ prefix } .${ k } ` , result )
81
96
}
82
-
83
- const okeys = Object . keys ( oldR )
84
- const onlyNkeys = okeys . filter ( k => newR [ k ] === undefined )
85
- for ( let i = 0 ; i < onlyNkeys . length ; i ++ ) {
86
- result [ `${ prefix } .${ onlyNkeys [ i ] } ` ] = null
87
- }
88
97
} else {
89
98
result [ prefix ] = newR
90
99
}
0 commit comments