Skip to content

chore: tidy up #10705

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions packages/svelte/src/internal/client/reactivity/deriveds.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,27 @@ import { default_equals, safe_equal } from './equality.js';
*/
/*#__NO_SIDE_EFFECTS__*/
export function derived(fn) {
const is_unowned = current_effect === null;
const flags = is_unowned ? DERIVED | UNOWNED : DERIVED;
const signal = /** @type {import('../types.js').Derived<V>} */ ({
let flags = DERIVED | CLEAN;
if (current_effect === null) flags |= UNOWNED;

/** @type {import('#client').Derived<V>} */
const signal = {
b: current_block,
c: null,
d: null,
e: default_equals,
f: flags | CLEAN,
f: flags,
i: fn,
r: null,
// @ts-expect-error
v: UNINITIALIZED,
w: 0,
x: null,
y: null
});
};

if (DEV) {
// @ts-expect-error
signal.inspect = new Set();
/** @type {import('#client').DerivedDebug} */ (signal).inspect = new Set();
}

if (current_consumer !== null) {
Expand All @@ -42,7 +44,7 @@ export function derived(fn) {
/**
* @template V
* @param {() => V} fn
* @returns {import('../types.js').Derived<V>}
* @returns {import('#client').Derived<V>}
*/
/*#__NO_SIDE_EFFECTS__*/
export function derived_safe_equal(fn) {
Expand Down
51 changes: 26 additions & 25 deletions packages/svelte/src/internal/client/reactivity/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import {
import { DIRTY, MANAGED, RENDER_EFFECT, EFFECT, PRE_EFFECT } from '../constants.js';

/**
* @param {import('../types.js').Reaction} target_signal
* @param {import('../types.js').Reaction} ref_signal
* @param {import('#client').Reaction} target_signal
* @param {import('#client').Reaction} ref_signal
* @returns {void}
*/
export function push_reference(target_signal, ref_signal) {
Expand All @@ -24,14 +24,14 @@ export function push_reference(target_signal, ref_signal) {
}

/**
* @param {import('../types.js').EffectType} type
* @param {(() => void | (() => void)) | ((b: import('../types.js').Block) => void | (() => void))} fn
* @param {import('./types.js').EffectType} type
* @param {(() => void | (() => void)) | ((b: import('#client').Block) => void | (() => void))} fn
* @param {boolean} sync
* @param {null | import('../types.js').Block} block
* @param {null | import('#client').Block} block
* @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} */
const signal = {
b: block,
Expand All @@ -54,13 +54,16 @@ function internal_create_effect(type, fn, sync, block, schedule) {
push_reference(current_effect, signal);
}
}

if (schedule) {
schedule_effect(signal, sync);
}

return signal;
}

/**
* Internal representation of `$effect.active()`
* @returns {boolean}
*/
export function effect_active() {
Expand All @@ -70,7 +73,7 @@ export function effect_active() {
/**
* Internal representation of `$effect(...)`
* @param {() => void | (() => void)} fn
* @returns {import('../types.js').Effect}
* @returns {import('#client').Effect}
*/
export function user_effect(fn) {
if (current_effect === null) {
Expand All @@ -85,7 +88,7 @@ export function user_effect(fn) {
current_component_context !== null &&
!current_component_context.m;

const effect = internal_create_effect(
const effect = create_effect(
EFFECT,
fn,
false,
Expand All @@ -94,9 +97,7 @@ export function user_effect(fn) {
);

if (apply_component_effect_heuristics) {
const context = /** @type {import('../types.js').ComponentContext} */ (
current_component_context
);
const context = /** @type {import('#client').ComponentContext} */ (current_component_context);
(context.e ??= []).push(effect);
}

Expand All @@ -117,33 +118,33 @@ export function user_root_effect(fn) {

/**
* @param {() => void | (() => void)} fn
* @returns {import('../types.js').Effect}
* @returns {import('#client').Effect}
*/
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
* @returns {import('../types.js').Effect}
* @returns {import('#client').Effect}
*/
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 {boolean} sync
* @returns {import('../types.js').Effect}
* @returns {import('#client').Effect}
*/
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(...)`
* @param {() => void | (() => void)} fn
* @returns {import('../types.js').Effect}
* @returns {import('#client').Effect}
*/
export function pre_effect(fn) {
if (current_effect === null) {
Expand All @@ -155,7 +156,7 @@ export function pre_effect(fn) {
);
}
const sync = current_effect !== null && (current_effect.f & RENDER_EFFECT) !== 0;
return internal_create_effect(
return create_effect(
PRE_EFFECT,
() => {
const val = fn();
Expand All @@ -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.
*
* @param {() => void | (() => void)} fn
* @returns {import('../types.js').Effect}
* @returns {import('#client').Effect}
*/
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 {any} block
* @param {any} managed
* @param {any} sync
* @returns {import('../types.js').Effect}
* @returns {import('#client').Effect}
*/
export function render_effect(fn, block = current_block, managed = false, sync = true) {
let flags = RENDER_EFFECT;
if (managed) {
flags |= MANAGED;
}
return internal_create_effect(flags, /** @type {any} */ (fn), sync, block, true);
return create_effect(flags, /** @type {any} */ (fn), sync, block, true);
}
26 changes: 13 additions & 13 deletions packages/svelte/src/internal/client/reactivity/sources.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,31 @@ import { CLEAN, DERIVED, DIRTY, MANAGED, SOURCE } from '../constants.js';

/**
* @template V
* @param {V} initial_value
* @returns {import('../types.js').Source<V>}
* @param {V} value
* @returns {import('#client').Source<V>}
*/
/*#__NO_SIDE_EFFECTS__*/
export function source(initial_value) {
export function source(value) {
/** @type {import('#client').Source<V>} */
const signal = {
const source = {
c: null,
e: default_equals,
f: SOURCE | CLEAN,
v: initial_value,
v: value,
w: 0
};

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
* @param {V} initial_value
* @returns {import('../types.js').Source<V>}
* @returns {import('#client').Source<V>}
*/
/*#__NO_SIDE_EFFECTS__*/
export function mutable_source(initial_value) {
Expand All @@ -65,7 +65,7 @@ export function mutable_source(initial_value) {

/**
* @template V
* @param {import('./types.js').Source<V>} signal
* @param {import('#client').Source<V>} signal
* @param {V} value
* @returns {void}
*/
Expand All @@ -75,7 +75,7 @@ export function set_sync(signal, value) {

/**
* @template V
* @param {import('./types.js').Value<V>} source
* @param {import('#client').Value<V>} source
* @param {V} value
*/
export function mutate(source, value) {
Expand All @@ -88,7 +88,7 @@ export function mutate(source, value) {

/**
* @template V
* @param {import('./types.js').Source<V>} signal
* @param {import('#client').Source<V>} signal
* @param {V} value
* @returns {V}
*/
Expand Down Expand Up @@ -150,9 +150,9 @@ export function set(signal, value) {
// @ts-expect-error
if (DEV && signal.inspect) {
if (is_batching_effect) {
set_last_inspected_signal(/** @type {import('./types.js').ValueDebug} */ (signal));
set_last_inspected_signal(/** @type {import('#client').ValueDebug} */ (signal));
} else {
for (const fn of /** @type {import('./types.js').ValueDebug} */ (signal).inspect) fn();
for (const fn of /** @type {import('#client').ValueDebug} */ (signal).inspect) fn();
}
}
}
Expand Down
28 changes: 14 additions & 14 deletions packages/svelte/src/internal/client/reactivity/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
* track reassignments to stores and to track the correct component context.
* @template V
* @param {import('../types.js').Store<V> | null | undefined} store
* @param {import('#client').Store<V> | null | undefined} store
* @param {string} store_name
* @param {import('../types.js').StoreReferencesContainer} stores
* @param {import('#client').StoreReferencesContainer} stores
* @returns {V}
*/
export function store_get(store, store_name, stores) {
/** @type {import('../types.js').StoreReferencesContainer[''] | undefined} */
/** @type {import('#client').StoreReferencesContainer[''] | undefined} */
let entry = stores[store_name];
const is_new = entry === undefined;

Expand All @@ -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
// push_destroy_fn(entry.value, () => {
// /** @type {import('../types.js').StoreReferencesContainer['']} */ (entry).last_value =
// /** @type {import('../types.js').StoreReferencesContainer['']} */ (entry).value.value;
// /** @type {import('#client').StoreReferencesContainer['']} */ (entry).last_value =
// /** @type {import('#client').StoreReferencesContainer['']} */ (entry).value.value;
// });
stores[store_name] = entry;
}
Expand All @@ -49,8 +49,8 @@ export function store_get(store, store_name, stores) {

/**
* @template V
* @param {import('../types.js').Store<V> | null | undefined} store
* @param {import('../types.js').Source<V>} source
* @param {import('#client').Store<V> | null | undefined} store
* @param {import('#client').Source<V>} source
*/
function connect_store_to_signal(store, source) {
if (store == null) {
Expand All @@ -70,7 +70,7 @@ function connect_store_to_signal(store, source) {
/**
* Sets the new value of a store and returns that value.
* @template V
* @param {import('../types.js').Store<V>} store
* @param {import('#client').Store<V>} store
* @param {V} value
* @returns {V}
*/
Expand All @@ -81,7 +81,7 @@ export function store_set(store, value) {

/**
* 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) {
on_destroy(() => {
Expand All @@ -97,7 +97,7 @@ export function unsubscribe_on_destroy(stores) {

/**
* 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 {V} new_value the new store value
* @template V
Expand All @@ -110,18 +110,18 @@ export function mutate_store(store, expression, new_value) {
/**
* @template V
* @param {unknown} val
* @returns {val is import('../types.js').Store<V>}
* @returns {val is import('#client').Store<V>}
*/
export function is_store(val) {
return (
typeof val === 'object' &&
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 {1 | -1} [d]
* @returns {number}
Expand All @@ -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 {1 | -1} [d]
* @returns {number}
Expand Down
4 changes: 2 additions & 2 deletions packages/svelte/src/internal/client/reactivity/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ export type Effect = {

export type Reaction = Derived | Effect;

export type Signal<V = unknown> = Source<V> | Reaction;

export type MaybeSignal<T = unknown> = T | Source<T>;

export type UnwrappedSignal<T> = T extends Value<infer U> ? U : T;

export type Value<V = unknown> = Source<V> | Derived<V>;

export type ValueDebug<V = unknown> = SourceDebug<V> | DerivedDebug<V>;

export type Signal = Source | Derived | Effect;
Loading