Skip to content

Commit ae27f27

Browse files
authored
make internal sources ownerless (#13013)
* make internal sources ownerless * WIP * WIP * WIP * same for mutable_state * oops * DRY * unrelated * changeset * fix
1 parent 574d260 commit ae27f27

File tree

25 files changed

+137
-81
lines changed

25 files changed

+137
-81
lines changed

.changeset/six-moons-invent.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: make internal sources ownerless

benchmarking/benchmarks/kairo/kairo_avoidable.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as $ from '../../../packages/svelte/src/internal/client/index.js';
33
import { busy } from './util.js';
44

55
function setup() {
6-
let head = $.source(0);
6+
let head = $.state(0);
77
let computed1 = $.derived(() => $.get(head));
88
let computed2 = $.derived(() => ($.get(computed1), 0));
99
let computed3 = $.derived(() => (busy(), $.get(computed2) + 1)); // heavy computation

benchmarking/benchmarks/kairo/kairo_broad.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { assert, fastest_test } from '../../utils.js';
22
import * as $ from '../../../packages/svelte/src/internal/client/index.js';
33

44
function setup() {
5-
let head = $.source(0);
5+
let head = $.state(0);
66
let last = head;
77
let counter = 0;
88

benchmarking/benchmarks/kairo/kairo_deep.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ let len = 50;
55
const iter = 50;
66

77
function setup() {
8-
let head = $.source(0);
8+
let head = $.state(0);
99
let current = head;
1010
for (let i = 0; i < len; i++) {
1111
let c = current;

benchmarking/benchmarks/kairo/kairo_diamond.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as $ from '../../../packages/svelte/src/internal/client/index.js';
44
let width = 5;
55

66
function setup() {
7-
let head = $.source(0);
7+
let head = $.state(0);
88
let current = [];
99
for (let i = 0; i < width; i++) {
1010
current.push(

benchmarking/benchmarks/kairo/kairo_mux.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { assert, fastest_test } from '../../utils.js';
22
import * as $ from '../../../packages/svelte/src/internal/client/index.js';
33

44
function setup() {
5-
let heads = new Array(100).fill(null).map((_) => $.source(0));
5+
let heads = new Array(100).fill(null).map((_) => $.state(0));
66
const mux = $.derived(() => {
77
return Object.fromEntries(heads.map((h) => $.get(h)).entries());
88
});

benchmarking/benchmarks/kairo/kairo_repeated.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as $ from '../../../packages/svelte/src/internal/client/index.js';
44
let size = 30;
55

66
function setup() {
7-
let head = $.source(0);
7+
let head = $.state(0);
88
let current = $.derived(() => {
99
let result = 0;
1010
for (let i = 0; i < size; i++) {

benchmarking/benchmarks/kairo/kairo_triangle.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function count(number) {
1111
}
1212

1313
function setup() {
14-
let head = $.source(0);
14+
let head = $.state(0);
1515
let current = head;
1616
let list = [];
1717
for (let i = 0; i < width; i++) {

benchmarking/benchmarks/kairo/kairo_unstable.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { assert, fastest_test } from '../../utils.js';
22
import * as $ from '../../../packages/svelte/src/internal/client/index.js';
33

44
function setup() {
5-
let head = $.source(0);
5+
let head = $.state(0);
66
const double = $.derived(() => $.get(head) * 2);
77
const inverse = $.derived(() => -$.get(head));
88
let current = $.derived(() => {

benchmarking/benchmarks/mol_bench.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ const numbers = Array.from({ length: 5 }, (_, i) => i);
2020

2121
function setup() {
2222
let res = [];
23-
const A = $.source(0);
24-
const B = $.source(0);
23+
const A = $.state(0);
24+
const B = $.state(0);
2525
const C = $.derived(() => ($.get(A) % 2) + ($.get(B) % 2));
2626
const D = $.derived(() => numbers.map((i) => i + ($.get(A) % 2) - ($.get(B) % 2)));
2727
D.equals = function (/** @type {number[]} */ l) {

benchmarking/benchmarks/sbench.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const COUNT = 1e5;
99
*/
1010
function create_data_signals(n, sources) {
1111
for (let i = 0; i < n; i++) {
12-
sources[i] = $.source(i);
12+
sources[i] = $.state(i);
1313
}
1414
return sources;
1515
}

packages/svelte/src/compiler/phases/3-transform/client/transform-client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ export function client_component(analysis, options) {
207207

208208
for (const [name, binding] of analysis.instance.scope.declarations) {
209209
if (binding.kind === 'legacy_reactive') {
210-
legacy_reactive_declarations.push(b.const(name, b.call('$.mutable_source')));
210+
legacy_reactive_declarations.push(b.const(name, b.call('$.mutable_state')));
211211
}
212212
if (binding.kind === 'store_sub') {
213213
if (store_setup.length === 0) {

packages/svelte/src/compiler/phases/3-transform/client/visitors/ClassBody.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,17 +113,17 @@ export function ClassBody(node, context) {
113113
value =
114114
field.kind === 'state'
115115
? b.call(
116-
'$.source',
116+
'$.state',
117117
should_proxy(init, context.state.scope) ? b.call('$.proxy', init) : init
118118
)
119119
: field.kind === 'raw_state'
120-
? b.call('$.source', init)
120+
? b.call('$.state', init)
121121
: field.kind === 'derived_by'
122122
? b.call('$.derived', init)
123123
: b.call('$.derived', b.thunk(init));
124124
} else {
125125
// if no arguments, we know it's state as `$derived()` is a compile error
126-
value = b.call('$.source');
126+
value = b.call('$.state');
127127
}
128128

129129
if (is_private) {

packages/svelte/src/compiler/phases/3-transform/client/visitors/VariableDeclaration.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ export function VariableDeclaration(node, context) {
126126
value = b.call('$.proxy', value);
127127
}
128128
if (is_state_source(binding, context.state.analysis)) {
129-
value = b.call('$.source', value);
129+
value = b.call('$.state', value);
130130
}
131131
return value;
132132
};
@@ -291,7 +291,7 @@ export function VariableDeclaration(node, context) {
291291
*/
292292
function create_state_declarators(declarator, scope, value) {
293293
if (declarator.id.type === 'Identifier') {
294-
return [b.declarator(declarator.id, b.call('$.mutable_source', value))];
294+
return [b.declarator(declarator.id, b.call('$.mutable_state', value))];
295295
}
296296

297297
const tmp = scope.generate('tmp');
@@ -303,7 +303,7 @@ function create_state_declarators(declarator, scope, value) {
303303
const binding = scope.get(/** @type {Identifier} */ (path.node).name);
304304
return b.declarator(
305305
path.node,
306-
binding?.kind === 'state' ? b.call('$.mutable_source', value) : value
306+
binding?.kind === 'state' ? b.call('$.mutable_state', value) : value
307307
);
308308
})
309309
];

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,8 @@ export function await_block(node, get_input, pending_fn, then_fn, catch_fn) {
5252
/** @type {Effect | null} */
5353
var catch_effect;
5454

55-
var input_source = runes
56-
? source(/** @type {V} */ (undefined))
57-
: mutable_source(/** @type {V} */ (undefined));
58-
var error_source = runes ? source(undefined) : mutable_source(undefined);
55+
var input_source = (runes ? source : mutable_source)(/** @type {V} */ (undefined));
56+
var error_source = (runes ? source : mutable_source)(undefined);
5957
var resolved = false;
6058

6159
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export {
105105
user_effect,
106106
user_pre_effect
107107
} from './reactivity/effects.js';
108-
export { mutable_source, mutate, source, set } from './reactivity/sources.js';
108+
export { mutable_state, mutate, set, state } from './reactivity/sources.js';
109109
export {
110110
prop,
111111
rest_props,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ export function proxy(value, parent = null, prev) {
118118

119119
// create a source, but only if it's an own property and not a prototype property
120120
if (s === undefined && (!exists || get_descriptor(target, prop)?.writable)) {
121-
s = source(proxy(exists ? target[prop] : UNINITIALIZED, metadata), null);
121+
s = source(proxy(exists ? target[prop] : UNINITIALIZED, metadata));
122122
sources.set(prop, s);
123123
}
124124

@@ -170,7 +170,7 @@ export function proxy(value, parent = null, prev) {
170170
(current_effect !== null && (!has || get_descriptor(target, prop)?.writable))
171171
) {
172172
if (s === undefined) {
173-
s = source(has ? proxy(target[prop], metadata) : UNINITIALIZED, null);
173+
s = source(has ? proxy(target[prop], metadata) : UNINITIALIZED);
174174
sources.set(prop, s);
175175
}
176176

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

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,39 +34,34 @@ let inspect_effects = new Set();
3434
/**
3535
* @template V
3636
* @param {V} v
37-
* @param {Reaction | null} [owner]
3837
* @returns {Source<V>}
3938
*/
40-
/*#__NO_SIDE_EFFECTS__*/
41-
export function source(v, owner = current_reaction) {
42-
var source = {
39+
export function source(v) {
40+
return {
4341
f: 0, // TODO ideally we could skip this altogether, but it causes type errors
4442
v,
4543
reactions: null,
4644
equals,
4745
version: 0
4846
};
47+
}
4948

50-
if (owner !== null && (owner.f & DERIVED) !== 0) {
51-
if (derived_sources === null) {
52-
set_derived_sources([source]);
53-
} else {
54-
derived_sources.push(source);
55-
}
56-
}
57-
58-
return source;
49+
/**
50+
* @template V
51+
* @param {V} v
52+
*/
53+
export function state(v) {
54+
return push_derived_source(source(v));
5955
}
6056

6157
/**
6258
* @template V
6359
* @param {V} initial_value
64-
* @param {Reaction | null} [owner]
6560
* @returns {Source<V>}
6661
*/
6762
/*#__NO_SIDE_EFFECTS__*/
68-
export function mutable_source(initial_value, owner) {
69-
const s = source(initial_value, owner);
63+
export function mutable_source(initial_value) {
64+
const s = source(initial_value);
7065
s.equals = safe_equals;
7166

7267
// bind the signal to the component context, in case we need to
@@ -78,6 +73,32 @@ export function mutable_source(initial_value, owner) {
7873
return s;
7974
}
8075

76+
/**
77+
* @template V
78+
* @param {V} v
79+
* @returns {Source<V>}
80+
*/
81+
export function mutable_state(v) {
82+
return push_derived_source(mutable_source(v));
83+
}
84+
85+
/**
86+
* @template V
87+
* @param {Source<V>} source
88+
*/
89+
/*#__NO_SIDE_EFFECTS__*/
90+
function push_derived_source(source) {
91+
if (current_reaction !== null && (current_reaction.f & DERIVED) !== 0) {
92+
if (derived_sources === null) {
93+
set_derived_sources([source]);
94+
} else {
95+
derived_sources.push(source);
96+
}
97+
}
98+
99+
return source;
100+
}
101+
81102
/**
82103
* @template V
83104
* @param {Value<V>} source

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { mutable_source, set } from './sources.js';
1919
export function store_get(store, store_name, stores) {
2020
const entry = (stores[store_name] ??= {
2121
store: null,
22-
source: mutable_source(undefined, null),
22+
source: mutable_source(undefined),
2323
unsubscribe: noop
2424
});
2525

0 commit comments

Comments
 (0)