chore: DRY out increment logic (#16332)

pull/16336/head
Rich Harris 2 months ago committed by GitHub
parent c3361bfdb7
commit 0cafe34c92
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -8,7 +8,7 @@ import {
is_array, is_array,
object_prototype object_prototype
} from '../shared/utils.js'; } from '../shared/utils.js';
import { state as source, set } from './reactivity/sources.js'; import { state as source, set, increment } from './reactivity/sources.js';
import { PROXY_PATH_SYMBOL, STATE_SYMBOL } from '#client/constants'; import { PROXY_PATH_SYMBOL, STATE_SYMBOL } from '#client/constants';
import { UNINITIALIZED } from '../../constants.js'; import { UNINITIALIZED } from '../../constants.js';
import * as e from './errors.js'; import * as e from './errors.js';
@ -118,7 +118,7 @@ export function proxy(value) {
if (prop in target) { if (prop in target) {
const s = with_parent(() => source(UNINITIALIZED, stack)); const s = with_parent(() => source(UNINITIALIZED, stack));
sources.set(prop, s); sources.set(prop, s);
update_version(version); increment(version);
if (DEV) { if (DEV) {
tag(s, get_label(path, prop)); tag(s, get_label(path, prop));
@ -136,7 +136,7 @@ export function proxy(value) {
} }
} }
set(s, UNINITIALIZED); set(s, UNINITIALIZED);
update_version(version); increment(version);
} }
return true; return true;
@ -304,7 +304,7 @@ export function proxy(value) {
} }
} }
update_version(version); increment(version);
} }
return true; return true;
@ -343,14 +343,6 @@ function get_label(path, prop) {
return /^\d+$/.test(prop) ? `${path}[${prop}]` : `${path}['${prop}']`; return /^\d+$/.test(prop) ? `${path}[${prop}]` : `${path}['${prop}']`;
} }
/**
* @param {Source<number>} signal
* @param {1 | -1} [d]
*/
function update_version(signal, d = 1) {
set(signal, signal.v + d);
}
/** /**
* @param {any} value * @param {any} value
*/ */

@ -259,6 +259,14 @@ export function update_pre(source, d = 1) {
return set(source, d === 1 ? ++value : --value); return set(source, d === 1 ? ++value : --value);
} }
/**
* Silently (without using `get`) increment a source
* @param {Source<number>} source
*/
export function increment(source) {
set(source, source.v + 1);
}
/** /**
* @param {Value} signal * @param {Value} signal
* @param {number} status should be DIRTY or MAYBE_DIRTY * @param {number} status should be DIRTY or MAYBE_DIRTY

@ -1,8 +1,7 @@
import { get, tick, untrack } from '../internal/client/runtime.js'; import { get, tick, untrack } from '../internal/client/runtime.js';
import { effect_tracking, render_effect } from '../internal/client/reactivity/effects.js'; import { effect_tracking, render_effect } from '../internal/client/reactivity/effects.js';
import { source } from '../internal/client/reactivity/sources.js'; import { source, increment } from '../internal/client/reactivity/sources.js';
import { tag } from '../internal/client/dev/tracing.js'; import { tag } from '../internal/client/dev/tracing.js';
import { increment } from './utils.js';
import { DEV } from 'esm-env'; import { DEV } from 'esm-env';
/** /**

@ -1,9 +1,8 @@
/** @import { Source } from '#client' */ /** @import { Source } from '#client' */
import { DEV } from 'esm-env'; import { DEV } from 'esm-env';
import { set, source, state } from '../internal/client/reactivity/sources.js'; import { set, source, state, increment } from '../internal/client/reactivity/sources.js';
import { label, tag } from '../internal/client/dev/tracing.js'; import { label, tag } from '../internal/client/dev/tracing.js';
import { get, update_version } from '../internal/client/runtime.js'; import { get, update_version } from '../internal/client/runtime.js';
import { increment } from './utils.js';
/** /**
* A reactive version of the built-in [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) object. * A reactive version of the built-in [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) object.

@ -1,9 +1,8 @@
/** @import { Source } from '#client' */ /** @import { Source } from '#client' */
import { DEV } from 'esm-env'; import { DEV } from 'esm-env';
import { source, set, state } from '../internal/client/reactivity/sources.js'; import { source, set, state, increment } from '../internal/client/reactivity/sources.js';
import { label, tag } from '../internal/client/dev/tracing.js'; import { label, tag } from '../internal/client/dev/tracing.js';
import { get, update_version } from '../internal/client/runtime.js'; import { get, update_version } from '../internal/client/runtime.js';
import { increment } from './utils.js';
var read_methods = ['forEach', 'isDisjointFrom', 'isSubsetOf', 'isSupersetOf']; var read_methods = ['forEach', 'isDisjointFrom', 'isSubsetOf', 'isSupersetOf'];
var set_like_methods = ['difference', 'intersection', 'symmetricDifference', 'union']; var set_like_methods = ['difference', 'intersection', 'symmetricDifference', 'union'];

@ -1,9 +1,8 @@
import { DEV } from 'esm-env'; import { DEV } from 'esm-env';
import { state } from '../internal/client/reactivity/sources.js'; import { state, increment } from '../internal/client/reactivity/sources.js';
import { tag } from '../internal/client/dev/tracing.js'; import { tag } from '../internal/client/dev/tracing.js';
import { get } from '../internal/client/runtime.js'; import { get } from '../internal/client/runtime.js';
import { get_current_url } from './url.js'; import { get_current_url } from './url.js';
import { increment } from './utils.js';
export const REPLACE = Symbol(); export const REPLACE = Symbol();

@ -1,7 +0,0 @@
/** @import { Source } from '#client' */
import { set } from '../internal/client/reactivity/sources.js';
/** @param {Source<number>} source */
export function increment(source) {
set(source, source.v + 1);
}
Loading…
Cancel
Save