chore: lift "flushSync cannot be called in effects" restriction

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)
lift-flushsync-restriction
Simon Holthausen 1 week ago
parent e238e6611e
commit cf376b4fb9

@ -0,0 +1,5 @@
---
'svelte': patch
---
chore: lift "flushSync cannot be called in effects" restriction

@ -136,16 +136,6 @@ Often when encountering this issue, the value in question shouldn't be state (fo
Cannot use `fork(...)` unless the `experimental.async` compiler option is `true`
```
### flush_sync_in_effect
```
Cannot use `flushSync` inside an effect
```
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.
This restriction only applies when using the `experimental.async` option, which will be active by default in Svelte 6.
### fork_discarded
```

@ -104,14 +104,6 @@ Often when encountering this issue, the value in question shouldn't be state (fo
> Cannot use `fork(...)` unless the `experimental.async` compiler option is `true`
## flush_sync_in_effect
> Cannot use `flushSync` inside an effect
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.
This restriction only applies when using the `experimental.async` option, which will be active by default in Svelte 6.
## fork_discarded
> Cannot commit a fork that was already discarded

@ -245,22 +245,6 @@ export function experimental_async_fork() {
}
}
/**
* Cannot use `flushSync` inside an effect
* @returns {never}
*/
export function flush_sync_in_effect() {
if (DEV) {
const error = new Error(`flush_sync_in_effect\nCannot use \`flushSync\` inside an effect\nhttps://svelte.dev/e/flush_sync_in_effect`);
error.name = 'Svelte error';
throw error;
} else {
throw new Error(`https://svelte.dev/e/flush_sync_in_effect`);
}
}
/**
* Cannot commit a fork that was already discarded
* @returns {never}

@ -528,11 +528,6 @@ export class Batch {
* @returns {T}
*/
export function flushSync(fn) {
if (async_mode_flag && active_effect !== null) {
// We disallow this because it creates super-hard to reason about stack trace and because it's generally a bad idea
e.flush_sync_in_effect();
}
var was_flushing_sync = is_flushing_sync;
is_flushing_sync = true;

@ -1,12 +1,8 @@
import { async_mode } from '../../../helpers';
import { test } from '../../test';
export default test({
// In legacy mode this succeeds and logs 'hello'
// In async mode this throws an error because flushSync is called inside an effect
async test({ assert, target, logs }) {
assert.htmlEqual(target.innerHTML, `<button>show</button> <div>hello</div>`);
assert.deepEqual(logs, ['hello']);
},
runtime_error: async_mode ? 'flush_sync_in_effect' : undefined
}
});

Loading…
Cancel
Save