Skip to content

Commit

Permalink
chore: make reactions a Set
Browse files Browse the repository at this point in the history
  • Loading branch information
paoloricciuti committed Oct 26, 2024
1 parent 290dfa5 commit f156fdb
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 22 deletions.
5 changes: 2 additions & 3 deletions packages/svelte/src/internal/client/reactivity/sources.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,9 @@ function mark_reactions(signal, status) {
if (reactions === null) return;

var runes = is_runes();
var length = reactions.length;
var length = reactions.size;

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

// Skip any effects that are already dirty
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte/src/internal/client/reactivity/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface Signal {

export interface Value<V = unknown> extends Signal {
/** Signals that read from this signal */
reactions: null | Reaction[];
reactions: null | Set<Reaction>;
/** Equality function */
equals: Equals;
/** The latest value for this signal */
Expand Down
16 changes: 7 additions & 9 deletions packages/svelte/src/internal/client/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ export function check_dirtiness(reaction) {

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

reaction.f ^= DISCONNECTED;
Expand All @@ -207,9 +207,9 @@ export function check_dirtiness(reaction) {
is_unowned &&
active_effect !== null &&
!skip_reaction &&
!dependency?.reactions?.includes(reaction)
!dependency?.reactions?.has(reaction)
) {
(dependency.reactions ??= []).push(reaction);
(dependency.reactions ??= new Set()).add(reaction);
}

if (dependency.version > reaction.version) {
Expand Down Expand Up @@ -333,7 +333,7 @@ export function update_reaction(reaction) {

if (!skip_reaction) {
for (i = skipped_deps; i < deps.length; i++) {
(deps[i].reactions ??= []).push(reaction);
(deps[i].reactions ??= new Set()).add(reaction);
}
}
} else if (deps !== null && skipped_deps < deps.length) {
Expand Down Expand Up @@ -362,15 +362,13 @@ export function update_reaction(reaction) {
function remove_reaction(signal, dependency) {
let reactions = dependency.reactions;
if (reactions !== null) {
var index = reactions.indexOf(signal);
if (index !== -1) {
var new_length = reactions.length - 1;
if (reactions.has(signal)) {
var new_length = reactions.size - 1;
if (new_length === 0) {
reactions = dependency.reactions = null;
} else {
// Swap with last element and then remove.
reactions[index] = reactions[new_length];
reactions.pop();
reactions.delete(signal);
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion packages/svelte/src/reactivity/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ export class SvelteMap extends Map {
increment(s);

// if not every reaction of s is a reaction of version we need to also include version
const needs_version_increase = !s.reactions?.every((r) => version.reactions?.includes(r));
const needs_version_increase =
version.reactions !== null &&
!(s.reactions === null ? [] : [...s.reactions.values()]).every((reaction) =>
version.reactions?.has(reaction)
);
if (needs_version_increase) {
increment(version);
}
Expand Down
16 changes: 8 additions & 8 deletions packages/svelte/tests/signals/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,13 +238,13 @@ describe('signals', () => {
return () => {
flushSync(() => set(count, 1));
// Ensure we're not leaking consumers
assert.deepEqual(count.reactions?.length, 1);
assert.deepEqual(count.reactions?.size, 1);
flushSync(() => set(count, 2));
// Ensure we're not leaking consumers
assert.deepEqual(count.reactions?.length, 1);
assert.deepEqual(count.reactions?.size, 1);
flushSync(() => set(count, 3));
// Ensure we're not leaking consumers
assert.deepEqual(count.reactions?.length, 1);
assert.deepEqual(count.reactions?.size, 1);
assert.deepEqual(log, [0, 1, 2, 3]);
};
});
Expand Down Expand Up @@ -306,8 +306,8 @@ describe('signals', () => {
flushSync(() => set(count, 4));
flushSync(() => set(count, 0));
// Ensure we're not leaking consumers
assert.deepEqual(count.reactions?.length, 2);
assert.deepEqual(calc.reactions?.length, 1);
assert.deepEqual(count.reactions?.size, 2);
assert.deepEqual(calc.reactions?.size, 1);
assert.deepEqual(log, [0, 2, 'limit', 0]);
destroy();
// Ensure we're not leaking consumers
Expand Down Expand Up @@ -486,7 +486,7 @@ describe('signals', () => {
return () => {
flushSync();
assert.equal(a?.deps?.length, 1);
assert.equal(s?.reactions?.length, 1);
assert.equal(s?.reactions?.size, 1);
destroy();
assert.equal(s?.reactions, null);
};
Expand Down Expand Up @@ -659,12 +659,12 @@ describe('signals', () => {
});

assert.equal($.get(d), 0);
assert.equal(count.reactions?.length, 1);
assert.equal(count.reactions?.size, 1);
assert.equal(d.deps?.length, 1);

set(count, 1);
assert.equal($.get(d), 2);
assert.equal(count.reactions?.length, 1);
assert.equal(count.reactions?.size, 1);
assert.equal(d.deps?.length, 1);

destroy();
Expand Down

0 comments on commit f156fdb

Please sign in to comment.