Skip to content

Commit 84b2618

Browse files
authored
chore: lift "flushSync cannot be called in effects" restriction (#17139)
Since async-await was introduced into the code base a lot has changed. This lifts the restriction. Closes #17131 (though I still wonder why Skeleton does that)
1 parent ea8838e commit 84b2618

File tree

6 files changed

+6
-44
lines changed

6 files changed

+6
-44
lines changed

.changeset/heavy-lions-tap.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
chore: lift "flushSync cannot be called in effects" restriction

documentation/docs/98-reference/.generated/client-errors.md

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -136,16 +136,6 @@ Often when encountering this issue, the value in question shouldn't be state (fo
136136
Cannot use `fork(...)` unless the `experimental.async` compiler option is `true`
137137
```
138138

139-
### flush_sync_in_effect
140-
141-
```
142-
Cannot use `flushSync` inside an effect
143-
```
144-
145-
The `flushSync()` function can be used to flush any pending effects synchronously. It cannot be used if effects are currently being flushed — in other words, you can call it after a state change but _not_ inside an effect.
146-
147-
This restriction only applies when using the `experimental.async` option, which will be active by default in Svelte 6.
148-
149139
### fork_discarded
150140

151141
```

packages/svelte/messages/client-errors/errors.md

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,6 @@ Often when encountering this issue, the value in question shouldn't be state (fo
104104

105105
> Cannot use `fork(...)` unless the `experimental.async` compiler option is `true`
106106
107-
## flush_sync_in_effect
108-
109-
> Cannot use `flushSync` inside an effect
110-
111-
The `flushSync()` function can be used to flush any pending effects synchronously. It cannot be used if effects are currently being flushed — in other words, you can call it after a state change but _not_ inside an effect.
112-
113-
This restriction only applies when using the `experimental.async` option, which will be active by default in Svelte 6.
114-
115107
## fork_discarded
116108

117109
> Cannot commit a fork that was already discarded

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

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -245,22 +245,6 @@ export function experimental_async_fork() {
245245
}
246246
}
247247

248-
/**
249-
* Cannot use `flushSync` inside an effect
250-
* @returns {never}
251-
*/
252-
export function flush_sync_in_effect() {
253-
if (DEV) {
254-
const error = new Error(`flush_sync_in_effect\nCannot use \`flushSync\` inside an effect\nhttps://svelte.dev/e/flush_sync_in_effect`);
255-
256-
error.name = 'Svelte error';
257-
258-
throw error;
259-
} else {
260-
throw new Error(`https://svelte.dev/e/flush_sync_in_effect`);
261-
}
262-
}
263-
264248
/**
265249
* Cannot commit a fork that was already discarded
266250
* @returns {never}

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -563,11 +563,6 @@ export class Batch {
563563
* @returns {T}
564564
*/
565565
export function flushSync(fn) {
566-
if (async_mode_flag && active_effect !== null) {
567-
// We disallow this because it creates super-hard to reason about stack trace and because it's generally a bad idea
568-
e.flush_sync_in_effect();
569-
}
570-
571566
var was_flushing_sync = is_flushing_sync;
572567
is_flushing_sync = true;
573568

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
import { async_mode } from '../../../helpers';
21
import { test } from '../../test';
32

43
export default test({
5-
// In legacy mode this succeeds and logs 'hello'
6-
// In async mode this throws an error because flushSync is called inside an effect
74
async test({ assert, target, logs }) {
85
assert.htmlEqual(target.innerHTML, `<button>show</button> <div>hello</div>`);
96
assert.deepEqual(logs, ['hello']);
10-
},
11-
runtime_error: async_mode ? 'flush_sync_in_effect' : undefined
7+
}
128
});

0 commit comments

Comments
 (0)