mirror of https://github.com/sveltejs/svelte
Merge pull request #1274 from sveltejs/fix-perf-regression
A new list diffing algorithmpull/1279/head
commit
89c0864c81
@ -1,80 +1,99 @@
|
|||||||
import { assign } from './utils.js';
|
export function destroyBlock(block, lookup) {
|
||||||
|
block.u();
|
||||||
export function destroyIteration(iteration, lookup) {
|
block.d();
|
||||||
var first = iteration.first
|
lookup[block.key] = null;
|
||||||
if (first && first.parentNode) {
|
|
||||||
iteration.u();
|
|
||||||
}
|
|
||||||
iteration.d();
|
|
||||||
lookup[iteration.key] = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function outroAndDestroyIteration(iteration, lookup) {
|
export function outroAndDestroyBlock(block, lookup) {
|
||||||
iteration.o(function() {
|
block.o(function() {
|
||||||
iteration.u();
|
destroyBlock(block, lookup);
|
||||||
iteration.d();
|
|
||||||
lookup[iteration.key] = null;
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO is it possible to avoid mounting iterations that are
|
export function updateKeyedEach(old_blocks, component, changed, key_prop, dynamic, list, lookup, node, has_outro, create_each_block, intro_method, get_context) {
|
||||||
// already in the right place?
|
var o = old_blocks.length;
|
||||||
export function updateKeyedEach(component, key, changed, key_prop, dynamic, list, head, lookup, node, has_outro, create_each_block, intro_method, get_context) {
|
var n = list.length;
|
||||||
var keep = {};
|
|
||||||
|
var i = o;
|
||||||
|
var old_indexes = {};
|
||||||
|
while (i--) old_indexes[old_blocks[i].key] = i;
|
||||||
|
|
||||||
var i = list.length;
|
var new_blocks = [];
|
||||||
|
var new_lookup = {};
|
||||||
|
var deltas = {};
|
||||||
|
|
||||||
|
var i = n;
|
||||||
while (i--) {
|
while (i--) {
|
||||||
var key = list[i][key_prop];
|
var key = list[i][key_prop];
|
||||||
var iteration = lookup[key];
|
var block = lookup[key];
|
||||||
|
|
||||||
if (iteration) {
|
if (!block) {
|
||||||
if (dynamic) iteration.p(changed, get_context(i));
|
block = create_each_block(component, key, get_context(i));
|
||||||
} else {
|
block.c();
|
||||||
iteration = lookup[key] = create_each_block(component, key, get_context(i));
|
} else if (dynamic) {
|
||||||
iteration.c();
|
block.p(changed, get_context(i));
|
||||||
}
|
}
|
||||||
|
|
||||||
lookup[key] = iteration;
|
new_blocks[i] = new_lookup[key] = block;
|
||||||
keep[key] = 1;
|
|
||||||
|
if (key in old_indexes) deltas[key] = Math.abs(i - old_indexes[key]);
|
||||||
}
|
}
|
||||||
|
|
||||||
var destroy = has_outro
|
var next = null;
|
||||||
? outroAndDestroyIteration
|
|
||||||
: destroyIteration;
|
var will_move = {};
|
||||||
|
var did_move = {};
|
||||||
|
|
||||||
iteration = head;
|
var destroy = has_outro ? outroAndDestroyBlock : destroyBlock;
|
||||||
while (iteration) {
|
|
||||||
if (!keep[iteration.key]) destroy(iteration, lookup);
|
function insert(block) {
|
||||||
iteration = iteration.next;
|
block[intro_method](node, next && next.first);
|
||||||
|
next = lookup[block.key] = block;
|
||||||
|
n--;
|
||||||
}
|
}
|
||||||
|
|
||||||
var next = null;
|
while (o && n) {
|
||||||
|
var new_block = new_blocks[n - 1];
|
||||||
|
var old_block = old_blocks[o - 1];
|
||||||
|
var new_key = new_block.key;
|
||||||
|
var old_key = old_block.key;
|
||||||
|
|
||||||
i = list.length;
|
if (new_block === old_block) {
|
||||||
while (i--) {
|
// do nothing
|
||||||
key = list[i][key_prop];
|
next = new_block;
|
||||||
iteration = lookup[key];
|
o--;
|
||||||
|
n--;
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (!new_lookup[old_key]) {
|
||||||
|
// remove old block
|
||||||
|
destroy(old_block, lookup);
|
||||||
|
o--;
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (!lookup[new_key] || will_move[new_key]) {
|
||||||
|
insert(new_block);
|
||||||
|
}
|
||||||
|
|
||||||
var anchor;
|
else if (did_move[old_key]) {
|
||||||
|
o--;
|
||||||
|
|
||||||
if (has_outro) {
|
} else if (deltas[new_key] > deltas[old_key]) {
|
||||||
var next_key = next && next.key;
|
did_move[new_key] = true;
|
||||||
var neighbour = iteration.next;
|
insert(new_block);
|
||||||
var anchor_key;
|
|
||||||
|
|
||||||
while (neighbour && anchor_key != next_key && !keep[anchor_key]) {
|
|
||||||
anchor = neighbour && neighbour.first;
|
|
||||||
neighbour = neighbour.next;
|
|
||||||
anchor_key = neighbour && neighbour.key;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
anchor = next && next.first;
|
will_move[old_key] = true;
|
||||||
|
o--;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
iteration[intro_method](node, anchor);
|
while (o--) {
|
||||||
|
var old_block = old_blocks[o];
|
||||||
iteration.next = next;
|
if (!new_lookup[old_block.key]) destroy(old_block, lookup);
|
||||||
if (next) next.last = iteration;
|
|
||||||
next = iteration;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
while (n) insert(new_blocks[n - 1]);
|
||||||
|
|
||||||
|
return new_blocks;
|
||||||
}
|
}
|
Loading…
Reference in new issue