chore: tidy up (#10705)

* tidy up

* tidy up

* tidy up

* tidy up

* remove redundant CLEAN

* lint

---------

Co-authored-by: Rich Harris <rich.harris@vercel.com>
pull/10718/head
Rich Harris 2 years ago committed by GitHub
parent b3d0a0695a
commit a5a566dfa7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -11,25 +11,27 @@ import { default_equals, safe_equal } from './equality.js';
*/ */
/*#__NO_SIDE_EFFECTS__*/ /*#__NO_SIDE_EFFECTS__*/
export function derived(fn) { export function derived(fn) {
const is_unowned = current_effect === null; let flags = DERIVED | CLEAN;
const flags = is_unowned ? DERIVED | UNOWNED : DERIVED; if (current_effect === null) flags |= UNOWNED;
const signal = /** @type {import('../types.js').Derived<V>} */ ({
/** @type {import('#client').Derived<V>} */
const signal = {
b: current_block, b: current_block,
c: null, c: null,
d: null, d: null,
e: default_equals, e: default_equals,
f: flags | CLEAN, f: flags,
i: fn, i: fn,
r: null, r: null,
// @ts-expect-error
v: UNINITIALIZED, v: UNINITIALIZED,
w: 0, w: 0,
x: null, x: null,
y: null y: null
}); };
if (DEV) { if (DEV) {
// @ts-expect-error /** @type {import('#client').DerivedDebug} */ (signal).inspect = new Set();
signal.inspect = new Set();
} }
if (current_consumer !== null) { if (current_consumer !== null) {
@ -42,7 +44,7 @@ export function derived(fn) {
/** /**
* @template V * @template V
* @param {() => V} fn * @param {() => V} fn
* @returns {import('../types.js').Derived<V>} * @returns {import('#client').Derived<V>}
*/ */
/*#__NO_SIDE_EFFECTS__*/ /*#__NO_SIDE_EFFECTS__*/
export function derived_safe_equal(fn) { export function derived_safe_equal(fn) {

@ -10,8 +10,8 @@ import {
import { DIRTY, MANAGED, RENDER_EFFECT, EFFECT, PRE_EFFECT } from '../constants.js'; import { DIRTY, MANAGED, RENDER_EFFECT, EFFECT, PRE_EFFECT } from '../constants.js';
/** /**
* @param {import('../types.js').Reaction} target_signal * @param {import('#client').Reaction} target_signal
* @param {import('../types.js').Reaction} ref_signal * @param {import('#client').Reaction} ref_signal
* @returns {void} * @returns {void}
*/ */
export function push_reference(target_signal, ref_signal) { export function push_reference(target_signal, ref_signal) {
@ -24,14 +24,14 @@ export function push_reference(target_signal, ref_signal) {
} }
/** /**
* @param {import('../types.js').EffectType} type * @param {import('./types.js').EffectType} type
* @param {(() => void | (() => void)) | ((b: import('../types.js').Block) => void | (() => void))} fn * @param {(() => void | (() => void)) | ((b: import('#client').Block) => void | (() => void))} fn
* @param {boolean} sync * @param {boolean} sync
* @param {null | import('../types.js').Block} block * @param {null | import('#client').Block} block
* @param {boolean} schedule * @param {boolean} schedule
* @returns {import('../types.js').Effect} * @returns {import('#client').Effect}
*/ */
function internal_create_effect(type, fn, sync, block, schedule) { function create_effect(type, fn, sync, block, schedule) {
/** @type {import('#client').Effect} */ /** @type {import('#client').Effect} */
const signal = { const signal = {
b: block, b: block,
@ -54,13 +54,16 @@ function internal_create_effect(type, fn, sync, block, schedule) {
push_reference(current_effect, signal); push_reference(current_effect, signal);
} }
} }
if (schedule) { if (schedule) {
schedule_effect(signal, sync); schedule_effect(signal, sync);
} }
return signal; return signal;
} }
/** /**
* Internal representation of `$effect.active()`
* @returns {boolean} * @returns {boolean}
*/ */
export function effect_active() { export function effect_active() {
@ -70,7 +73,7 @@ export function effect_active() {
/** /**
* Internal representation of `$effect(...)` * Internal representation of `$effect(...)`
* @param {() => void | (() => void)} fn * @param {() => void | (() => void)} fn
* @returns {import('../types.js').Effect} * @returns {import('#client').Effect}
*/ */
export function user_effect(fn) { export function user_effect(fn) {
if (current_effect === null) { if (current_effect === null) {
@ -85,7 +88,7 @@ export function user_effect(fn) {
current_component_context !== null && current_component_context !== null &&
!current_component_context.m; !current_component_context.m;
const effect = internal_create_effect( const effect = create_effect(
EFFECT, EFFECT,
fn, fn,
false, false,
@ -94,9 +97,7 @@ export function user_effect(fn) {
); );
if (apply_component_effect_heuristics) { if (apply_component_effect_heuristics) {
const context = /** @type {import('../types.js').ComponentContext} */ ( const context = /** @type {import('#client').ComponentContext} */ (current_component_context);
current_component_context
);
(context.e ??= []).push(effect); (context.e ??= []).push(effect);
} }
@ -117,33 +118,33 @@ export function user_root_effect(fn) {
/** /**
* @param {() => void | (() => void)} fn * @param {() => void | (() => void)} fn
* @returns {import('../types.js').Effect} * @returns {import('#client').Effect}
*/ */
export function effect(fn) { export function effect(fn) {
return internal_create_effect(EFFECT, fn, false, current_block, true); return create_effect(EFFECT, fn, false, current_block, true);
} }
/** /**
* @param {() => void | (() => void)} fn * @param {() => void | (() => void)} fn
* @returns {import('../types.js').Effect} * @returns {import('#client').Effect}
*/ */
export function managed_effect(fn) { export function managed_effect(fn) {
return internal_create_effect(EFFECT | MANAGED, fn, false, current_block, true); return create_effect(EFFECT | MANAGED, fn, false, current_block, true);
} }
/** /**
* @param {() => void | (() => void)} fn * @param {() => void | (() => void)} fn
* @param {boolean} sync * @param {boolean} sync
* @returns {import('../types.js').Effect} * @returns {import('#client').Effect}
*/ */
export function managed_pre_effect(fn, sync) { export function managed_pre_effect(fn, sync) {
return internal_create_effect(PRE_EFFECT | MANAGED, fn, sync, current_block, true); return create_effect(PRE_EFFECT | MANAGED, fn, sync, current_block, true);
} }
/** /**
* Internal representation of `$effect.pre(...)` * Internal representation of `$effect.pre(...)`
* @param {() => void | (() => void)} fn * @param {() => void | (() => void)} fn
* @returns {import('../types.js').Effect} * @returns {import('#client').Effect}
*/ */
export function pre_effect(fn) { export function pre_effect(fn) {
if (current_effect === null) { if (current_effect === null) {
@ -155,7 +156,7 @@ export function pre_effect(fn) {
); );
} }
const sync = current_effect !== null && (current_effect.f & RENDER_EFFECT) !== 0; const sync = current_effect !== null && (current_effect.f & RENDER_EFFECT) !== 0;
return internal_create_effect( return create_effect(
PRE_EFFECT, PRE_EFFECT,
() => { () => {
const val = fn(); const val = fn();
@ -173,24 +174,24 @@ export function pre_effect(fn) {
* bindings which are in later effects. However, we don't use a pre_effect directly as we don't want to flush anything. * bindings which are in later effects. However, we don't use a pre_effect directly as we don't want to flush anything.
* *
* @param {() => void | (() => void)} fn * @param {() => void | (() => void)} fn
* @returns {import('../types.js').Effect} * @returns {import('#client').Effect}
*/ */
export function invalidate_effect(fn) { export function invalidate_effect(fn) {
return internal_create_effect(PRE_EFFECT, fn, true, current_block, true); return create_effect(PRE_EFFECT, fn, true, current_block, true);
} }
/** /**
* @template {import('../types.js').Block} B * @template {import('#client').Block} B
* @param {(block: B) => void | (() => void)} fn * @param {(block: B) => void | (() => void)} fn
* @param {any} block * @param {any} block
* @param {any} managed * @param {any} managed
* @param {any} sync * @param {any} sync
* @returns {import('../types.js').Effect} * @returns {import('#client').Effect}
*/ */
export function render_effect(fn, block = current_block, managed = false, sync = true) { export function render_effect(fn, block = current_block, managed = false, sync = true) {
let flags = RENDER_EFFECT; let flags = RENDER_EFFECT;
if (managed) { if (managed) {
flags |= MANAGED; flags |= MANAGED;
} }
return internal_create_effect(flags, /** @type {any} */ (fn), sync, block, true); return create_effect(flags, /** @type {any} */ (fn), sync, block, true);
} }

@ -23,31 +23,31 @@ import { CLEAN, DERIVED, DIRTY, MANAGED, SOURCE } from '../constants.js';
/** /**
* @template V * @template V
* @param {V} initial_value * @param {V} value
* @returns {import('../types.js').Source<V>} * @returns {import('#client').Source<V>}
*/ */
/*#__NO_SIDE_EFFECTS__*/ /*#__NO_SIDE_EFFECTS__*/
export function source(initial_value) { export function source(value) {
/** @type {import('#client').Source<V>} */ /** @type {import('#client').Source<V>} */
const signal = { const source = {
c: null, c: null,
e: default_equals, e: default_equals,
f: SOURCE | CLEAN, f: SOURCE | CLEAN,
v: initial_value, v: value,
w: 0 w: 0
}; };
if (DEV) { if (DEV) {
/** @type {import('#client').SourceDebug} */ (signal).inspect = new Set(); /** @type {import('#client').SourceDebug<V>} */ (source).inspect = new Set();
} }
return signal; return source;
} }
/** /**
* @template V * @template V
* @param {V} initial_value * @param {V} initial_value
* @returns {import('../types.js').Source<V>} * @returns {import('#client').Source<V>}
*/ */
/*#__NO_SIDE_EFFECTS__*/ /*#__NO_SIDE_EFFECTS__*/
export function mutable_source(initial_value) { export function mutable_source(initial_value) {
@ -65,7 +65,7 @@ export function mutable_source(initial_value) {
/** /**
* @template V * @template V
* @param {import('./types.js').Source<V>} signal * @param {import('#client').Source<V>} signal
* @param {V} value * @param {V} value
* @returns {void} * @returns {void}
*/ */
@ -75,7 +75,7 @@ export function set_sync(signal, value) {
/** /**
* @template V * @template V
* @param {import('./types.js').Value<V>} source * @param {import('#client').Value<V>} source
* @param {V} value * @param {V} value
*/ */
export function mutate(source, value) { export function mutate(source, value) {
@ -88,7 +88,7 @@ export function mutate(source, value) {
/** /**
* @template V * @template V
* @param {import('./types.js').Source<V>} signal * @param {import('#client').Source<V>} signal
* @param {V} value * @param {V} value
* @returns {V} * @returns {V}
*/ */
@ -150,9 +150,9 @@ export function set(signal, value) {
// @ts-expect-error // @ts-expect-error
if (DEV && signal.inspect) { if (DEV && signal.inspect) {
if (is_batching_effect) { if (is_batching_effect) {
set_last_inspected_signal(/** @type {import('./types.js').ValueDebug} */ (signal)); set_last_inspected_signal(/** @type {import('#client').ValueDebug} */ (signal));
} else { } else {
for (const fn of /** @type {import('./types.js').ValueDebug} */ (signal).inspect) fn(); for (const fn of /** @type {import('#client').ValueDebug} */ (signal).inspect) fn();
} }
} }
} }

@ -10,13 +10,13 @@ import { mutable_source, set } from './sources.js';
* signal that will be updated when the store is. The store references container is needed to * signal that will be updated when the store is. The store references container is needed to
* track reassignments to stores and to track the correct component context. * track reassignments to stores and to track the correct component context.
* @template V * @template V
* @param {import('../types.js').Store<V> | null | undefined} store * @param {import('#client').Store<V> | null | undefined} store
* @param {string} store_name * @param {string} store_name
* @param {import('../types.js').StoreReferencesContainer} stores * @param {import('#client').StoreReferencesContainer} stores
* @returns {V} * @returns {V}
*/ */
export function store_get(store, store_name, stores) { export function store_get(store, store_name, stores) {
/** @type {import('../types.js').StoreReferencesContainer[''] | undefined} */ /** @type {import('#client').StoreReferencesContainer[''] | undefined} */
let entry = stores[store_name]; let entry = stores[store_name];
const is_new = entry === undefined; const is_new = entry === undefined;
@ -29,8 +29,8 @@ export function store_get(store, store_name, stores) {
}; };
// TODO: can we remove this code? it was refactored out when we split up source/comptued signals // TODO: can we remove this code? it was refactored out when we split up source/comptued signals
// push_destroy_fn(entry.value, () => { // push_destroy_fn(entry.value, () => {
// /** @type {import('../types.js').StoreReferencesContainer['']} */ (entry).last_value = // /** @type {import('#client').StoreReferencesContainer['']} */ (entry).last_value =
// /** @type {import('../types.js').StoreReferencesContainer['']} */ (entry).value.value; // /** @type {import('#client').StoreReferencesContainer['']} */ (entry).value.value;
// }); // });
stores[store_name] = entry; stores[store_name] = entry;
} }
@ -49,8 +49,8 @@ export function store_get(store, store_name, stores) {
/** /**
* @template V * @template V
* @param {import('../types.js').Store<V> | null | undefined} store * @param {import('#client').Store<V> | null | undefined} store
* @param {import('../types.js').Source<V>} source * @param {import('#client').Source<V>} source
*/ */
function connect_store_to_signal(store, source) { function connect_store_to_signal(store, source) {
if (store == null) { if (store == null) {
@ -70,7 +70,7 @@ function connect_store_to_signal(store, source) {
/** /**
* Sets the new value of a store and returns that value. * Sets the new value of a store and returns that value.
* @template V * @template V
* @param {import('../types.js').Store<V>} store * @param {import('#client').Store<V>} store
* @param {V} value * @param {V} value
* @returns {V} * @returns {V}
*/ */
@ -81,7 +81,7 @@ export function store_set(store, value) {
/** /**
* Unsubscribes from all auto-subscribed stores on destroy * Unsubscribes from all auto-subscribed stores on destroy
* @param {import('../types.js').StoreReferencesContainer} stores * @param {import('#client').StoreReferencesContainer} stores
*/ */
export function unsubscribe_on_destroy(stores) { export function unsubscribe_on_destroy(stores) {
on_destroy(() => { on_destroy(() => {
@ -97,7 +97,7 @@ export function unsubscribe_on_destroy(stores) {
/** /**
* Updates a store with a new value. * Updates a store with a new value.
* @param {import('../types.js').Store<V>} store the store to update * @param {import('#client').Store<V>} store the store to update
* @param {any} expression the expression that mutates the store * @param {any} expression the expression that mutates the store
* @param {V} new_value the new store value * @param {V} new_value the new store value
* @template V * @template V
@ -110,18 +110,18 @@ export function mutate_store(store, expression, new_value) {
/** /**
* @template V * @template V
* @param {unknown} val * @param {unknown} val
* @returns {val is import('../types.js').Store<V>} * @returns {val is import('#client').Store<V>}
*/ */
export function is_store(val) { export function is_store(val) {
return ( return (
typeof val === 'object' && typeof val === 'object' &&
val !== null && val !== null &&
typeof (/** @type {import('../types.js').Store<V>} */ (val).subscribe) === 'function' typeof (/** @type {import('#client').Store<V>} */ (val).subscribe) === 'function'
); );
} }
/** /**
* @param {import('../types.js').Store<number>} store * @param {import('#client').Store<number>} store
* @param {number} store_value * @param {number} store_value
* @param {1 | -1} [d] * @param {1 | -1} [d]
* @returns {number} * @returns {number}
@ -132,7 +132,7 @@ export function update_store(store, store_value, d = 1) {
} }
/** /**
* @param {import('../types.js').Store<number>} store * @param {import('#client').Store<number>} store
* @param {number} store_value * @param {number} store_value
* @param {1 | -1} [d] * @param {1 | -1} [d]
* @returns {number} * @returns {number}

@ -78,8 +78,6 @@ export type Effect = {
export type Reaction = Derived | Effect; export type Reaction = Derived | Effect;
export type Signal<V = unknown> = Source<V> | Reaction;
export type MaybeSignal<T = unknown> = T | Source<T>; export type MaybeSignal<T = unknown> = T | Source<T>;
export type UnwrappedSignal<T> = T extends Value<infer U> ? U : T; export type UnwrappedSignal<T> = T extends Value<infer U> ? U : T;
@ -87,3 +85,5 @@ export type UnwrappedSignal<T> = T extends Value<infer U> ? U : T;
export type Value<V = unknown> = Source<V> | Derived<V>; export type Value<V = unknown> = Source<V> | Derived<V>;
export type ValueDebug<V = unknown> = SourceDebug<V> | DerivedDebug<V>; export type ValueDebug<V = unknown> = SourceDebug<V> | DerivedDebug<V>;
export type Signal = Source | Derived | Effect;

@ -865,8 +865,7 @@ export function mark_subtree_inert(signal, inert, visited_blocks = new Set()) {
} }
/** /**
* @template V * @param {import('./types.js').Signal} signal
* @param {import('./types.js').Signal<V>} signal
* @param {number} to_status * @param {number} to_status
* @param {boolean} force_schedule * @param {boolean} force_schedule
* @returns {void} * @returns {void}
@ -962,9 +961,9 @@ export function push_destroy_fn(signal, destroy_fn) {
} }
const STATUS_MASK = ~(DIRTY | MAYBE_DIRTY | CLEAN); const STATUS_MASK = ~(DIRTY | MAYBE_DIRTY | CLEAN);
/** /**
* @template V * @param {import('./types.js').Signal} signal
* @param {import('./types.js').Signal<V>} signal
* @param {number} status * @param {number} status
* @returns {void} * @returns {void}
*/ */
@ -974,14 +973,14 @@ export function set_signal_status(signal, status) {
/** /**
* @template V * @template V
* @param {V | import('./types.js').Signal<V>} val * @param {V | import('./types.js').Value<V>} val
* @returns {val is import('./types.js').Signal<V>} * @returns {val is import('./types.js').Value<V>}
*/ */
export function is_signal(val) { export function is_signal(val) {
return ( return (
typeof val === 'object' && typeof val === 'object' &&
val !== null && val !== null &&
typeof (/** @type {import('./types.js').Signal<V>} */ (val).f) === 'number' typeof (/** @type {import('./types.js').Value<V>} */ (val).f) === 'number'
); );
} }

@ -27,13 +27,13 @@ export type Store<V> = {
export type ComponentContext = { export type ComponentContext = {
/** local signals (needed for beforeUpdate/afterUpdate) */ /** local signals (needed for beforeUpdate/afterUpdate) */
d: null | Signal<any>[]; d: null | Source[];
/** props */ /** props */
s: Record<string, unknown>; s: Record<string, unknown>;
/** exports (and props, if `accessors: true`) */ /** exports (and props, if `accessors: true`) */
x: Record<string, any> | null; x: Record<string, any> | null;
/** effects */ /** effects */
e: null | Array<Effect>; e: null | Effect[];
/** mounted */ /** mounted */
m: boolean; m: boolean;
/** parent */ /** parent */
@ -223,11 +223,11 @@ export type EachItemBlock = {
/** dom */ /** dom */
d: null | TemplateNode | Array<TemplateNode>; d: null | TemplateNode | Array<TemplateNode>;
/** effect */ /** effect */
e: null | Reaction; e: null | Effect;
/** item */ /** item */
v: any | Value<any>; v: any | Source<any>;
/** index */ /** index */
i: number | Value<number>; i: number | Source<number>;
/** key */ /** key */
k: unknown; k: unknown;
/** parent */ /** parent */
@ -292,7 +292,7 @@ export type StoreReferencesContainer = Record<
store: Store<any> | null; store: Store<any> | null;
last_value: any; last_value: any;
unsubscribe: Function; unsubscribe: Function;
value: Value<any>; value: Source<any>;
} }
>; >;

@ -1,10 +1,10 @@
import { describe, assert, it } from 'vitest'; import { describe, assert, it } from 'vitest';
import * as $ from '../../src/internal/client/runtime'; import * as $ from '../../src/internal/client/runtime';
import { derived } from '../../src/internal/client/reactivity/deriveds';
import { effect, render_effect, user_effect } from '../../src/internal/client/reactivity/effects'; import { effect, render_effect, user_effect } from '../../src/internal/client/reactivity/effects';
import { source, set } from '../../src/internal/client/reactivity/sources'; import { source, set } from '../../src/internal/client/reactivity/sources';
import type { Derived } from '../../src/internal/client/types'; import type { Derived } from '../../src/internal/client/types';
import { proxy } from '../../src/internal/client/proxy'; import { proxy } from '../../src/internal/client/proxy';
import { derived } from '../../src/internal/client/reactivity/deriveds';
/** /**
* @param runes runes mode * @param runes runes mode

Loading…
Cancel
Save