blockless
Rich Harris 2 years ago
parent e6b8022187
commit 6b664a7419

@ -1,4 +1,3 @@
export const SOURCE = 1;
export const DERIVED = 1 << 1; export const DERIVED = 1 << 1;
export const EFFECT = 1 << 2; export const EFFECT = 1 << 2;
export const PRE_EFFECT = 1 << 3; export const PRE_EFFECT = 1 << 3;

@ -0,0 +1,21 @@
/**
* @param {((value?: number) => number)} fn
* @param {1 | -1} [d]
* @returns {number}
*/
export function update_prop(fn, d = 1) {
const value = fn();
fn(value + d);
return value;
}
/**
* @param {((value?: number) => number)} fn
* @param {1 | -1} [d]
* @returns {number}
*/
export function update_pre_prop(fn, d = 1) {
const value = fn() + d;
fn(value);
return value;
}

@ -20,7 +20,7 @@ import {
untrack untrack
} from '../runtime.js'; } from '../runtime.js';
import { default_equals, safe_equal } from './equality.js'; import { default_equals, safe_equal } from './equality.js';
import { CLEAN, DERIVED, DIRTY, MANAGED, SOURCE } from '../constants.js'; import { CLEAN, DERIVED, DIRTY, MANAGED } from '../constants.js';
/** /**
* @template V * @template V
@ -31,7 +31,7 @@ import { CLEAN, DERIVED, DIRTY, MANAGED, SOURCE } from '../constants.js';
export function source(value) { export function source(value) {
/** @type {import('#client').Source<V>} */ /** @type {import('#client').Source<V>} */
const source = { const source = {
f: SOURCE | CLEAN, f: CLEAN,
v: value, v: value,
eq: default_equals, eq: default_equals,
consumers: null, consumers: null,
@ -160,3 +160,25 @@ export function mutate(source, value) {
); );
return value; return value;
} }
/**
* @param {import('#client').ValueSignal<number>} signal
* @param {1 | -1} [d]
* @returns {number}
*/
export function update(signal, d = 1) {
const value = get(signal);
set(signal, value + d);
return value;
}
/**
* @param {import('#client').ValueSignal<number>} signal
* @param {1 | -1} [d]
* @returns {number}
*/
export function update_pre(signal, d = 1) {
const value = get(signal) + d;
set(signal, value);
return value;
}

@ -1,13 +1,6 @@
import type { DERIVED, EFFECT, PRE_EFFECT, RENDER_EFFECT, SOURCE } from '../constants'; import type { DERIVED, EFFECT, PRE_EFFECT, RENDER_EFFECT } from '../constants';
import type { ComponentContext, EqualsFunctions, TemplateNode, Transition } from '../types'; import type { ComponentContext, EqualsFunctions, TemplateNode, Transition } from '../types';
type SignalFlags =
| typeof SOURCE
| typeof DERIVED
| typeof EFFECT
| typeof PRE_EFFECT
| typeof RENDER_EFFECT;
export type EffectType = typeof EFFECT | typeof PRE_EFFECT | typeof RENDER_EFFECT; export type EffectType = typeof EFFECT | typeof PRE_EFFECT | typeof RENDER_EFFECT;
export interface Source<V = unknown> { export interface Source<V = unknown> {
@ -16,7 +9,7 @@ export interface Source<V = unknown> {
/** equals: For value equality */ /** equals: For value equality */
eq: EqualsFunctions; eq: EqualsFunctions;
/** flags: The types that the signal represent, as a bitwise value */ /** flags: The types that the signal represent, as a bitwise value */
f: SignalFlags; f: number;
/** value: The latest value for this signal */ /** value: The latest value for this signal */
v: V; v: V;
// write version // write version
@ -46,7 +39,7 @@ export interface Effect {
/** destroy: Thing(s) that need destroying */ /** destroy: Thing(s) that need destroying */
y: null | (() => void); y: null | (() => void);
/** The types that the signal represent, as a bitwise value */ /** The types that the signal represent, as a bitwise value */
f: SignalFlags; f: number;
/** init: The function that we invoke for effects and computeds */ /** init: The function that we invoke for effects and computeds */
fn: null | (() => void | (() => void)); fn: null | (() => void | (() => void));
/** deriveds belonging to this effect */ /** deriveds belonging to this effect */

@ -3,7 +3,6 @@ import {
array_prototype, array_prototype,
get_descriptors, get_descriptors,
get_prototype_of, get_prototype_of,
is_array,
is_frozen, is_frozen,
object_freeze, object_freeze,
object_prototype object_prototype
@ -23,12 +22,11 @@ import {
DESTROYED, DESTROYED,
INERT, INERT,
MANAGED, MANAGED,
SOURCE,
STATE_SYMBOL, STATE_SYMBOL,
BRANCH_EFFECT BRANCH_EFFECT
} from './constants.js'; } from './constants.js';
import { flush_tasks } from './dom/task.js'; import { flush_tasks } from './dom/task.js';
import { mutate, set } from './reactivity/sources.js'; import { mutate } from './reactivity/sources.js';
const IS_EFFECT = EFFECT | PRE_EFFECT | RENDER_EFFECT; const IS_EFFECT = EFFECT | PRE_EFFECT | RENDER_EFFECT;
@ -936,50 +934,6 @@ function get_parent_context(component_context) {
return null; return null;
} }
/**
* @param {import('#client').ValueSignal<number>} signal
* @param {1 | -1} [d]
* @returns {number}
*/
export function update(signal, d = 1) {
const value = get(signal);
set(signal, value + d);
return value;
}
/**
* @param {((value?: number) => number)} fn
* @param {1 | -1} [d]
* @returns {number}
*/
export function update_prop(fn, d = 1) {
const value = fn();
fn(value + d);
return value;
}
/**
* @param {import('#client').ValueSignal<number>} signal
* @param {1 | -1} [d]
* @returns {number}
*/
export function update_pre(signal, d = 1) {
const value = get(signal) + d;
set(signal, value);
return value;
}
/**
* @param {((value?: number) => number)} fn
* @param {1 | -1} [d]
* @returns {number}
*/
export function update_pre_prop(fn, d = 1) {
const value = fn() + d;
fn(value);
return value;
}
/** /**
* @param {Record<string, unknown>} obj * @param {Record<string, unknown>} obj
* @param {string[]} keys * @param {string[]} keys

@ -4,10 +4,6 @@ export {
flushSync, flushSync,
tick, tick,
untrack, untrack,
update,
update_prop,
update_pre,
update_pre_prop,
value_or_fallback, value_or_fallback,
exclude_from_object, exclude_from_object,
pop, pop,
@ -29,8 +25,9 @@ export { element } from './client/dom/blocks/svelte-element.js';
export * from './client/dom/blocks/each.js'; export * from './client/dom/blocks/each.js';
export * from './client/reactivity/effects.js'; export * from './client/reactivity/effects.js';
export * from './client/reactivity/deriveds.js'; export * from './client/reactivity/deriveds.js';
export * from './client/reactivity/sources.js';
export * from './client/reactivity/equality.js'; export * from './client/reactivity/equality.js';
export * from './client/reactivity/props.js';
export * from './client/reactivity/sources.js';
export * from './client/reactivity/store.js'; export * from './client/reactivity/store.js';
export * from './client/render.js'; export * from './client/render.js';
export * from './client/validate.js'; export * from './client/validate.js';

Loading…
Cancel
Save