-
-
Notifications
You must be signed in to change notification settings - Fork 5k
fix: avoid false positives for reactivity loss warning #18088
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7ec6e92
fix: avoid false positives for reactivity loss warning
dummdidumm 387e972
another test
dummdidumm d3415e0
another microtask
dummdidumm 12efbfb
add test that fails on 18089
Rich-Harris 3cf1c49
gather dependencies synchronously, and non-transitively (#18091)
Rich-Harris File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| 'svelte': patch | ||
| --- | ||
|
|
||
| fix: avoid false positives for reactivity loss warning |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...ages/svelte/tests/runtime-runes/samples/async-reactivity-loss-async-after-sync/_config.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import { tick } from 'svelte'; | ||
| import { test } from '../../test'; | ||
| import { normalise_trace_logs } from '../../../helpers.js'; | ||
|
|
||
| export default test({ | ||
| compileOptions: { | ||
| dev: true | ||
| }, | ||
|
|
||
| async test({ assert, target, warnings }) { | ||
| await new Promise((r) => setTimeout(r, 25)); | ||
|
|
||
| const [count] = target.querySelectorAll('button'); | ||
|
|
||
| count.click(); | ||
| await new Promise((r) => setTimeout(r, 25)); | ||
|
|
||
| assert.deepEqual(normalise_trace_logs(warnings), [ | ||
| { | ||
| log: 'Detected reactivity loss when reading `other`. This happens when state is read in an async function after an earlier `await`' | ||
| }, | ||
| { | ||
| log: 'Detected reactivity loss when reading `other`. This happens when state is read in an async function after an earlier `await`' | ||
| } | ||
| ]); | ||
| } | ||
| }); |
27 changes: 27 additions & 0 deletions
27
...ges/svelte/tests/runtime-runes/samples/async-reactivity-loss-async-after-sync/main.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| <script> | ||
| let count = $state(0); | ||
| let other = $state(0); | ||
|
|
||
| function delayed(value, ms = 1000) { | ||
| return new Promise((f) => setTimeout(() => f(value), ms)) | ||
| } | ||
|
|
||
| async function foo() { | ||
| await new Promise(r => setTimeout(r, 10)); | ||
| } | ||
|
|
||
| async function bar() { | ||
| const value = await delayed(count, 10); | ||
| other; // should trigger warning | ||
| return value; | ||
| } | ||
|
|
||
| async function get() { | ||
| foo(); | ||
| return await bar(); | ||
| } | ||
| </script> | ||
|
|
||
| <button onclick={() => count++}>{count}</button> | ||
| <button onclick={() => other++}>{other}</button> | ||
| {await get()} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
...s/svelte/tests/runtime-runes/samples/async-reactivity-loss-no-false-positive-1/_config.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import { tick } from 'svelte'; | ||
| import { test } from '../../test'; | ||
|
|
||
| export default test({ | ||
| compileOptions: { | ||
| dev: true | ||
| }, | ||
|
|
||
| async test({ assert, target, warnings }) { | ||
| await tick(); | ||
|
|
||
| const [x, y] = target.querySelectorAll('button'); | ||
|
|
||
| y.click(); | ||
| await tick(); | ||
| x.click(); | ||
| await new Promise((r) => setTimeout(r, 15)); | ||
|
|
||
| assert.equal(warnings.length, 0); | ||
| } | ||
| }); |
16 changes: 16 additions & 0 deletions
16
.../svelte/tests/runtime-runes/samples/async-reactivity-loss-no-false-positive-1/main.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <script> | ||
| let x = $state(0); | ||
| let y = $state(0); | ||
| async function foo(x) { | ||
| if (x) { | ||
| await 1; // restores reactivity loss warning context | ||
| await new Promise(r => setTimeout(r, 10)) // saves reactivity loss warning context; should not keep it while running | ||
| } | ||
| return x | ||
| } | ||
| </script> | ||
|
|
||
| {x} {await foo(y)} | ||
|
|
||
| <button onclick={() => x += 1}>x++</button> | ||
| <button onclick={() => y += 1}>y++</button> |
19 changes: 19 additions & 0 deletions
19
...s/svelte/tests/runtime-runes/samples/async-reactivity-loss-no-false-positive-2/_config.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import { tick } from 'svelte'; | ||
| import { test } from '../../test'; | ||
|
|
||
| export default test({ | ||
| compileOptions: { | ||
| dev: true | ||
| }, | ||
|
|
||
| async test({ assert, target, warnings }) { | ||
| await tick(); | ||
|
|
||
| const [count] = target.querySelectorAll('button'); | ||
|
|
||
| count.click(); | ||
| await tick(); | ||
|
|
||
| assert.equal(warnings.length, 0); | ||
| } | ||
| }); |
14 changes: 14 additions & 0 deletions
14
.../svelte/tests/runtime-runes/samples/async-reactivity-loss-no-false-positive-2/main.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <script> | ||
| let count = $state(0); | ||
| async function delay(c) { | ||
| if (c) { | ||
| await new Promise(r => setTimeout(r)); | ||
| count; // count already read synchronously; should not result in reacitive loss warning | ||
| } | ||
| return c; | ||
| } | ||
| </script> | ||
|
|
||
| {await delay(count)} | ||
|
|
||
| <button onclick={() => count += 1}>count++</button> |
19 changes: 19 additions & 0 deletions
19
...s/svelte/tests/runtime-runes/samples/async-reactivity-loss-no-false-positive-3/_config.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import { tick } from 'svelte'; | ||
| import { test } from '../../test'; | ||
|
|
||
| export default test({ | ||
| compileOptions: { | ||
| dev: true | ||
| }, | ||
|
|
||
| async test({ assert, target, warnings }) { | ||
| await new Promise((r) => setTimeout(r, 5)); | ||
|
|
||
| const [count] = target.querySelectorAll('button'); | ||
|
|
||
| count.click(); | ||
| await tick(); | ||
|
|
||
| assert.equal(warnings.length, 0); | ||
|
Rich-Harris marked this conversation as resolved.
|
||
| } | ||
| }); | ||
14 changes: 14 additions & 0 deletions
14
.../svelte/tests/runtime-runes/samples/async-reactivity-loss-no-false-positive-3/main.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <script> | ||
| let count = $state(0); | ||
|
|
||
| async function run() { | ||
| await new Promise(r => setTimeout(r)); | ||
| } | ||
| async function get() { | ||
| run(); | ||
| return 1; | ||
| } | ||
| </script> | ||
|
|
||
| <button onclick={() => count++}>{count}</button> | ||
| {await get()} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.