Skip to content

make internal sources ownerless #13013

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 10 commits into from
Aug 24, 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
5 changes: 5 additions & 0 deletions .changeset/six-moons-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: make internal sources ownerless
2 changes: 1 addition & 1 deletion benchmarking/benchmarks/kairo/kairo_avoidable.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as $ from '../../../packages/svelte/src/internal/client/index.js';
import { busy } from './util.js';

function setup() {
let head = $.source(0);
let head = $.state(0);
let computed1 = $.derived(() => $.get(head));
let computed2 = $.derived(() => ($.get(computed1), 0));
let computed3 = $.derived(() => (busy(), $.get(computed2) + 1)); // heavy computation
Expand Down
2 changes: 1 addition & 1 deletion benchmarking/benchmarks/kairo/kairo_broad.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { assert, fastest_test } from '../../utils.js';
import * as $ from '../../../packages/svelte/src/internal/client/index.js';

function setup() {
let head = $.source(0);
let head = $.state(0);
let last = head;
let counter = 0;

Expand Down
2 changes: 1 addition & 1 deletion benchmarking/benchmarks/kairo/kairo_deep.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ let len = 50;
const iter = 50;

function setup() {
let head = $.source(0);
let head = $.state(0);
let current = head;
for (let i = 0; i < len; i++) {
let c = current;
Expand Down
2 changes: 1 addition & 1 deletion benchmarking/benchmarks/kairo/kairo_diamond.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as $ from '../../../packages/svelte/src/internal/client/index.js';
let width = 5;

function setup() {
let head = $.source(0);
let head = $.state(0);
let current = [];
for (let i = 0; i < width; i++) {
current.push(
Expand Down
2 changes: 1 addition & 1 deletion benchmarking/benchmarks/kairo/kairo_mux.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { assert, fastest_test } from '../../utils.js';
import * as $ from '../../../packages/svelte/src/internal/client/index.js';

function setup() {
let heads = new Array(100).fill(null).map((_) => $.source(0));
let heads = new Array(100).fill(null).map((_) => $.state(0));
const mux = $.derived(() => {
return Object.fromEntries(heads.map((h) => $.get(h)).entries());
});
Expand Down
2 changes: 1 addition & 1 deletion benchmarking/benchmarks/kairo/kairo_repeated.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as $ from '../../../packages/svelte/src/internal/client/index.js';
let size = 30;

function setup() {
let head = $.source(0);
let head = $.state(0);
let current = $.derived(() => {
let result = 0;
for (let i = 0; i < size; i++) {
Expand Down
2 changes: 1 addition & 1 deletion benchmarking/benchmarks/kairo/kairo_triangle.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function count(number) {
}

function setup() {
let head = $.source(0);
let head = $.state(0);
let current = head;
let list = [];
for (let i = 0; i < width; i++) {
Expand Down
2 changes: 1 addition & 1 deletion benchmarking/benchmarks/kairo/kairo_unstable.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { assert, fastest_test } from '../../utils.js';
import * as $ from '../../../packages/svelte/src/internal/client/index.js';

function setup() {
let head = $.source(0);
let head = $.state(0);
const double = $.derived(() => $.get(head) * 2);
const inverse = $.derived(() => -$.get(head));
let current = $.derived(() => {
Expand Down
4 changes: 2 additions & 2 deletions benchmarking/benchmarks/mol_bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ const numbers = Array.from({ length: 5 }, (_, i) => i);

function setup() {
let res = [];
const A = $.source(0);
const B = $.source(0);
const A = $.state(0);
const B = $.state(0);
const C = $.derived(() => ($.get(A) % 2) + ($.get(B) % 2));
const D = $.derived(() => numbers.map((i) => i + ($.get(A) % 2) - ($.get(B) % 2)));
D.equals = function (/** @type {number[]} */ l) {
Expand Down
2 changes: 1 addition & 1 deletion benchmarking/benchmarks/sbench.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const COUNT = 1e5;
*/
function create_data_signals(n, sources) {
for (let i = 0; i < n; i++) {
sources[i] = $.source(i);
sources[i] = $.state(i);
}
return sources;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ export function client_component(analysis, options) {

for (const [name, binding] of analysis.instance.scope.declarations) {
if (binding.kind === 'legacy_reactive') {
legacy_reactive_declarations.push(b.const(name, b.call('$.mutable_source')));
legacy_reactive_declarations.push(b.const(name, b.call('$.mutable_state')));
}
if (binding.kind === 'store_sub') {
if (store_setup.length === 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,17 @@ export function ClassBody(node, context) {
value =
field.kind === 'state'
? b.call(
'$.source',
'$.state',
should_proxy(init, context.state.scope) ? b.call('$.proxy', init) : init
)
: field.kind === 'raw_state'
? b.call('$.source', init)
? b.call('$.state', init)
: field.kind === 'derived_by'
? b.call('$.derived', init)
: b.call('$.derived', b.thunk(init));
} else {
// if no arguments, we know it's state as `$derived()` is a compile error
value = b.call('$.source');
value = b.call('$.state');
}

if (is_private) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export function VariableDeclaration(node, context) {
value = b.call('$.proxy', value);
}
if (is_state_source(binding, context.state.analysis)) {
value = b.call('$.source', value);
value = b.call('$.state', value);
}
return value;
};
Expand Down Expand Up @@ -291,7 +291,7 @@ export function VariableDeclaration(node, context) {
*/
function create_state_declarators(declarator, scope, value) {
if (declarator.id.type === 'Identifier') {
return [b.declarator(declarator.id, b.call('$.mutable_source', value))];
return [b.declarator(declarator.id, b.call('$.mutable_state', value))];
}

const tmp = scope.generate('tmp');
Expand All @@ -303,7 +303,7 @@ function create_state_declarators(declarator, scope, value) {
const binding = scope.get(/** @type {Identifier} */ (path.node).name);
return b.declarator(
path.node,
binding?.kind === 'state' ? b.call('$.mutable_source', value) : value
binding?.kind === 'state' ? b.call('$.mutable_state', value) : value
);
})
];
Expand Down
6 changes: 2 additions & 4 deletions packages/svelte/src/internal/client/dom/blocks/await.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,8 @@ export function await_block(node, get_input, pending_fn, then_fn, catch_fn) {
/** @type {Effect | null} */
var catch_effect;

var input_source = runes
? source(/** @type {V} */ (undefined))
: mutable_source(/** @type {V} */ (undefined));
var error_source = runes ? source(undefined) : mutable_source(undefined);
var input_source = (runes ? source : mutable_source)(/** @type {V} */ (undefined));
var error_source = (runes ? source : mutable_source)(undefined);
var resolved = false;

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte/src/internal/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export {
user_effect,
user_pre_effect
} from './reactivity/effects.js';
export { mutable_source, mutate, source, set } from './reactivity/sources.js';
export { mutable_state, mutate, set, state } from './reactivity/sources.js';
export {
prop,
rest_props,
Expand Down
4 changes: 2 additions & 2 deletions packages/svelte/src/internal/client/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export function proxy(value, parent = null, prev) {

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

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

Expand Down
53 changes: 37 additions & 16 deletions packages/svelte/src/internal/client/reactivity/sources.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,39 +34,34 @@ let inspect_effects = new Set();
/**
* @template V
* @param {V} v
* @param {Reaction | null} [owner]
* @returns {Source<V>}
*/
/*#__NO_SIDE_EFFECTS__*/
export function source(v, owner = current_reaction) {
var source = {
export function source(v) {
return {
f: 0, // TODO ideally we could skip this altogether, but it causes type errors
v,
reactions: null,
equals,
version: 0
};
}

if (owner !== null && (owner.f & DERIVED) !== 0) {
if (derived_sources === null) {
set_derived_sources([source]);
} else {
derived_sources.push(source);
}
}

return source;
/**
* @template V
* @param {V} v
*/
export function state(v) {
return push_derived_source(source(v));
}

/**
* @template V
* @param {V} initial_value
* @param {Reaction | null} [owner]
* @returns {Source<V>}
*/
/*#__NO_SIDE_EFFECTS__*/
export function mutable_source(initial_value, owner) {
const s = source(initial_value, owner);
export function mutable_source(initial_value) {
const s = source(initial_value);
s.equals = safe_equals;

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

/**
* @template V
* @param {V} v
* @returns {Source<V>}
*/
export function mutable_state(v) {
return push_derived_source(mutable_source(v));
}

/**
* @template V
* @param {Source<V>} source
*/
/*#__NO_SIDE_EFFECTS__*/
function push_derived_source(source) {
if (current_reaction !== null && (current_reaction.f & DERIVED) !== 0) {
if (derived_sources === null) {
set_derived_sources([source]);
} else {
derived_sources.push(source);
}
}

return source;
}

/**
* @template V
* @param {Value<V>} source
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte/src/internal/client/reactivity/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { mutable_source, set } from './sources.js';
export function store_get(store, store_name, stores) {
const entry = (stores[store_name] ??= {
store: null,
source: mutable_source(undefined, null),
source: mutable_source(undefined),
unsubscribe: noop
});

Expand Down
Loading
Loading