Lint and format src/runtime/internal/keyed_each.js

pull/8569/head
S. Elliott Johnson 3 years ago
parent 4dbfda1463
commit 3d446c53cb

@ -2,124 +2,126 @@ import { transition_in, transition_out } from './transitions';
import { run_all } from './utils'; import { run_all } from './utils';
/** @returns {void} */ /** @returns {void} */
export function destroy_block(block, lookup) { export function destroy_block(block, lookup) {
block.d(1); block.d(1);
lookup.delete(block.key); lookup.delete(block.key);
} }
/** @returns {void} */ /** @returns {void} */
export function outro_and_destroy_block(block, lookup) { export function outro_and_destroy_block(block, lookup) {
transition_out(block, 1, 1, () => { transition_out(block, 1, 1, () => {
lookup.delete(block.key); lookup.delete(block.key);
}); });
} }
/** @returns {void} */ /** @returns {void} */
export function fix_and_destroy_block(block, lookup) { export function fix_and_destroy_block(block, lookup) {
block.f(); block.f();
destroy_block(block, lookup); destroy_block(block, lookup);
} }
/** @returns {void} */ /** @returns {void} */
export function fix_and_outro_and_destroy_block(block, lookup) { export function fix_and_outro_and_destroy_block(block, lookup) {
block.f(); block.f();
outro_and_destroy_block(block, lookup); outro_and_destroy_block(block, lookup);
} }
/** @returns {any[]} */ /** @returns {any[]} */
export function update_keyed_each(old_blocks, dirty, get_key, dynamic, ctx, list, lookup, node, destroy, create_each_block, next, get_context) { export function update_keyed_each(
let o = old_blocks.length; old_blocks,
let n = list.length; dirty,
let i = o; get_key,
const old_indexes = {}; dynamic,
while (i--) ctx,
old_indexes[old_blocks[i].key] = i; list,
const new_blocks = []; lookup,
const new_lookup = new Map(); node,
const deltas = new Map(); destroy,
const updates = []; create_each_block,
i = n; next,
while (i--) { get_context
const child_ctx = get_context(ctx, list, i); ) {
const key = get_key(child_ctx); let o = old_blocks.length;
let block = lookup.get(key); let n = list.length;
if (!block) { let i = o;
block = create_each_block(key, child_ctx); const old_indexes = {};
block.c(); while (i--) old_indexes[old_blocks[i].key] = i;
} const new_blocks = [];
else if (dynamic) { const new_lookup = new Map();
// defer updates until all the DOM shuffling is done const deltas = new Map();
updates.push(() => block.p(child_ctx, dirty)); const updates = [];
} i = n;
new_lookup.set(key, (new_blocks[i] = block)); while (i--) {
if (key in old_indexes) const child_ctx = get_context(ctx, list, i);
deltas.set(key, Math.abs(i - old_indexes[key])); const key = get_key(child_ctx);
} let block = lookup.get(key);
const will_move = new Set(); if (!block) {
const did_move = new Set(); block = create_each_block(key, child_ctx);
/** @returns {void} */ block.c();
function insert(block) { } else if (dynamic) {
transition_in(block, 1); // defer updates until all the DOM shuffling is done
block.m(node, next); updates.push(() => block.p(child_ctx, dirty));
lookup.set(block.key, block); }
next = block.first; new_lookup.set(key, (new_blocks[i] = block));
n--; if (key in old_indexes) deltas.set(key, Math.abs(i - old_indexes[key]));
} }
while (o && n) { const will_move = new Set();
const new_block = new_blocks[n - 1]; const did_move = new Set();
const old_block = old_blocks[o - 1]; /** @returns {void} */
const new_key = new_block.key; function insert(block) {
const old_key = old_block.key; transition_in(block, 1);
if (new_block === old_block) { block.m(node, next);
// do nothing lookup.set(block.key, block);
next = new_block.first; next = block.first;
o--; n--;
n--; }
} while (o && n) {
else if (!new_lookup.has(old_key)) { const new_block = new_blocks[n - 1];
// remove old block const old_block = old_blocks[o - 1];
destroy(old_block, lookup); const new_key = new_block.key;
o--; const old_key = old_block.key;
} if (new_block === old_block) {
else if (!lookup.has(new_key) || will_move.has(new_key)) { // do nothing
insert(new_block); next = new_block.first;
} o--;
else if (did_move.has(old_key)) { n--;
o--; } else if (!new_lookup.has(old_key)) {
} // remove old block
else if (deltas.get(new_key) > deltas.get(old_key)) { destroy(old_block, lookup);
did_move.add(new_key); o--;
insert(new_block); } else if (!lookup.has(new_key) || will_move.has(new_key)) {
} insert(new_block);
else { } else if (did_move.has(old_key)) {
will_move.add(old_key); o--;
o--; } else if (deltas.get(new_key) > deltas.get(old_key)) {
} did_move.add(new_key);
} insert(new_block);
while (o--) { } else {
const old_block = old_blocks[o]; will_move.add(old_key);
if (!new_lookup.has(old_block.key)) o--;
destroy(old_block, lookup); }
} }
while (n) while (o--) {
insert(new_blocks[n - 1]); const old_block = old_blocks[o];
run_all(updates); if (!new_lookup.has(old_block.key)) destroy(old_block, lookup);
return new_blocks; }
while (n) insert(new_blocks[n - 1]);
run_all(updates);
return new_blocks;
} }
/** @returns {void} */ /** @returns {void} */
export function validate_each_keys(ctx, list, get_context, get_key) { export function validate_each_keys(ctx, list, get_context, get_key) {
const keys = new Map(); const keys = new Map();
for (let i = 0; i < list.length; i++) { for (let i = 0; i < list.length; i++) {
const key = get_key(get_context(ctx, list, i)); const key = get_key(get_context(ctx, list, i));
if (keys.has(key)) { if (keys.has(key)) {
let value = ''; let value = '';
try { try {
value = `with value '${String(key)}' `; value = `with value '${String(key)}' `;
} } catch (e) {
catch (e) { // can't stringify
// can't stringify }
} throw new Error(
throw new Error(`Cannot have duplicate keys in a keyed each: Keys at index ${keys.get(key)} and ${i} ${value}are duplicates`); `Cannot have duplicate keys in a keyed each: Keys at index ${keys.get(
} key
keys.set(key, i); )} and ${i} ${value}are duplicates`
} );
}
keys.set(key, i);
}
} }

Loading…
Cancel
Save