Skip to content

Commit 8a416a8

Browse files
committed
replace tag_if_necessary with conditional tagging
1 parent c6d0db4 commit 8a416a8

File tree

11 files changed

+138
-101
lines changed

11 files changed

+138
-101
lines changed

packages/svelte/src/motion/spring.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
import { writable } from '../store/shared/index.js';
55
import { loop } from '../internal/client/loop.js';
66
import { raf } from '../internal/client/timing.js';
7-
import { is_date, tag_if_necessary } from './utils.js';
7+
import { is_date } from './utils.js';
88
import { set, source } from '../internal/client/reactivity/sources.js';
99
import { render_effect } from '../internal/client/reactivity/effects.js';
10+
import { tag } from '../internal/client/dev/tracing.js';
1011
import { get } from '../internal/client/runtime.js';
1112
import { deferred, noop } from '../internal/shared/utils.js';
13+
import { DEV } from 'esm-env';
1214

1315
/**
1416
* @template T
@@ -168,12 +170,12 @@ export function spring(value, opts = {}) {
168170
* @since 5.8.0
169171
*/
170172
export class Spring {
171-
#stiffness = tag_if_necessary(source(0.15), 'Spring.stiffness');
172-
#damping = tag_if_necessary(source(0.8), 'Spring.damping');
173-
#precision = tag_if_necessary(source(0.01), 'Spring.precision');
173+
#stiffness = source(0.15);
174+
#damping = source(0.8);
175+
#precision = source(0.01);
174176

175-
#current = tag_if_necessary(source(/** @type {T} */ (undefined)), 'Spring.current');
176-
#target = tag_if_necessary(source(/** @type {T} */ (undefined)), 'Spring.target');
177+
#current;
178+
#target;
177179

178180
#last_value = /** @type {T} */ (undefined);
179181
#last_time = 0;
@@ -192,11 +194,20 @@ export class Spring {
192194
* @param {SpringOpts} [options]
193195
*/
194196
constructor(value, options = {}) {
195-
this.#current.v = this.#target.v = value;
197+
this.#current = DEV ? tag(source(value), 'Spring.current') : source(value);
198+
this.#target = DEV ? tag(source(value), 'Spring.target') : source(value);
196199

197200
if (typeof options.stiffness === 'number') this.#stiffness.v = clamp(options.stiffness, 0, 1);
198201
if (typeof options.damping === 'number') this.#damping.v = clamp(options.damping, 0, 1);
199202
if (typeof options.precision === 'number') this.#precision.v = options.precision;
203+
204+
if (DEV) {
205+
tag(this.#stiffness, 'Spring.stiffness');
206+
tag(this.#damping, 'Spring.damping');
207+
tag(this.#precision, 'Spring.precision');
208+
tag(this.#current, 'Spring.current');
209+
tag(this.#target, 'Spring.target');
210+
}
200211
}
201212

202213
/**

packages/svelte/src/motion/tweened.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ import { writable } from '../store/shared/index.js';
55
import { raf } from '../internal/client/timing.js';
66
import { loop } from '../internal/client/loop.js';
77
import { linear } from '../easing/index.js';
8-
import { is_date, tag_if_necessary } from './utils.js';
8+
import { is_date } from './utils.js';
99
import { set, source } from '../internal/client/reactivity/sources.js';
10+
import { tag } from '../internal/client/dev/tracing.js';
1011
import { get, render_effect } from 'svelte/internal/client';
12+
import { DEV } from 'esm-env';
1113

1214
/**
1315
* @template T
@@ -175,8 +177,8 @@ export function tweened(value, defaults = {}) {
175177
* @since 5.8.0
176178
*/
177179
export class Tween {
178-
#current = tag_if_necessary(source(/** @type {T} */ (undefined)), 'Tween.current');
179-
#target = tag_if_necessary(source(/** @type {T} */ (undefined)), 'Tween.target');
180+
#current;
181+
#target;
180182

181183
/** @type {TweenedOptions<T>} */
182184
#defaults;
@@ -189,8 +191,14 @@ export class Tween {
189191
* @param {TweenedOptions<T>} options
190192
*/
191193
constructor(value, options = {}) {
192-
this.#current.v = this.#target.v = value;
194+
this.#current = source(value);
195+
this.#target = source(value);
193196
this.#defaults = options;
197+
198+
if (DEV) {
199+
tag(this.#current, 'Tween.current');
200+
tag(this.#target, 'Tween.target');
201+
}
194202
}
195203

196204
/**

packages/svelte/src/motion/utils.js

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,7 @@
1-
/** @import { Source } from '#client' */
2-
import { DEV } from 'esm-env';
3-
import { tag } from '../internal/client/dev/tracing.js';
4-
51
/**
62
* @param {any} obj
73
* @returns {obj is Date}
84
*/
95
export function is_date(obj) {
106
return Object.prototype.toString.call(obj) === '[object Date]';
117
}
12-
13-
/**
14-
* @template {Source<any>} T
15-
* @param {T} source
16-
* @param {string} name
17-
* @returns {T}
18-
*/
19-
export function tag_if_necessary(source, name) {
20-
if (DEV) {
21-
return /** @type {T} */ (tag(source, name));
22-
}
23-
return source;
24-
}

packages/svelte/src/reactivity/create-subscriber.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { get, tick, untrack } from '../internal/client/runtime.js';
22
import { effect_tracking, render_effect } from '../internal/client/reactivity/effects.js';
33
import { source } from '../internal/client/reactivity/sources.js';
4-
import { increment, tag_if_necessary } from './utils.js';
4+
import { tag } from '../internal/client/dev/tracing.js';
5+
import { increment } from './utils.js';
6+
import { DEV } from 'esm-env';
57

68
/**
79
* Returns a `subscribe` function that, if called in an effect (including expressions in the template),
@@ -47,10 +49,14 @@ import { increment, tag_if_necessary } from './utils.js';
4749
*/
4850
export function createSubscriber(start) {
4951
let subscribers = 0;
50-
let version = tag_if_necessary(source(0), 'createSubscriber version');
52+
let version = source(0);
5153
/** @type {(() => void) | void} */
5254
let stop;
5355

56+
if (DEV) {
57+
tag(version, 'createSubscriber version');
58+
}
59+
5460
return () => {
5561
if (effect_tracking()) {
5662
get(version);

packages/svelte/src/reactivity/date.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
/** @import { Source } from '#client' */
22
import { derived } from '../internal/client/index.js';
33
import { source, set } from '../internal/client/reactivity/sources.js';
4+
import { tag } from '../internal/client/dev/tracing.js';
45
import { active_reaction, get, set_active_reaction } from '../internal/client/runtime.js';
5-
import { tag_if_necessary } from './utils.js';
6+
import { DEV } from 'esm-env';
67

78
var inited = false;
89

@@ -39,7 +40,7 @@ var inited = false;
3940
* ```
4041
*/
4142
export class SvelteDate extends Date {
42-
#time = tag_if_necessary(source(super.getTime()), 'SvelteDate.#time');
43+
#time = source(super.getTime());
4344

4445
/** @type {Map<keyof Date, Source<unknown>>} */
4546
#deriveds = new Map();
@@ -50,6 +51,11 @@ export class SvelteDate extends Date {
5051
constructor(...params) {
5152
// @ts-ignore
5253
super(...params);
54+
55+
if (DEV) {
56+
tag(this.#time, 'SvelteDate.#time');
57+
}
58+
5359
if (!inited) this.#init();
5460
}
5561

packages/svelte/src/reactivity/map.js

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
/** @import { Source } from '#client' */
22
import { DEV } from 'esm-env';
33
import { set, source } from '../internal/client/reactivity/sources.js';
4+
import { tag } from '../internal/client/dev/tracing.js';
45
import { get } from '../internal/client/runtime.js';
5-
import { increment, tag_if_necessary } from './utils.js';
6+
import { increment } from './utils.js';
67

78
/**
89
* A reactive version of the built-in [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) object.
@@ -53,17 +54,22 @@ import { increment, tag_if_necessary } from './utils.js';
5354
export class SvelteMap extends Map {
5455
/** @type {Map<K, Source<number>>} */
5556
#sources = new Map();
56-
#version = tag_if_necessary(source(0), 'SvelteMap version');
57-
#size = tag_if_necessary(source(0), 'SvelteMap.size');
57+
#version = source(0);
58+
#size = source(0);
5859

5960
/**
6061
* @param {Iterable<readonly [K, V]> | null | undefined} [value]
6162
*/
6263
constructor(value) {
6364
super();
6465

65-
// If the value is invalid then the native exception will fire here
66-
if (DEV) value = new Map(value);
66+
if (DEV) {
67+
// If the value is invalid then the native exception will fire here
68+
value = new Map(value);
69+
70+
tag(this.#version, 'SvelteMap version');
71+
tag(this.#size, 'SvelteMap.size');
72+
}
6773

6874
if (value) {
6975
for (var [key, v] of value) {
@@ -81,10 +87,13 @@ export class SvelteMap extends Map {
8187
if (s === undefined) {
8288
var ret = super.get(key);
8389
if (ret !== undefined) {
84-
s = tag_if_necessary(
85-
source(0),
86-
`SvelteMap Entry [${typeof key === 'symbol' ? `Symbol(${key.description})` : key}]`
87-
);
90+
s = source(0);
91+
92+
if (DEV) {
93+
var label = `SvelteMap Entry [${typeof key === 'symbol' ? `Symbol(${key.description})` : key}]`;
94+
tag(s, label);
95+
}
96+
8897
sources.set(key, s);
8998
} else {
9099
// We should always track the version in case
@@ -115,10 +124,13 @@ export class SvelteMap extends Map {
115124
if (s === undefined) {
116125
var ret = super.get(key);
117126
if (ret !== undefined) {
118-
s = tag_if_necessary(
119-
source(0),
120-
`SvelteMap Entry [${typeof key === 'symbol' ? `Symbol(${key.description})` : key}]`
121-
);
127+
s = source(0);
128+
129+
if (DEV) {
130+
var label = `SvelteMap Entry [${typeof key === 'symbol' ? `Symbol(${key.description})` : key}]`;
131+
tag(s, label);
132+
}
133+
122134
sources.set(key, s);
123135
} else {
124136
// We should always track the version in case
@@ -144,13 +156,14 @@ export class SvelteMap extends Map {
144156
var version = this.#version;
145157

146158
if (s === undefined) {
147-
sources.set(
148-
key,
149-
tag_if_necessary(
150-
source(0),
151-
`SvelteMap Entry [${typeof key === 'symbol' ? `Symbol(${key.description})` : key}]`
152-
)
153-
);
159+
s = source(0);
160+
161+
if (DEV) {
162+
var label = `SvelteMap Entry [${typeof key === 'symbol' ? `Symbol(${key.description})` : key}]`;
163+
tag(s, label);
164+
}
165+
166+
sources.set(key, s);
154167
set(this.#size, super.size);
155168
increment(version);
156169
} else if (prev_res !== value) {
@@ -209,18 +222,19 @@ export class SvelteMap extends Map {
209222
if (this.#size.v !== sources.size) {
210223
for (var key of super.keys()) {
211224
if (!sources.has(key)) {
212-
sources.set(
213-
key,
214-
tag_if_necessary(
215-
source(0),
216-
`SvelteMap Entry [${typeof key === 'symbol' ? `Symbol(${key.description})` : key}]`
217-
)
218-
);
225+
var s = source(0);
226+
227+
if (DEV) {
228+
var label = `SvelteMap Entry [${typeof key === 'symbol' ? `Symbol(${key.description})` : key}]`;
229+
tag(s, label);
230+
}
231+
232+
sources.set(key, s);
219233
}
220234
}
221235
}
222236

223-
for (var [, s] of this.#sources) {
237+
for ([, s] of this.#sources) {
224238
get(s);
225239
}
226240
}

packages/svelte/src/reactivity/set.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
/** @import { Source } from '#client' */
22
import { DEV } from 'esm-env';
33
import { source, set } from '../internal/client/reactivity/sources.js';
4+
import { tag } from '../internal/client/dev/tracing.js';
45
import { get } from '../internal/client/runtime.js';
5-
import { increment, tag_if_necessary } from './utils.js';
6+
import { increment } from './utils.js';
67

78
var read_methods = ['forEach', 'isDisjointFrom', 'isSubsetOf', 'isSupersetOf'];
89
var set_like_methods = ['difference', 'intersection', 'symmetricDifference', 'union'];
@@ -47,17 +48,22 @@ var inited = false;
4748
export class SvelteSet extends Set {
4849
/** @type {Map<T, Source<boolean>>} */
4950
#sources = new Map();
50-
#version = tag_if_necessary(source(0), 'SvelteSet version');
51-
#size = tag_if_necessary(source(0), 'SvelteSet.size');
51+
#version = source(0);
52+
#size = source(0);
5253

5354
/**
5455
* @param {Iterable<T> | null | undefined} [value]
5556
*/
5657
constructor(value) {
5758
super();
5859

59-
// If the value is invalid then the native exception will fire here
60-
if (DEV) value = new Set(value);
60+
if (DEV) {
61+
// If the value is invalid then the native exception will fire here
62+
value = new Set(value);
63+
64+
tag(this.#version, 'SvelteSet version');
65+
tag(this.#size, 'SvelteSet.size');
66+
}
6167

6268
if (value) {
6369
for (var element of value) {
@@ -110,10 +116,13 @@ export class SvelteSet extends Set {
110116
return false;
111117
}
112118

113-
s = tag_if_necessary(
114-
source(true),
115-
`SvelteSet Entry [${typeof value === 'symbol' ? `Symbol(${value.description})` : value}]`
116-
);
119+
s = source(true);
120+
121+
if (DEV) {
122+
var label = `SvelteSet Entry [${typeof value === 'symbol' ? `Symbol(${value.description})` : value}]`;
123+
tag(s, label);
124+
}
125+
117126
sources.set(value, s);
118127
}
119128

packages/svelte/src/reactivity/url-search-params.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import { DEV } from 'esm-env';
12
import { source } from '../internal/client/reactivity/sources.js';
3+
import { tag } from '../internal/client/dev/tracing.js';
24
import { get } from '../internal/client/runtime.js';
35
import { get_current_url } from './url.js';
4-
import { increment, tag_if_necessary } from './utils.js';
6+
import { increment } from './utils.js';
57

68
export const REPLACE = Symbol();
79

@@ -32,7 +34,7 @@ export const REPLACE = Symbol();
3234
* ```
3335
*/
3436
export class SvelteURLSearchParams extends URLSearchParams {
35-
#version = tag_if_necessary(source(0), 'SvelteURLSearchParams version');
37+
#version = DEV ? tag(source(0), 'SvelteURLSearchParams version') : source(0);
3638
#url = get_current_url();
3739

3840
#updating = false;

0 commit comments

Comments
 (0)