Skip to content

Commit

Permalink
fix: ensure effect_tracking correctly handles tracking reactions (#14005
Browse files Browse the repository at this point in the history
)

* fix: ensure effect_tracking correctly handles tracking reactions

* fix: ensure effect_tracking correctly handles tracking reactions
  • Loading branch information
trueadm authored Oct 28, 2024
1 parent 777d059 commit 3876b30
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/clean-badgers-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: ensure effect_tracking correctly handles tracking reactions
7 changes: 5 additions & 2 deletions packages/svelte/src/internal/client/reactivity/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import {
set_is_destroying_effect,
set_is_flushing_effect,
set_signal_status,
untrack
untrack,
skip_reaction
} from '../runtime.js';
import {
DIRTY,
Expand Down Expand Up @@ -167,7 +168,9 @@ export function effect_tracking() {
return false;
}

return (active_reaction.f & UNOWNED) === 0;
// If it's skipped, that's because we're inside an unowned
// that is not being tracked by another reaction
return !skip_reaction;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { flushSync } from 'svelte';
import { test } from '../../test';

export default test({
async test({ assert, target, logs }) {
let btn1 = target.querySelector('button');

btn1?.click();
flushSync();

btn1?.click();
flushSync();

btn1?.click();
flushSync();

assert.deepEqual(logs, ['light', 'dark', 'light']);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<script>
import { store, themeState } from './theme.svelte.js';
let i = 0;
const increment = () => {
store.update(() => ({ theme: ++i % 2 == 0 ? 'dark' : 'light' }));
}
</script>

<button onclick={increment}>+</button>

{themeState.value.theme}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { fromStore, writable } from 'svelte/store';

export const store = writable({ theme: 'dark' });

class ThemeState {
#storeState = fromStore(store);
value = $derived(this.#storeState.current);

constructor() {
$effect.root(() => {
$effect(() => {
console.log(this.value.theme);
});
});
}
}

export const themeState = new ThemeState();

0 comments on commit 3876b30

Please sign in to comment.