Skip to content

Commit de8053e

Browse files
committed
fix: use state instead of source in reactive classes
1 parent 777e67b commit de8053e

File tree

4 files changed

+41
-13
lines changed

4 files changed

+41
-13
lines changed

packages/svelte/src/motion/spring.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { writable } from '../store/shared/index.js';
55
import { loop } from '../internal/client/loop.js';
66
import { raf } from '../internal/client/timing.js';
77
import { is_date } from './utils.js';
8-
import { set, source } from '../internal/client/reactivity/sources.js';
8+
import { set, state } from '../internal/client/reactivity/sources.js';
99
import { render_effect } from '../internal/client/reactivity/effects.js';
1010
import { tag } from '../internal/client/dev/tracing.js';
1111
import { get } from '../internal/client/runtime.js';
@@ -170,9 +170,9 @@ export function spring(value, opts = {}) {
170170
* @since 5.8.0
171171
*/
172172
export class Spring {
173-
#stiffness = source(0.15);
174-
#damping = source(0.8);
175-
#precision = source(0.01);
173+
#stiffness = state(0.15);
174+
#damping = state(0.8);
175+
#precision = state(0.01);
176176

177177
#current;
178178
#target;
@@ -194,8 +194,8 @@ export class Spring {
194194
* @param {SpringOpts} [options]
195195
*/
196196
constructor(value, options = {}) {
197-
this.#current = DEV ? tag(source(value), 'Spring.current') : source(value);
198-
this.#target = DEV ? tag(source(value), 'Spring.target') : source(value);
197+
this.#current = DEV ? tag(state(value), 'Spring.current') : state(value);
198+
this.#target = DEV ? tag(state(value), 'Spring.target') : state(value);
199199

200200
if (typeof options.stiffness === 'number') this.#stiffness.v = clamp(options.stiffness, 0, 1);
201201
if (typeof options.damping === 'number') this.#damping.v = clamp(options.damping, 0, 1);

packages/svelte/src/motion/tweened.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { raf } from '../internal/client/timing.js';
66
import { loop } from '../internal/client/loop.js';
77
import { linear } from '../easing/index.js';
88
import { is_date } from './utils.js';
9-
import { set, source } from '../internal/client/reactivity/sources.js';
9+
import { set, state } from '../internal/client/reactivity/sources.js';
1010
import { tag } from '../internal/client/dev/tracing.js';
1111
import { get, render_effect } from 'svelte/internal/client';
1212
import { DEV } from 'esm-env';
@@ -191,8 +191,8 @@ export class Tween {
191191
* @param {TweenedOptions<T>} options
192192
*/
193193
constructor(value, options = {}) {
194-
this.#current = source(value);
195-
this.#target = source(value);
194+
this.#current = state(value);
195+
this.#target = state(value);
196196
this.#defaults = options;
197197

198198
if (DEV) {

packages/svelte/src/reactivity/map.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { DEV } from 'esm-env';
33
import { set, source, state } from '../internal/client/reactivity/sources.js';
44
import { label, tag } from '../internal/client/dev/tracing.js';
5-
import { get } from '../internal/client/runtime.js';
5+
import { get, push_reaction_value } from '../internal/client/runtime.js';
66
import { increment } from './utils.js';
77

88
/**
@@ -83,11 +83,13 @@ export class SvelteMap extends Map {
8383
has(key) {
8484
var sources = this.#sources;
8585
var s = sources.get(key);
86+
var is_new = false;
8687

8788
if (s === undefined) {
8889
var ret = super.get(key);
8990
if (ret !== undefined) {
9091
s = source(0);
92+
is_new = true;
9193

9294
if (DEV) {
9395
tag(s, `SvelteMap get(${label(key)})`);
@@ -103,6 +105,12 @@ export class SvelteMap extends Map {
103105
}
104106

105107
get(s);
108+
if (is_new) {
109+
// if it's a new source we want to push to the reactions values
110+
// AFTER we read it so that the effect depends on it but we don't
111+
// trigger an unsafe mutation (since it was created within the derived)
112+
push_reaction_value(s);
113+
}
106114
return true;
107115
}
108116

@@ -119,11 +127,13 @@ export class SvelteMap extends Map {
119127
get(key) {
120128
var sources = this.#sources;
121129
var s = sources.get(key);
130+
var is_new = false;
122131

123132
if (s === undefined) {
124133
var ret = super.get(key);
125134
if (ret !== undefined) {
126135
s = source(0);
136+
is_new = true;
127137

128138
if (DEV) {
129139
tag(s, `SvelteMap get(${label(key)})`);
@@ -139,6 +149,12 @@ export class SvelteMap extends Map {
139149
}
140150

141151
get(s);
152+
if (is_new) {
153+
// if it's a new source we want to push to the reactions values
154+
// AFTER we read it so that the effect depends on it but we don't
155+
// trigger an unsafe mutation (since it was created within the derived)
156+
push_reaction_value(s);
157+
}
142158
return super.get(key);
143159
}
144160

@@ -154,7 +170,7 @@ export class SvelteMap extends Map {
154170
var version = this.#version;
155171

156172
if (s === undefined) {
157-
s = source(0);
173+
s = state(0);
158174

159175
if (DEV) {
160176
tag(s, `SvelteMap get(${label(key)})`);
@@ -216,11 +232,12 @@ export class SvelteMap extends Map {
216232
get(this.#version);
217233

218234
var sources = this.#sources;
235+
var new_sources = new WeakSet();
219236
if (this.#size.v !== sources.size) {
220237
for (var key of super.keys()) {
221238
if (!sources.has(key)) {
222239
var s = source(0);
223-
240+
new_sources.add(s);
224241
if (DEV) {
225242
tag(s, `SvelteMap get(${label(key)})`);
226243
}
@@ -232,6 +249,12 @@ export class SvelteMap extends Map {
232249

233250
for ([, s] of this.#sources) {
234251
get(s);
252+
if (new_sources.has(s)) {
253+
// if it's a new source we want to push to the reactions values
254+
// AFTER we read it so that the effect depends on it but we don't
255+
// trigger an unsafe mutation (since it was created within the derived)
256+
push_reaction_value(s);
257+
}
235258
}
236259
}
237260

packages/svelte/src/reactivity/set.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { DEV } from 'esm-env';
33
import { source, set, state } from '../internal/client/reactivity/sources.js';
44
import { label, tag } from '../internal/client/dev/tracing.js';
5-
import { get } from '../internal/client/runtime.js';
5+
import { get, push_reaction_value } from '../internal/client/runtime.js';
66
import { increment } from './utils.js';
77

88
var read_methods = ['forEach', 'isDisjointFrom', 'isSubsetOf', 'isSupersetOf'];
@@ -107,6 +107,7 @@ export class SvelteSet extends Set {
107107
var has = super.has(value);
108108
var sources = this.#sources;
109109
var s = sources.get(value);
110+
var is_new = false;
110111

111112
if (s === undefined) {
112113
if (!has) {
@@ -117,6 +118,7 @@ export class SvelteSet extends Set {
117118
}
118119

119120
s = source(true);
121+
is_new = true;
120122

121123
if (DEV) {
122124
tag(s, `SvelteSet has(${label(value)})`);
@@ -126,6 +128,9 @@ export class SvelteSet extends Set {
126128
}
127129

128130
get(s);
131+
if (is_new) {
132+
push_reaction_value(s);
133+
}
129134
return has;
130135
}
131136

0 commit comments

Comments
 (0)