whitespace is free

proxied-state-each-blocks
Rich Harris 2 years ago
parent 233f88f32d
commit 0acdeccb8b

@ -83,6 +83,7 @@ function each(anchor_node, collection, flags, key_fn, render_fn, fallback_fn, re
} }
}); });
}; };
const create_fallback_effect = () => { const create_fallback_effect = () => {
/** @type {import('./types.js').Render} */ /** @type {import('./types.js').Render} */
const fallback = { const fallback = {
@ -115,6 +116,7 @@ function each(anchor_node, collection, flags, key_fn, render_fn, fallback_fn, re
fallback.e = effect; fallback.e = effect;
current_fallback = fallback; current_fallback = fallback;
}; };
const each = render_effect( const each = render_effect(
() => { () => {
/** @type {V[]} */ /** @type {V[]} */
@ -153,6 +155,7 @@ function each(anchor_node, collection, flags, key_fn, render_fn, fallback_fn, re
block, block,
false false
); );
render = render_effect( render = render_effect(
/** @param {import('./types.js').EachBlock} block */ /** @param {import('./types.js').EachBlock} block */
(block) => { (block) => {
@ -164,6 +167,7 @@ function each(anchor_node, collection, flags, key_fn, render_fn, fallback_fn, re
block, block,
true true
); );
push_destroy_fn(each, () => { push_destroy_fn(each, () => {
const flags = block.f; const flags = block.f;
const anchor_node = block.a; const anchor_node = block.a;
@ -184,6 +188,7 @@ function each(anchor_node, collection, flags, key_fn, render_fn, fallback_fn, re
reconcile_fn([], block, anchor_node, is_controlled, render_fn, flags, false, keys); reconcile_fn([], block, anchor_node, is_controlled, render_fn, flags, false, keys);
destroy_signal(/** @type {import('./types.js').EffectSignal} */ (render)); destroy_signal(/** @type {import('./types.js').EffectSignal} */ (render));
}); });
block.e = each; block.e = each;
} }
@ -254,9 +259,11 @@ function reconcile_indexed_array(
/** @type {Array<import('./types.js').EachItemBlock>} */ /** @type {Array<import('./types.js').EachItemBlock>} */
var b_blocks; var b_blocks;
var block; var block;
if (active_transitions.length !== 0) { if (active_transitions.length !== 0) {
destroy_active_transition_blocks(active_transitions); destroy_active_transition_blocks(active_transitions);
} }
if (b === 0) { if (b === 0) {
b_blocks = []; b_blocks = [];
// Remove old blocks // Remove old blocks
@ -308,6 +315,7 @@ function reconcile_indexed_array(
} }
} }
} }
each_block.v = b_blocks; each_block.v = b_blocks;
} }
// Reconcile arrays by the equality of the elements in the array. This algorithm // Reconcile arrays by the equality of the elements in the array. This algorithm
@ -357,9 +365,11 @@ function reconcile_tracked_array(
/** @type {Array<import('./types.js').EachItemBlock>} */ /** @type {Array<import('./types.js').EachItemBlock>} */
var b_blocks; var b_blocks;
var block; var block;
if (active_transitions.length !== 0) { if (active_transitions.length !== 0) {
destroy_active_transition_blocks(active_transitions); destroy_active_transition_blocks(active_transitions);
} }
if (b === 0) { if (b === 0) {
b_blocks = []; b_blocks = [];
// Remove old blocks // Remove old blocks
@ -532,11 +542,12 @@ function reconcile_tracked_array(
} }
} }
} }
each_block.v = b_blocks; each_block.v = b_blocks;
} }
// Longest Increased Subsequence algorithm.
/** /**
* Longest Increased Subsequence algorithm
* @param {Int32Array} a * @param {Int32Array} a
* @returns {void} * @returns {void}
*/ */
@ -558,22 +569,28 @@ function mark_lis(a) {
/** @type {number} */ /** @type {number} */
var hi; var hi;
// Skip -1 values at the start of the input array `a`. // Skip -1 values at the start of the input array `a`.
for (; a[i] === NEW_BLOCK; ++i) { for (; a[i] === NEW_BLOCK; ++i) {
/**/ /**/
} }
index[0] = i++; index[0] = i++;
for (; i < length; ++i) { for (; i < length; ++i) {
k = a[i]; k = a[i];
if (k !== NEW_BLOCK) { if (k !== NEW_BLOCK) {
// Ignore -1 values. // Ignore -1 values.
j = index[index_length]; j = index[index_length];
if (a[j] < k) { if (a[j] < k) {
parent[i] = j; parent[i] = j;
index[++index_length] = i; index[++index_length] = i;
} else { } else {
lo = 0; lo = 0;
hi = index_length; hi = index_length;
while (lo < hi) { while (lo < hi) {
j = (lo + hi) >> 1; j = (lo + hi) >> 1;
if (a[index[j]] < k) { if (a[index[j]] < k) {
@ -582,6 +599,7 @@ function mark_lis(a) {
hi = j; hi = j;
} }
} }
if (k < a[index[lo]]) { if (k < a[index[lo]]) {
if (lo > 0) { if (lo > 0) {
parent[i] = index[lo - 1]; parent[i] = index[lo - 1];
@ -591,8 +609,10 @@ function mark_lis(a) {
} }
} }
} }
// Mutate input array `a` and assign -2 value to all nodes that are part of LIS. // Mutate input array `a` and assign -2 value to all nodes that are part of LIS.
j = index[index_length]; j = index[index_length];
while (index_length-- >= 0) { while (index_length-- >= 0) {
a[j] = LIS_BLOCK; a[j] = LIS_BLOCK;
j = parent[j]; j = parent[j];
@ -608,6 +628,7 @@ function mark_lis(a) {
*/ */
function insert_each_item_block(block, dom, is_controlled, sibling) { function insert_each_item_block(block, dom, is_controlled, sibling) {
var current = /** @type {import('./types.js').TemplateNode} */ (block.d); var current = /** @type {import('./types.js').TemplateNode} */ (block.d);
if (sibling === null) { if (sibling === null) {
if (is_controlled) { if (is_controlled) {
return insert(current, /** @type {Element} */ (dom), null); return insert(current, /** @type {Element} */ (dom), null);
@ -615,6 +636,7 @@ function insert_each_item_block(block, dom, is_controlled, sibling) {
return insert(current, /** @type {Element} */ (dom.parentNode), dom); return insert(current, /** @type {Element} */ (dom.parentNode), dom);
} }
} }
return insert(current, null, sibling); return insert(current, null, sibling);
} }
@ -624,9 +646,11 @@ function insert_each_item_block(block, dom, is_controlled, sibling) {
*/ */
function get_first_child(block) { function get_first_child(block) {
var current = block.d; var current = block.d;
if (is_array(current)) { if (is_array(current)) {
return /** @type {Text | Element | Comment} */ (current[0]); return /** @type {Text | Element | Comment} */ (current[0]);
} }
return /** @type {Text | Element | Comment} */ (current); return /** @type {Text | Element | Comment} */ (current);
} }
@ -636,10 +660,12 @@ function get_first_child(block) {
*/ */
function destroy_active_transition_blocks(active_transitions) { function destroy_active_transition_blocks(active_transitions) {
var length = active_transitions.length; var length = active_transitions.length;
if (length > 0) { if (length > 0) {
var i = 0; var i = 0;
var block; var block;
var transition; var transition;
for (; i < length; i++) { for (; i < length; i++) {
block = active_transitions[i]; block = active_transitions[i];
transition = block.r; transition = block.r;
@ -648,6 +674,7 @@ function destroy_active_transition_blocks(active_transitions) {
destroy_each_item_block(block, null, false); destroy_each_item_block(block, null, false);
} }
} }
active_transitions.length = 0; active_transitions.length = 0;
} }
} }
@ -658,6 +685,7 @@ function destroy_active_transition_blocks(active_transitions) {
*/ */
function get_first_element(block) { function get_first_element(block) {
const current = block.d; const current = block.d;
if (is_array(current)) { if (is_array(current)) {
for (let i = 0; i < current.length; i++) { for (let i = 0; i < current.length; i++) {
const node = current[i]; const node = current[i];
@ -666,6 +694,7 @@ function get_first_element(block) {
} }
} }
} }
return /** @type {Text | Element | Comment} */ (current); return /** @type {Text | Element | Comment} */ (current);
} }
@ -718,6 +747,7 @@ export function destroy_each_item_block(
controlled = false controlled = false
) { ) {
const transitions = block.s; const transitions = block.s;
if (apply_transitions && transitions !== null) { if (apply_transitions && transitions !== null) {
trigger_transitions(transitions, 'out'); trigger_transitions(transitions, 'out');
if (transition_block !== null) { if (transition_block !== null) {
@ -744,6 +774,7 @@ export function destroy_each_item_block(
*/ */
function each_item_block(array, item, key, index, render_fn, flags) { function each_item_block(array, item, key, index, render_fn, flags) {
const each_item_not_reactive = (flags & EACH_ITEM_REACTIVE) === 0; const each_item_not_reactive = (flags & EACH_ITEM_REACTIVE) === 0;
const item_value = const item_value =
(flags & EACH_IS_PROXIED) !== 0 && (flags & EACH_KEYED) === 0 (flags & EACH_IS_PROXIED) !== 0 && (flags & EACH_KEYED) === 0
? lazy_property(array, index) ? lazy_property(array, index)
@ -752,8 +783,10 @@ function each_item_block(array, item, key, index, render_fn, flags) {
: (flags & EACH_IS_IMMUTABLE) === 0 : (flags & EACH_IS_IMMUTABLE) === 0
? mutable_source(item) ? mutable_source(item)
: source(item); : source(item);
const index_value = (flags & EACH_INDEX_REACTIVE) === 0 ? index : source(index); const index_value = (flags & EACH_INDEX_REACTIVE) === 0 ? index : source(index);
const block = create_each_item_block(item_value, index_value, key); const block = create_each_item_block(item_value, index_value, key);
const effect = render_effect( const effect = render_effect(
/** @param {import('./types.js').EachItemBlock} block */ /** @param {import('./types.js').EachItemBlock} block */
(block) => { (block) => {
@ -762,6 +795,7 @@ function each_item_block(array, item, key, index, render_fn, flags) {
block, block,
true true
); );
block.e = effect; block.e = effect;
return block; return block;
} }

Loading…
Cancel
Save