Skip to content

Commit f156fdb

Browse files
committed
chore: make reactions a Set
1 parent 290dfa5 commit f156fdb

File tree

5 files changed

+23
-22
lines changed

5 files changed

+23
-22
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,9 @@ function mark_reactions(signal, status) {
219219
if (reactions === null) return;
220220

221221
var runes = is_runes();
222-
var length = reactions.length;
222+
var length = reactions.size;
223223

224-
for (var i = 0; i < length; i++) {
225-
var reaction = reactions[i];
224+
for (var reaction of reactions) {
226225
var flags = reaction.f;
227226

228227
// Skip any effects that are already dirty

packages/svelte/src/internal/client/reactivity/types.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export interface Signal {
99

1010
export interface Value<V = unknown> extends Signal {
1111
/** Signals that read from this signal */
12-
reactions: null | Reaction[];
12+
reactions: null | Set<Reaction>;
1313
/** Equality function */
1414
equals: Equals;
1515
/** The latest value for this signal */

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ export function check_dirtiness(reaction) {
187187

188188
if ((flags & DISCONNECTED) !== 0) {
189189
for (i = 0; i < dependencies.length; i++) {
190-
(dependencies[i].reactions ??= []).push(reaction);
190+
(dependencies[i].reactions ??= new Set()).add(reaction);
191191
}
192192

193193
reaction.f ^= DISCONNECTED;
@@ -207,9 +207,9 @@ export function check_dirtiness(reaction) {
207207
is_unowned &&
208208
active_effect !== null &&
209209
!skip_reaction &&
210-
!dependency?.reactions?.includes(reaction)
210+
!dependency?.reactions?.has(reaction)
211211
) {
212-
(dependency.reactions ??= []).push(reaction);
212+
(dependency.reactions ??= new Set()).add(reaction);
213213
}
214214

215215
if (dependency.version > reaction.version) {
@@ -333,7 +333,7 @@ export function update_reaction(reaction) {
333333

334334
if (!skip_reaction) {
335335
for (i = skipped_deps; i < deps.length; i++) {
336-
(deps[i].reactions ??= []).push(reaction);
336+
(deps[i].reactions ??= new Set()).add(reaction);
337337
}
338338
}
339339
} else if (deps !== null && skipped_deps < deps.length) {
@@ -362,15 +362,13 @@ export function update_reaction(reaction) {
362362
function remove_reaction(signal, dependency) {
363363
let reactions = dependency.reactions;
364364
if (reactions !== null) {
365-
var index = reactions.indexOf(signal);
366-
if (index !== -1) {
367-
var new_length = reactions.length - 1;
365+
if (reactions.has(signal)) {
366+
var new_length = reactions.size - 1;
368367
if (new_length === 0) {
369368
reactions = dependency.reactions = null;
370369
} else {
371370
// Swap with last element and then remove.
372-
reactions[index] = reactions[new_length];
373-
reactions.pop();
371+
reactions.delete(signal);
374372
}
375373
}
376374
}

packages/svelte/src/reactivity/map.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,11 @@ export class SvelteMap extends Map {
105105
increment(s);
106106

107107
// if not every reaction of s is a reaction of version we need to also include version
108-
const needs_version_increase = !s.reactions?.every((r) => version.reactions?.includes(r));
108+
const needs_version_increase =
109+
version.reactions !== null &&
110+
!(s.reactions === null ? [] : [...s.reactions.values()]).every((reaction) =>
111+
version.reactions?.has(reaction)
112+
);
109113
if (needs_version_increase) {
110114
increment(version);
111115
}

packages/svelte/tests/signals/test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -238,13 +238,13 @@ describe('signals', () => {
238238
return () => {
239239
flushSync(() => set(count, 1));
240240
// Ensure we're not leaking consumers
241-
assert.deepEqual(count.reactions?.length, 1);
241+
assert.deepEqual(count.reactions?.size, 1);
242242
flushSync(() => set(count, 2));
243243
// Ensure we're not leaking consumers
244-
assert.deepEqual(count.reactions?.length, 1);
244+
assert.deepEqual(count.reactions?.size, 1);
245245
flushSync(() => set(count, 3));
246246
// Ensure we're not leaking consumers
247-
assert.deepEqual(count.reactions?.length, 1);
247+
assert.deepEqual(count.reactions?.size, 1);
248248
assert.deepEqual(log, [0, 1, 2, 3]);
249249
};
250250
});
@@ -306,8 +306,8 @@ describe('signals', () => {
306306
flushSync(() => set(count, 4));
307307
flushSync(() => set(count, 0));
308308
// Ensure we're not leaking consumers
309-
assert.deepEqual(count.reactions?.length, 2);
310-
assert.deepEqual(calc.reactions?.length, 1);
309+
assert.deepEqual(count.reactions?.size, 2);
310+
assert.deepEqual(calc.reactions?.size, 1);
311311
assert.deepEqual(log, [0, 2, 'limit', 0]);
312312
destroy();
313313
// Ensure we're not leaking consumers
@@ -486,7 +486,7 @@ describe('signals', () => {
486486
return () => {
487487
flushSync();
488488
assert.equal(a?.deps?.length, 1);
489-
assert.equal(s?.reactions?.length, 1);
489+
assert.equal(s?.reactions?.size, 1);
490490
destroy();
491491
assert.equal(s?.reactions, null);
492492
};
@@ -659,12 +659,12 @@ describe('signals', () => {
659659
});
660660

661661
assert.equal($.get(d), 0);
662-
assert.equal(count.reactions?.length, 1);
662+
assert.equal(count.reactions?.size, 1);
663663
assert.equal(d.deps?.length, 1);
664664

665665
set(count, 1);
666666
assert.equal($.get(d), 2);
667-
assert.equal(count.reactions?.length, 1);
667+
assert.equal(count.reactions?.size, 1);
668668
assert.equal(d.deps?.length, 1);
669669

670670
destroy();

0 commit comments

Comments
 (0)