Skip to content

chore: refactor signal types #10698

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 11 commits into from
Mar 5, 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: 9 additions & 9 deletions packages/svelte/src/internal/client/dom/blocks/each.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ export function create_each_block(flags, anchor) {
}

/**
* @param {any | import('../../types.js').Signal<any>} item
* @param {number | import('../../types.js').Signal<number>} index
* @param {any | import('../../types.js').Value<any>} item
* @param {number | import('../../types.js').Value<number>} index
* @param {null | unknown} key
* @returns {import('../../types.js').EachItemBlock}
*/
Expand Down Expand Up @@ -110,7 +110,7 @@ function each(anchor_node, collection, flags, key_fn, render_fn, fallback_fn, re
/** @type {Array<string> | null} */
let keys = null;

/** @type {null | import('../../types.js').EffectSignal} */
/** @type {null | import('../../types.js').Effect} */
let render = null;

/**
Expand Down Expand Up @@ -279,7 +279,7 @@ function each(anchor_node, collection, flags, key_fn, render_fn, fallback_fn, re
}
// Clear the array
reconcile_fn([], block, anchor_node, is_controlled, render_fn, flags, false, keys);
destroy_signal(/** @type {import('../../types.js').EffectSignal} */ (render));
destroy_signal(/** @type {import('../../types.js').Effect} */ (render));
});

