@@ -130,7 +130,7 @@ const getParentNode = (fiber: IFiber): HTMLElement | undefined => {
130
130
const diffKids = ( fiber : any , children : FreNode ) : void => {
131
131
let aCh = fiber . kids || [ ] ,
132
132
bCh = ( fiber . kids = arrayfy ( children ) as any )
133
- const actions = idiff ( aCh , bCh )
133
+ const actions = diff ( aCh , bCh )
134
134
135
135
for ( let i = 0 , prev = null , len = bCh . length ; i < len ; i ++ ) {
136
136
@@ -165,13 +165,11 @@ const side = (effects: IEffect[]): void => {
165
165
effects . length = 0
166
166
}
167
167
168
- const diff = function ( opts ) {
168
+ const diff = function ( a , b ) {
169
169
var actions = [ ] ,
170
170
aIdx = { } ,
171
171
bIdx = { } ,
172
- a = opts . old ,
173
- b = opts . cur ,
174
- key = opts . key ,
172
+ key = v => v . key + v . type ,
175
173
i , j ;
176
174
for ( i = 0 ; i < a . length ; i ++ ) {
177
175
aIdx [ key ( a [ i ] ) ] = i ;
@@ -184,27 +182,27 @@ const diff = function (opts) {
184
182
if ( aElm === null ) {
185
183
i ++ ;
186
184
} else if ( b . length <= j ) {
187
- opts . remove ( i )
185
+ removeElement ( a [ i ] )
188
186
i ++ ;
189
187
} else if ( a . length <= i ) {
190
- opts . add ( bElm , i )
188
+ actions . push ( { op : TAG . INSERT , elm : bElm , before : a [ i ] } )
191
189
j ++ ;
192
190
} else if ( key ( aElm ) === key ( bElm ) ) {
193
191
clone ( aElm , bElm )
194
- opts . update ( aElm , bElm )
192
+ actions . push ( { op : TAG . UPDATE } )
195
193
i ++ ; j ++ ;
196
194
} else {
197
195
var curElmInNew = bIdx [ key ( aElm ) ]
198
196
var wantedElmInOld = aIdx [ key ( bElm ) ]
199
197
if ( curElmInNew === undefined ) {
200
- opts . remove ( i ) ;
198
+ removeElement ( a [ i ] )
201
199
i ++ ;
202
200
} else if ( wantedElmInOld === undefined ) {
203
- opts . add ( bElm , i )
201
+ actions . push ( { op : TAG . INSERT , elm : bElm , before : a [ i ] } )
204
202
j ++
205
203
} else {
206
204
clone ( a [ wantedElmInOld ] , bElm )
207
- opts . move ( wantedElmInOld , i )
205
+ actions . push ( { op : TAG . MOVE , elm : a [ wantedElmInOld ] , before : a [ i ] } )
208
206
a [ wantedElmInOld ] = null
209
207
j ++
210
208
}
@@ -213,26 +211,6 @@ const diff = function (opts) {
213
211
return actions
214
212
}
215
213
216
- var idiff = function ( a , b ) {
217
- var actions = [ ] // 必须保持和 b 一致的顺序
218
- var extr = ( v ) => v . key + v . type
219
- var update = ( a , b ) => actions . push ( { op : TAG . UPDATE } )
220
- var move = ( from , to ) => actions . push ( { op : TAG . MOVE , elm : a [ from ] , before : a [ to ] } )
221
- var add = ( elm , i ) => actions . push ( { op : TAG . INSERT , elm : elm , before : a [ i ] } )
222
- var remove = ( i ) => {
223
- const fiber = a [ i ]
224
- removeElement ( fiber )
225
- }
226
-
227
- diff ( {
228
- old : a ,
229
- cur : b ,
230
- key : extr ,
231
- add, move, remove, update
232
- } )
233
- return actions
234
- }
235
-
236
214
export const getCurrentFiber = ( ) => currentFiber || null
237
215
export const isFn = ( x : any ) : x is Function => typeof x === 'function'
238
216
export const isStr = ( s : any ) : s is number | string =>
0 commit comments