state-onchange
Rich Harris 5 months ago
parent d5f785bd7e
commit d8e60f0140

@ -39,8 +39,11 @@ export function proxy(value, onchange) {
} }
if (STATE_SYMBOL in value) { if (STATE_SYMBOL in value) {
// @ts-ignore if (onchange) {
value[PROXY_ONCHANGE_SYMBOL](onchange); // @ts-ignore
value[PROXY_ONCHANGE_SYMBOL](onchange);
}
return value; return value;
} }
@ -144,7 +147,7 @@ export function proxy(value, onchange) {
// when we delete a property if the source is a proxy we remove the current onchange from // when we delete a property if the source is a proxy we remove the current onchange from
// the proxy `onchanges` so that it doesn't trigger it anymore // the proxy `onchanges` so that it doesn't trigger it anymore
if (typeof s.v === 'object' && s.v !== null && STATE_SYMBOL in s.v) { if (onchange && typeof s.v === 'object' && s.v !== null && STATE_SYMBOL in s.v) {
s.v[PROXY_ONCHANGE_SYMBOL](onchange, true); s.v[PROXY_ONCHANGE_SYMBOL](onchange, true);
} }
@ -161,24 +164,15 @@ export function proxy(value, onchange) {
} }
if (prop === PROXY_ONCHANGE_SYMBOL) { if (prop === PROXY_ONCHANGE_SYMBOL) {
return ( return (/** @type {(() => unknown)} */ value, /** @type {boolean} */ remove) => {
/** @type {(() => unknown) | undefined} */ value,
/** @type {boolean} */ remove
) => {
// we either add or remove the passed in value // we either add or remove the passed in value
// to the onchanges array or we set every source onchange // to the onchanges array or we set every source onchange
// to the passed in value (if it's undefined it will make the chain stop) // to the passed in value (if it's undefined it will make the chain stop)
if (onchange != null && value) { // if (onchange != null && value) {
if (remove) { if (remove) {
onchanges?.delete(value); onchanges?.delete(value);
} else {
onchanges?.add(value);
}
} else { } else {
onchange = value; onchanges?.add(value);
for (let [, s] of sources) {
s.o = value;
}
} }
}; };
} }
@ -277,6 +271,7 @@ export function proxy(value, onchange) {
var other_s = sources.get(i + ''); var other_s = sources.get(i + '');
if (other_s !== undefined) { if (other_s !== undefined) {
if ( if (
onchange &&
typeof other_s.v === 'object' && typeof other_s.v === 'object' &&
other_s.v !== null && other_s.v !== null &&
STATE_SYMBOL in other_s.v STATE_SYMBOL in other_s.v
@ -309,11 +304,13 @@ export function proxy(value, onchange) {
} }
} else { } else {
has = s.v !== UNINITIALIZED; has = s.v !== UNINITIALIZED;
// when we set a property if the source is a proxy we remove the current onchange from // when we set a property if the source is a proxy we remove the current onchange from
// the proxy `onchanges` so that it doesn't trigger it anymore // the proxy `onchanges` so that it doesn't trigger it anymore
if (typeof s.v === 'object' && s.v !== null && STATE_SYMBOL in s.v) { if (onchange && typeof s.v === 'object' && s.v !== null && STATE_SYMBOL in s.v) {
s.v[PROXY_ONCHANGE_SYMBOL](onchange, true); s.v[PROXY_ONCHANGE_SYMBOL](onchange, true);
} }
set( set(
s, s,
with_parent(() => proxy(value, onchange)) with_parent(() => proxy(value, onchange))

Loading…
Cancel
Save