Skip to content

Commit 86f3265

Browse files
authored
chore: refactor signal types (#10698)
- use interface instead of type (interface is less forgiving) - rename SourceSignal to Source, etc - make Derived extend Source, rather than deriveds sharing a type with effects, since they have much more in common with each other - have a Value type which is a union of Source and Derived, and a Reaction type which is a union of Derived and Effect - avoid using the Signal type (which is a union of all three) unless necessary
1 parent 0cc74e4 commit 86f3265

File tree

13 files changed

+198
-230
lines changed

13 files changed

+198
-230
lines changed

packages/svelte/src/internal/client/dom/blocks/each.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ export function create_each_block(flags, anchor) {
5555
}
5656

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

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

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

285285
block.e = each;
@@ -318,7 +318,7 @@ export function each_indexed(anchor_node, collection, flags, render_fn, fallback
318318
* @param {import('../../types.js').EachBlock} each_block
319319
* @param {Element | Comment | Text} dom
320320
* @param {boolean} is_controlled
321-
* @param {(anchor: null, item: V, index: number | import('../../types.js').Signal<number>) => void} render_fn
321+
* @param {(anchor: null, item: V, index: number | import('../../types.js').Source<number>) => void} render_fn
322322
* @param {number} flags
323323
* @param {boolean} apply_transitions
324324
* @returns {void}
@@ -433,7 +433,7 @@ function reconcile_indexed_array(
433433
* @param {import('../../types.js').EachBlock} each_block
434434
* @param {Element | Comment | Text} dom
435435
* @param {boolean} is_controlled
436-
* @param {(anchor: null, item: V, index: number | import('../../types.js').Signal<number>) => void} render_fn
436+
* @param {(anchor: null, item: V, index: number | import('../../types.js').Source<number>) => void} render_fn
437437
* @param {number} flags
438438
* @param {boolean} apply_transitions
439439
* @param {Array<string> | null} keys
@@ -848,7 +848,7 @@ function update_each_item_block(block, item, index, type) {
848848
each_animation(block, transitions);
849849
}
850850
if (index_is_reactive) {
851-
set(/** @type {import('../../types.js').Signal<number>} */ (block.i), index);
851+
set(/** @type {import('../../types.js').Value<number>} */ (block.i), index);
852852
} else {
853853
block.i = index;
854854
}
@@ -890,15 +890,15 @@ export function destroy_each_item_block(
890890
if (!controlled && dom !== null) {
891891
remove(dom);
892892
}
893-
destroy_signal(/** @type {import('../../types.js').EffectSignal} */ (block.e));
893+
destroy_signal(/** @type {import('../../types.js').Effect} */ (block.e));
894894
}
895895

896896
/**
897897
* @template V
898898
* @param {V} item
899899
* @param {unknown} key
900900
* @param {number} index
901-
* @param {(anchor: null, item: V, index: number | import('../../types.js').Signal<number>) => void} render_fn
901+
* @param {(anchor: null, item: V, index: number | import('../../types.js').Value<number>) => void} render_fn
902902
* @param {number} flags
903903
* @returns {import('../../types.js').EachItemBlock}
904904
*/

packages/svelte/src/internal/client/dom/blocks/if.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export function if_block(anchor_node, condition_fn, consequent_fn, alternate_fn)
5555
let alternate_dom = null;
5656
let has_mounted = false;
5757
/**
58-
* @type {import('../../types.js').EffectSignal | null}
58+
* @type {import('../../types.js').Effect | null}
5959
*/
6060
let current_branch_effect = null;
6161

@@ -117,7 +117,7 @@ export function if_block(anchor_node, condition_fn, consequent_fn, alternate_fn)
117117
const consequent_effect = render_effect(
118118
(
119119
/** @type {any} */ _,
120-
/** @type {import('../../types.js').EffectSignal | null} */ consequent_effect
120+
/** @type {import('../../types.js').Effect | null} */ consequent_effect
121121
) => {
122122
const result = block.v;
123123
if (!result && consequent_dom !== null) {
@@ -143,7 +143,7 @@ export function if_block(anchor_node, condition_fn, consequent_fn, alternate_fn)
143143
const alternate_effect = render_effect(
144144
(
145145
/** @type {any} */ _,
146-
/** @type {import('../../types.js').EffectSignal | null} */ alternate_effect
146+
/** @type {import('../../types.js').Effect | null} */ alternate_effect
147147
) => {
148148
const result = block.v;
149149
if (result && alternate_dom !== null) {

packages/svelte/src/internal/client/proxy.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ export function unstate(value) {
148148
}
149149

150150
/**
151-
* @param {import('./types.js').Signal<number>} signal
151+
* @param {import('./types.js').Source<number>} signal
152152
* @param {1 | -1} [d]
153153
*/
154154
function update_version(signal, d = 1) {

packages/svelte/src/internal/client/reactivity/deriveds.js

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,48 @@
1+
import { DEV } from 'esm-env';
12
import { CLEAN, DERIVED, UNINITIALIZED, UNOWNED } from '../constants.js';
23
import { current_block, current_consumer, current_effect } from '../runtime.js';
3-
import { create_computation_signal, push_reference } from './effects.js';
4+
import { push_reference } from './effects.js';
45
import { default_equals, safe_equal } from './equality.js';
56

67
/**
78
* @template V
89
* @param {() => V} fn
9-
* @returns {import('../types.js').ComputationSignal<V>}
10+
* @returns {import('../types.js').Derived<V>}
1011
*/
1112
/*#__NO_SIDE_EFFECTS__*/
1213
export function derived(fn) {
1314
const is_unowned = current_effect === null;
1415
const flags = is_unowned ? DERIVED | UNOWNED : DERIVED;
15-
const signal = /** @type {import('../types.js').ComputationSignal<V>} */ (
16-
create_computation_signal(flags | CLEAN, UNINITIALIZED, current_block)
17-
);
18-
signal.i = fn;
19-
signal.e = default_equals;
16+
const signal = /** @type {import('../types.js').Derived<V>} */ ({
17+
b: current_block,
18+
c: null,
19+
d: null,
20+
e: default_equals,
21+
f: flags | CLEAN,
22+
i: fn,
23+
r: null,
24+
v: UNINITIALIZED,
25+
w: 0,
26+
x: null,
27+
y: null
28+
});
29+
30+
if (DEV) {
31+
// @ts-expect-error
32+
signal.inspect = new Set();
33+
}
34+
2035
if (current_consumer !== null) {
2136
push_reference(current_consumer, signal);
2237
}
38+
2339
return signal;
2440
}
2541

2642
/**
2743
* @template V
2844
* @param {() => V} fn
29-
* @returns {import('../types.js').ComputationSignal<V>}
45+
* @returns {import('../types.js').Derived<V>}
3046
*/
3147
/*#__NO_SIDE_EFFECTS__*/
3248
export function derived_safe_equal(fn) {

packages/svelte/src/internal/client/reactivity/effects.js

Lines changed: 27 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,59 +2,16 @@ import { DEV } from 'esm-env';
22
import {
33
current_block,
44
current_component_context,
5-
current_consumer,
65
current_effect,
76
destroy_signal,
87
flush_local_render_effects,
98
schedule_effect
109
} from '../runtime.js';
11-
import { default_equals, safe_equal } from './equality.js';
12-
import {
13-
DIRTY,
14-
MANAGED,
15-
RENDER_EFFECT,
16-
EFFECT,
17-
PRE_EFFECT,
18-
DERIVED,
19-
UNOWNED,
20-
CLEAN,
21-
UNINITIALIZED
22-
} from '../constants.js';
23-
24-
/**
25-
* @template V
26-
* @param {import('../types.js').SignalFlags} flags
27-
* @param {V} value
28-
* @param {import('../types.js').Block | null} block
29-
*/
30-
export function create_computation_signal(flags, value, block) {
31-
/** @type {import('../types.js').ComputationSignal<V>} */
32-
const signal = {
33-
b: block,
34-
c: null,
35-
d: null,
36-
e: null,
37-
f: flags,
38-
l: 0,
39-
i: null,
40-
r: null,
41-
v: value,
42-
w: 0,
43-
x: null,
44-
y: null
45-
};
46-
47-
if (DEV) {
48-
// @ts-expect-error
49-
signal.inspect = new Set();
50-
}
51-
52-
return signal;
53-
}
10+
import { DIRTY, MANAGED, RENDER_EFFECT, EFFECT, PRE_EFFECT } from '../constants.js';
5411

5512
/**
56-
* @param {import('../types.js').ComputationSignal} target_signal
57-
* @param {import('../types.js').ComputationSignal} ref_signal
13+
* @param {import('../types.js').Reaction} target_signal
14+
* @param {import('../types.js').Reaction} ref_signal
5815
* @returns {void}
5916
*/
6017
export function push_reference(target_signal, ref_signal) {
@@ -72,12 +29,25 @@ export function push_reference(target_signal, ref_signal) {
7229
* @param {boolean} sync
7330
* @param {null | import('../types.js').Block} block
7431
* @param {boolean} schedule
75-
* @returns {import('../types.js').EffectSignal}
32+
* @returns {import('../types.js').Effect}
7633
*/
7734
function internal_create_effect(type, fn, sync, block, schedule) {
78-
const signal = create_computation_signal(type | DIRTY, null, block);
79-
signal.i = fn;
80-
signal.x = current_component_context;
35+
/** @type {import('#client').Effect} */
36+
const signal = {
37+
b: block,
38+
c: null,
39+
d: null,
40+
e: null,
41+
f: type | DIRTY,
42+
l: 0,
43+
i: fn,
44+
r: null,
45+
v: null,
46+
w: 0,
47+
x: current_component_context,
48+
y: null
49+
};
50+
8151
if (current_effect !== null) {
8252
signal.l = current_effect.l + 1;
8353
if ((type & MANAGED) === 0) {
@@ -100,7 +70,7 @@ export function effect_active() {
10070
/**
10171
* Internal representation of `$effect(...)`
10272
* @param {() => void | (() => void)} fn
103-
* @returns {import('../types.js').EffectSignal}
73+
* @returns {import('../types.js').Effect}
10474
*/
10575
export function user_effect(fn) {
10676
if (current_effect === null) {
@@ -147,15 +117,15 @@ export function user_root_effect(fn) {
147117

148118
/**
149119
* @param {() => void | (() => void)} fn
150-
* @returns {import('../types.js').EffectSignal}
120+
* @returns {import('../types.js').Effect}
151121
*/
152122
export function effect(fn) {
153123
return internal_create_effect(EFFECT, fn, false, current_block, true);
154124
}
155125

156126
/**
157127
* @param {() => void | (() => void)} fn
158-
* @returns {import('../types.js').EffectSignal}
128+
* @returns {import('../types.js').Effect}
159129
*/
160130
export function managed_effect(fn) {
161131
return internal_create_effect(EFFECT | MANAGED, fn, false, current_block, true);
@@ -164,7 +134,7 @@ export function managed_effect(fn) {
164134
/**
165135
* @param {() => void | (() => void)} fn
166136
* @param {boolean} sync
167-
* @returns {import('../types.js').EffectSignal}
137+
* @returns {import('../types.js').Effect}
168138
*/
169139
export function managed_pre_effect(fn, sync) {
170140
return internal_create_effect(PRE_EFFECT | MANAGED, fn, sync, current_block, true);
@@ -173,7 +143,7 @@ export function managed_pre_effect(fn, sync) {
173143
/**
174144
* Internal representation of `$effect.pre(...)`
175145
* @param {() => void | (() => void)} fn
176-
* @returns {import('../types.js').EffectSignal}
146+
* @returns {import('../types.js').Effect}
177147
*/
178148
export function pre_effect(fn) {
179149
if (current_effect === null) {
@@ -203,7 +173,7 @@ export function pre_effect(fn) {
203173
* bindings which are in later effects. However, we don't use a pre_effect directly as we don't want to flush anything.
204174
*
205175
* @param {() => void | (() => void)} fn
206-
* @returns {import('../types.js').EffectSignal}
176+
* @returns {import('../types.js').Effect}
207177
*/
208178
export function invalidate_effect(fn) {
209179
return internal_create_effect(PRE_EFFECT, fn, true, current_block, true);
@@ -215,7 +185,7 @@ export function invalidate_effect(fn) {
215185
* @param {any} block
216186
* @param {any} managed
217187
* @param {any} sync
218-
* @returns {import('../types.js').EffectSignal}
188+
* @returns {import('../types.js').Effect}
219189
*/
220190
export function render_effect(fn, block = current_block, managed = false, sync = true) {
221191
let flags = RENDER_EFFECT;

0 commit comments

Comments
 (0)