don't think we need this any more

proxied-state-each-blocks
Rich Harris 2 years ago
parent 823a6c00e6
commit 826090ecfb

@ -15,7 +15,6 @@ import {
set_current_hydration_fragment
} from './hydration.js';
import { clear_text_content, map_get, map_set } from './operations.js';
import { STATE_SYMBOL } from './proxy.js';
import { insert, remove } from './reconciler.js';
import { empty } from './render.js';
import {
@ -274,15 +273,9 @@ function reconcile_indexed_array(
flags,
apply_transitions
) {
var is_proxied_array = STATE_SYMBOL in array;
var a_blocks = each_block.v;
var active_transitions = each_block.s;
if (is_proxied_array) {
flags &= ~EACH_ITEM_REACTIVE;
flags |= EACH_IS_PROXIED;
}
/** @type {number | void} */
var a = a_blocks.length;
@ -345,7 +338,7 @@ function reconcile_indexed_array(
item = array[index];
block = a_blocks[index];
b_blocks[index] = block;
update_each_item_block(block, item, index, flags);
update_each_item_block(block, index, flags);
}
}
}
@ -385,14 +378,8 @@ function reconcile_tracked_array(
) {
var a_blocks = each_block.v;
const is_computed_key = keys !== null;
var is_proxied_array = STATE_SYMBOL in array;
var active_transitions = each_block.s;
if (is_proxied_array) {
flags &= ~EACH_ITEM_REACTIVE;
flags |= EACH_IS_PROXIED;
}
/** @type {number | void} */
var a = a_blocks.length;
@ -469,7 +456,7 @@ function reconcile_tracked_array(
while (a_blocks[a_end].k === key) {
block = a_blocks[a_end--];
item = array[b_end];
update_each_item_block(block, item, b_end, flags);
update_each_item_block(block, b_end, flags);
sibling = get_first_child(block);
b_blocks[b_end] = block;
if (start > --b_end || start > a_end) {
@ -483,7 +470,7 @@ function reconcile_tracked_array(
while (start <= a_end && start <= b_end && a_blocks[start].k === key) {
item = array[start];
block = a_blocks[start];
update_each_item_block(block, item, start, flags);
update_each_item_block(block, start, flags);
b_blocks[start] = block;
++start;
key = is_computed_key ? keys[start] : array[start];
@ -544,7 +531,7 @@ function reconcile_tracked_array(
a = sources[i];
if (pos === MOVED_BLOCK && a !== LIS_BLOCK) {
block = b_blocks[b_end];
update_each_item_block(block, item, b_end, flags);
update_each_item_block(block, b_end, flags);
}
}
}
@ -561,7 +548,7 @@ function reconcile_tracked_array(
} else {
block = b_blocks[b_end];
if (!is_animated) {
update_each_item_block(block, item, b_end, flags);
update_each_item_block(block, b_end, flags);
}
}
if (should_create || (pos === MOVED_BLOCK && a !== LIS_BLOCK)) {
@ -732,23 +719,21 @@ function get_first_element(block) {
/**
* @param {import('./types.js').EachItemBlock} block
* @param {any} item
* @param {number} index
* @param {number} type
* @returns {void}
*/
function update_each_item_block(block, item, index, type) {
if ((type & EACH_ITEM_REACTIVE) !== 0) {
set_signal_value(block.v, item);
}
function update_each_item_block(block, index, type) {
const transitions = block.s;
const index_is_reactive = (type & EACH_INDEX_REACTIVE) !== 0;
// Handle each item animations
if (transitions !== null && (type & EACH_KEYED) !== 0) {
let prev_index = block.i;
if (index_is_reactive) {
prev_index = /** @type {import('./types.js').Signal<number>} */ (prev_index).v;
}
const items = block.p.v;
if (prev_index !== index && /** @type {number} */ (index) < items.length) {
const from_dom = /** @type {Element} */ (get_first_element(block));

@ -1,7 +1,7 @@
import { effect_active, get, set, increment, source, updating_derived } from './runtime.js';
import { get_descriptor, is_array } from './utils.js';
/** @typedef {{ p: StateObject | null; s: Map<string | symbol, import('./types.js').SourceSignal<any>>; v: import('./types.js').SourceSignal<number>; a: boolean }} Metadata */
/** @typedef {{ s: Map<string | symbol, import('./types.js').SourceSignal<any>>; v: import('./types.js').SourceSignal<number>; a: boolean }} Metadata */
/** @typedef {Record<string | symbol, any> & { [STATE_SYMBOL]: Metadata }} StateObject */
export const STATE_SYMBOL = Symbol();
@ -18,24 +18,13 @@ const is_frozen = Object.isFrozen;
* @returns {T}
*/
export function proxy(value) {
return wrap(value, null);
}
/**
* @template {StateObject} T
* @template {StateObject} P
* @param {T} value
* @param {P | null} parent
* @returns {T}
*/
function wrap(value, parent) {
if (typeof value === 'object' && value != null && !is_frozen(value) && !(STATE_SYMBOL in value)) {
const prototype = get_prototype_of(value);
// TODO handle Map and Set as well
if (prototype === object_prototype || prototype === array_prototype) {
// @ts-expect-error
value[STATE_SYMBOL] = init(value, parent);
value[STATE_SYMBOL] = init(value);
// @ts-expect-error not sure how to fix this
return new Proxy(value, handler);
@ -47,12 +36,10 @@ function wrap(value, parent) {
/**
* @param {StateObject} value
* @param {StateObject | null} parent
* @returns {Metadata}
*/
function init(value, parent) {
function init(value) {
return {
p: parent,
s: new Map(),
v: source(0),
a: is_array(value)
@ -76,7 +63,7 @@ const handler = {
(effect_active() || updating_derived) &&
(!(prop in target) || get_descriptor(target, prop)?.writable)
) {
s = source(wrap(target[prop], receiver));
s = source(proxy(target[prop]));
metadata.s.set(prop, s);
}
@ -86,7 +73,7 @@ const handler = {
const metadata = target[STATE_SYMBOL];
const s = metadata.s.get(prop);
if (s !== undefined) set(s, wrap(value, target));
if (s !== undefined) set(s, proxy(value));
if (metadata.a && prop === 'length') {
for (let i = value; i < target.length; i += 1) {

Loading…
Cancel
Save