@@ -121,73 +121,81 @@ export function populateNodeData(startCellIndex: number, endCellIndex: number, s
121
121
initialLoad : boolean ) : boolean {
122
122
if ( ! records || records . length === 0 ) {
123
123
nodes . length = 0 ;
124
+ viewContainer . clear ( ) ;
124
125
return true ;
125
126
}
126
127
const recordsLength = records . length ;
127
128
128
129
let hasChanges = false ;
129
- let node : VirtualNode ;
130
+ // let node: VirtualNode;
130
131
let availableNode : VirtualNode ;
131
132
let cell : VirtualCell ;
132
133
let viewInsertIndex : number = null ;
133
134
let totalNodes = nodes . length ;
134
135
let templateRef : TemplateRef < VirtualContext > ;
135
136
startCellIndex = Math . max ( startCellIndex , 0 ) ;
136
137
endCellIndex = Math . min ( endCellIndex , cells . length - 1 ) ;
138
+ console . log ( initialLoad ) ;
137
139
140
+ const usedNodes : any [ ] = [ ] ;
138
141
for ( var cellIndex = startCellIndex ; cellIndex <= endCellIndex ; cellIndex ++ ) {
139
142
cell = cells [ cellIndex ] ;
140
143
availableNode = null ;
141
144
142
145
// find the first one that's available
143
- if ( ! initialLoad ) {
144
- const existingNode = nodes . find ( n => n . cell === cellIndex ) ;
145
- if ( existingNode ) {
146
- if ( existingNode . view . context . $implicit === records [ existingNode . cell ] ) continue ; // optimization: node data is the same no need to update
147
- availableNode = existingNode ; // update existing node
148
- } else {
149
- for ( var i = 0 ; i < totalNodes ; i ++ ) {
150
- node = nodes [ i ] ;
151
-
152
- if ( cell . tmpl !== node . tmpl || i === 0 && cellIndex !== 0 ) {
153
- // the cell must use the correct template
154
- // first node can only be used by the first cell (css :first-child reasons)
155
- // this node is never available to be reused
156
- continue ;
157
- }
146
+ // if (!initialLoad) {
147
+ const existingNode = nodes . find ( n => n . cell === cellIndex && n . tmpl === cell . tmpl ) ;
148
+ if ( existingNode ) {
149
+ console . debug ( 'virtual-util' , 'found that cell is already rendered in existingNode' , existingNode ) ;
150
+ if ( existingNode . view . context . $implicit === records [ cell . record ] ) {
151
+ usedNodes . push ( existingNode ) ;
152
+ continue ; // optimization: node data is the same no need to update
153
+ }
154
+ console . debug ( 'virtual-util' , 'data is not the same, will need to update node. Setting as available node' ) ;
155
+ availableNode = existingNode ; // update existing node
156
+ } else {
157
+ console . debug ( 'virtual-util' , 'cell was not rendered in current nodes, will now look for an available node' ) ;
158
+ for ( var i = 0 ; i < totalNodes ; i ++ ) {
159
+ const node = nodes [ i ] ;
160
+
161
+ if ( cell . tmpl !== node . tmpl || i === 0 && cellIndex !== 0 ) {
162
+ // the cell must use the correct template
163
+ // first node can only be used by the first cell (css :first-child reasons)
164
+ // this node is never available to be reused
165
+ continue ;
166
+ }
158
167
159
- if ( node . cell < startCellIndex || node . cell > endCellIndex ) {
160
-
161
- if ( ! availableNode ) {
162
- // havent gotten an available node yet
163
- availableNode = nodes [ i ] ;
164
- break ;
165
- } else if ( scrollingDown ) {
166
- // scrolling down
167
- if ( node . cell < availableNode . cell ) {
168
- availableNode = nodes [ i ] ;
169
- break ;
170
- }
171
-
172
- } else {
173
- // scrolling up
174
- if ( node . cell > availableNode . cell ) {
175
- availableNode = nodes [ i ] ;
176
- break ;
177
- }
168
+ if ( node . cell < startCellIndex || node . cell > endCellIndex ) {
169
+
170
+ if ( ! availableNode ) {
171
+ // havent gotten an available node yet
172
+ availableNode = node ;
173
+ console . debug ( 'virtual-util' , 'found node to reuse' , availableNode ) ;
174
+ } else if ( scrollingDown ) {
175
+ // scrolling down
176
+ if ( node . cell < availableNode . cell ) {
177
+ availableNode = node ;
178
+ console . debug ( 'virtual-util' , 'scrolling-down found better node to reuse' , availableNode ) ;
179
+ }
180
+ } else {
181
+ // scrolling up
182
+ if ( node . cell > availableNode . cell ) {
183
+ availableNode = node ;
184
+ console . debug ( 'virtual-util' , 'scrolling-up found better node to reuse' , availableNode ) ;
178
185
}
179
186
}
180
187
}
181
188
}
182
189
}
190
+ // }
183
191
184
192
if ( ! availableNode ) {
185
193
// did not find an available node to put the cell data into
186
194
// insert a new node after existing ones
187
195
if ( viewInsertIndex === null ) {
188
196
viewInsertIndex = - 1 ;
189
197
for ( var j = totalNodes - 1 ; j >= 0 ; j -- ) {
190
- node = nodes [ j ] ;
198
+ const node = nodes [ j ] ;
191
199
if ( node ) {
192
200
viewInsertIndex = viewContainer . indexOf ( node . view ) ;
193
201
break ;
@@ -212,6 +220,7 @@ export function populateNodeData(startCellIndex: number, endCellIndex: number, s
212
220
} ;
213
221
214
222
totalNodes = nodes . push ( availableNode ) ;
223
+ console . warn ( 'virtual-util' , '++++++++ did not find a node to reuse, created a new one' , availableNode ) ;
215
224
}
216
225
217
226
// assign who's the new cell index for this node
@@ -225,8 +234,18 @@ export function populateNodeData(startCellIndex: number, endCellIndex: number, s
225
234
availableNode . hasChanges = true ;
226
235
availableNode . lastTransform = null ;
227
236
hasChanges = true ;
237
+ usedNodes . push ( availableNode ) ;
228
238
}
229
239
240
+ const unusedNodes = nodes . filter ( n => usedNodes . indexOf ( n ) < 0 ) ;
241
+ unusedNodes . forEach ( node => {
242
+ const index = viewContainer . indexOf ( node . view ) ;
243
+ viewContainer . remove ( index ) ;
244
+ const removeIndex = nodes . findIndex ( n => n === node ) ;
245
+ nodes . splice ( removeIndex , 1 ) ;
246
+ console . warn ( 'virtual-util' , '-------- found orphan node, removing' , node ) ;
247
+ } ) ;
248
+
230
249
return hasChanges ;
231
250
}
232
251
0 commit comments