block.e = each;
Expand Down Expand Up @@ -318,7 +318,7 @@ export function each_indexed(anchor_node, collection, flags, render_fn, fallback
* @param {import('../../types.js').EachBlock} each_block
* @param {Element | Comment | Text} dom
* @param {boolean} is_controlled
* @param {(anchor: null, item: V, index: number | import('../../types.js').Signal<number>) => void} render_fn
* @param {(anchor: null, item: V, index: number | import('../../types.js').Source<number>) => void} render_fn
* @param {number} flags
* @param {boolean} apply_transitions
* @returns {void}
Expand Down Expand Up @@ -433,7 +433,7 @@ function reconcile_indexed_array(
* @param {import('../../types.js').EachBlock} each_block
* @param {Element | Comment | Text} dom
* @param {boolean} is_controlled
* @param {(anchor: null, item: V, index: number | import('../../types.js').Signal<number>) => void} render_fn
* @param {(anchor: null, item: V, index: number | import('../../types.js').Source<number>) => void} render_fn
* @param {number} flags
* @param {boolean} apply_transitions
* @param {Array<string> | null} keys
Expand Down Expand Up @@ -848,7 +848,7 @@ function update_each_item_block(block, item, index, type) {
each_animation(block, transitions);
}
if (index_is_reactive) {
set(/** @type {import('../../types.js').Signal<number>} */ (block.i), index);
set(/** @type {import('../../types.js').Value<number>} */ (block.i), index);
} else {
block.i = index;
}
Expand Down Expand Up @@ -890,15 +890,15 @@ export function destroy_each_item_block(
if (!controlled && dom !== null) {
remove(dom);
}
destroy_signal(/** @type {import('../../types.js').EffectSignal} */ (block.e));
destroy_signal(/** @type {import('../../types.js').Effect} */ (block.e));
}

/**
* @template V
* @param {V} item
* @param {unknown} key
* @param {number} index
* @param {(anchor: null, item: V, index: number | import('../../types.js').Signal<number>) => void} render_fn
* @param {(anchor: null, item: V, index: number | import('../../types.js').Value<number>) => void} render_fn
* @param {number} flags
* @returns {import('../../types.js').EachItemBlock}
*/
Expand Down
6 changes: 3 additions & 3 deletions packages/svelte/src/internal/client/dom/blocks/if.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export function if_block(anchor_node, condition_fn, consequent_fn, alternate_fn)
let alternate_dom = null;
let has_mounted = false;
/**
* @type {import('../../types.js').EffectSignal | null}
* @type {import('../../types.js').Effect | null}
*/
let current_branch_effect = null;

Expand Down Expand Up @@ -117,7 +117,7 @@ export function if_block(anchor_node, condition_fn, consequent_fn, alternate_fn)
const consequent_effect = render_effect(
(
/** @type {any} */ _,
/** @type {import('../../types.js').EffectSignal | null} */ consequent_effect
/** @type {import('../../types.js').Effect | null} */ consequent_effect
) => {
const result = block.v;
if (!result && consequent_dom !== null) {
Expand All @@ -143,7 +143,7 @@ export function if_block(anchor_node, condition_fn, consequent_fn, alternate_fn)
const alternate_effect = render_effect(
(
/** @type {any} */ _,
/** @type {import('../../types.js').EffectSignal | null} */ alternate_effect
/** @type {import('../../types.js').Effect | null} */ alternate_effect
) => {
const result = block.v;
if (result && alternate_dom !== null) {
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte/src/internal/client/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export function unstate(value) {
}

/**
* @param {import('./types.js').Signal<number>} signal
* @param {import('./types.js').Source<number>} signal
* @param {1 | -1} [d]
*/
function update_version(signal, d = 1) {
Expand Down
32 changes: 24 additions & 8 deletions packages/svelte/src/internal/client/reactivity/deriveds.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,48 @@
import { DEV } from 'esm-env';
import { CLEAN, DERIVED, UNINITIALIZED, UNOWNED } from '../constants.js';
import { current_block, current_consumer, current_effect } from '../runtime.js';
import { create_computation_signal, push_reference } from './effects.js';
import { push_reference } from './effects.js';
import { default_equals, safe_equal } from './equality.js';

/**
* @template V
* @param {() => V} fn
* @returns {import('../types.js').ComputationSignal<V>}
* @returns {import('../types.js').Derived<V>}
*/
/*#__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').ComputationSignal<V>} */ (
create_computation_signal(flags | CLEAN, UNINITIALIZED, current_block)
);
signal.i = fn;
signal.e = default_equals;
const signal = /** @type {import('../types.js').Derived<V>} */ ({
b: current_block,
c: null,
d: null,
e: default_equals,
f: flags | CLEAN,
i: fn,
r: null,
v: UNINITIALIZED,
w: 0,
x: null,
y: null
});

if (DEV) {
// @ts-expect-error
signal.inspect = new Set();
}

if (current_consumer !== null) {
push_reference(current_consumer, signal);
}

return signal;
}

/**
* @template V
* @param {() => V} fn
* @returns {import('../types.js').ComputationSignal<V>}
* @returns {import('../types.js').Derived<V>}
*/
/*#__NO_SIDE_EFFECTS__*/
export function derived_safe_equal(fn) {
Expand Down
84 changes: 27 additions & 57 deletions packages/svelte/src/internal/client/reactivity/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,16 @@ import { DEV } from 'esm-env';
import {
current_block,
current_component_context,
current_consumer,
current_effect,
destroy_signal,
flush_local_render_effects,
schedule_effect
} from '../runtime.js';
import { default_equals, safe_equal } from './equality.js';
import {
DIRTY,
MANAGED,
RENDER_EFFECT,
EFFECT,
PRE_EFFECT,
DERIVED,
UNOWNED,
CLEAN,
UNINITIALIZED
} from '../constants.js';

/**
* @template V
* @param {import('../types.js').SignalFlags} flags
* @param {V} value
* @param {import('../types.js').Block | null} block
*/
export function create_computation_signal(flags, value, block) {
/** @type {import('../types.js').ComputationSignal<V>} */
const signal = {
b: block,
c: null,
d: null,
e: null,
f: flags,
l: 0,
i: null,
r: null,
v: value,
w: 0,
x: null,
y: null
};

if (DEV) {
// @ts-expect-error
signal.inspect = new Set();
}

return signal;
}
import { DIRTY, MANAGED, RENDER_EFFECT, EFFECT, PRE_EFFECT } from '../constants.js';

/**
* @param {import('../types.js').ComputationSignal} target_signal
* @param {import('../types.js').ComputationSignal} ref_signal
* @param {import('../types.js').Reaction} target_signal
* @param {import('../types.js').Reaction} ref_signal
* @returns {void}
*/
export function push_reference(target_signal, ref_signal) {
Expand All @@ -72,12 +29,25 @@ export function push_reference(target_signal, ref_signal) {
* @param {boolean} sync
* @param {null | import('../types.js').Block} block
* @param {boolean} schedule
* @returns {import('../types.js').EffectSignal}
* @returns {import('../types.js').Effect}
*/
function internal_create_effect(type, fn, sync, block, schedule) {
const signal = create_computation_signal(type | DIRTY, null, block);
signal.i = fn;
signal.x = current_component_context;
/** @type {import('#client').Effect} */
const signal = {
b: block,
c: null,
d: null,
e: null,
f: type | DIRTY,
l: 0,
i: fn,
r: null,
v: null,
w: 0,
x: current_component_context,
y: null
};

if (current_effect !== null) {
signal.l = current_effect.l + 1;
if ((type & MANAGED) === 0) {
Expand All @@ -100,7 +70,7 @@ export function effect_active() {
/**
* Internal representation of `$effect(...)`
* @param {() => void | (() => void)} fn
* @returns {import('../types.js').EffectSignal}
* @returns {import('../types.js').Effect}
*/
export function user_effect(fn) {
if (current_effect === null) {
Expand Down Expand Up @@ -147,15 +117,15 @@ export function user_root_effect(fn) {

/**
* @param {() => void | (() => void)} fn
* @returns {import('../types.js').EffectSignal}
* @returns {import('../types.js').Effect}
*/
export function effect(fn) {
return internal_create_effect(EFFECT, fn, false, current_block, true);
}

/**
* @param {() => void | (() => void)} fn
* @returns {import('../types.js').EffectSignal}
* @returns {import('../types.js').Effect}
*/
export function managed_effect(fn) {
return internal_create_effect(EFFECT | MANAGED, fn, false, current_block, true);
Expand All @@ -164,7 +134,7 @@ export function managed_effect(fn) {
/**
* @param {() => void | (() => void)} fn
* @param {boolean} sync
* @returns {import('../types.js').EffectSignal}
* @returns {import('../types.js').Effect}
*/
export function managed_pre_effect(fn, sync) {
return internal_create_effect(PRE_EFFECT | MANAGED, fn, sync, current_block, true);
Expand All @@ -173,7 +143,7 @@ export function managed_pre_effect(fn, sync) {
/**
* Internal representation of `$effect.pre(...)`
* @param {() => void | (() => void)} fn
* @returns {import('../types.js').EffectSignal}
* @returns {import('../types.js').Effect}
*/
export function pre_effect(fn) {
if (current_effect === null) {
Expand Down Expand Up @@ -203,7 +173,7 @@ 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').EffectSignal}
* @returns {import('../types.js').Effect}
*/
export function invalidate_effect(fn) {
return internal_create_effect(PRE_EFFECT, fn, true, current_block, true);
Expand All @@ -215,7 +185,7 @@ export function invalidate_effect(fn) {
* @param {any} block
* @param {any} managed
* @param {any} sync
* @returns {import('../types.js').EffectSignal}
* @returns {import('../types.js').Effect}
*/
export function render_effect(fn, block = current_block, managed = false, sync = true) {
let flags = RENDER_EFFECT;
Expand Down
Loading