Skip to content

Commit 8af20fe

Browse files
committed
fix flushSync types
1 parent c45b60c commit 8af20fe

File tree

9 files changed

+50
-51
lines changed

9 files changed

+50
-51
lines changed

.changeset/violet-camels-heal.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+
fix: update types and inline docs for flushSync

packages/svelte/src/compiler/phases/3-transform/client/transform-client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ export function client_component(analysis, options) {
312312

313313
const setter = b.set(key, [
314314
b.stmt(b.call(b.id(name), b.id('$$value'))),
315-
b.stmt(b.call('$.flush_sync'))
315+
b.stmt(b.call('$.flush'))
316316
]);
317317

318318
if (analysis.runes && binding.initial) {

packages/svelte/src/index-client.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/** @import { ComponentContext, ComponentContextLegacy } from '#client' */
22
/** @import { EventDispatcher } from './index.js' */
33
/** @import { NotFunction } from './internal/types.js' */
4-
import { flush_sync, untrack } from './internal/client/runtime.js';
4+
import { untrack } from './internal/client/runtime.js';
55
import { is_array } from './internal/shared/utils.js';
66
import { user_effect } from './internal/client/index.js';
77
import * as e from './internal/client/errors.js';
@@ -206,15 +206,7 @@ function init_update_callbacks(context) {
206206
return (l.u ??= { a: [], b: [], m: [] });
207207
}
208208

209-
/**
210-
* Synchronously flushes any pending state changes and those that result from it.
211-
* @param {() => void} [fn]
212-
* @returns {void}
213-
*/
214-
export function flushSync(fn) {
215-
flush_sync(fn);
216-
}
217-
209+
export { flushSync } from './internal/client/runtime.js';
218210
export { getContext, getAllContexts, hasContext, setContext } from './internal/client/context.js';
219211
export { hydrate, mount, unmount } from './internal/client/render.js';
220212
export { tick, untrack } from './internal/client/runtime.js';

packages/svelte/src/internal/client/dom/blocks/await.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { DEV } from 'esm-env';
33
import { is_promise } from '../../../shared/utils.js';
44
import { block, branch, pause_effect, resume_effect } from '../../reactivity/effects.js';
55
import { internal_set, mutable_source, source } from '../../reactivity/sources.js';
6-
import { flush_sync, set_active_effect, set_active_reaction } from '../../runtime.js';
6+
import { flushSync, set_active_effect, set_active_reaction } from '../../runtime.js';
77
import { hydrate_next, hydrate_node, hydrating } from '../hydration.js';
88
import { queue_micro_task } from '../task.js';
99
import { UNINITIALIZED } from '../../../../constants.js';
@@ -105,7 +105,7 @@ export function await_block(node, get_input, pending_fn, then_fn, catch_fn) {
105105

106106
// without this, the DOM does not update until two ticks after the promise
107107
// resolves, which is unexpected behaviour (and somewhat irksome to test)
108-
flush_sync();
108+
flushSync();
109109
}
110110
}
111111
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ export {
137137
get,
138138
safe_get,
139139
invalidate_inner_signals,
140-
flush_sync,
140+
flushSync as flush,
141141
tick,
142142
untrack,
143143
exclude_from_object,

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -823,12 +823,13 @@ function process_effects(effect) {
823823
}
824824

825825
/**
826-
* Internal version of `flushSync` with the option to not flush previous effects.
827-
* Returns the result of the passed function, if given.
828-
* @param {() => any} [fn]
829-
* @returns {any}
826+
* Synchronously flush any pending updates.
827+
* Returns void if no callback is provided, otherwise returns the result of calling the callback.
828+
* @template [T=void]
829+
* @param {(() => T) | undefined} [fn]
830+
* @returns {T extends void ? void : T}
830831
*/
831-
export function flush_sync(fn) {
832+
export function flushSync(fn) {
832833
flush_queued_root_effects();
833834

834835
var result = fn?.();
@@ -840,7 +841,7 @@ export function flush_sync(fn) {
840841
flush_tasks();
841842
}
842843

843-
return result;
844+
return /** @type {T extends void ? void : T} */ (result);
844845
}
845846

846847
/**
@@ -849,9 +850,9 @@ export function flush_sync(fn) {
849850
*/
850851
export async function tick() {
851852
await Promise.resolve();
852-
// By calling flush_sync we guarantee that any pending state changes are applied after one tick.
853+
// By calling flushSync we guarantee that any pending state changes are applied after one tick.
853854
// TODO look into whether we can make flushing subsequent updates synchronously in the future.
854-
flush_sync();
855+
flushSync();
855856
}
856857

857858
/**

packages/svelte/src/legacy/legacy-client.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { DIRTY, LEGACY_PROPS, MAYBE_DIRTY } from '../internal/client/constants.j
33
import { user_pre_effect } from '../internal/client/reactivity/effects.js';
44
import { mutable_source, set } from '../internal/client/reactivity/sources.js';
55
import { hydrate, mount, unmount } from '../internal/client/render.js';
6-
import { active_effect, flush_sync, get, set_signal_status } from '../internal/client/runtime.js';
6+
import { active_effect, flushSync, get, set_signal_status } from '../internal/client/runtime.js';
77
import { lifecycle_outside_component } from '../internal/shared/errors.js';
88
import { define_property, is_array } from '../internal/shared/utils.js';
99
import * as w from '../internal/client/warnings.js';
@@ -119,9 +119,9 @@ class Svelte4Component {
119119
recover: options.recover
120120
});
121121

122-
// We don't flush_sync for custom element wrappers or if the user doesn't want it
122+
// We don't flushSync for custom element wrappers or if the user doesn't want it
123123
if (!options?.props?.$$host || options.sync === false) {
124-
flush_sync();
124+
flushSync();
125125
}
126126

127127
this.#events = props.$$events;

packages/svelte/tests/store/test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ describe('toStore', () => {
602602
assert.deepEqual(log, [0]);
603603

604604
set(count, 1);
605-
$.flush_sync();
605+
$.flushSync();
606606
assert.deepEqual(log, [0, 1]);
607607

608608
unsubscribe();
@@ -625,7 +625,7 @@ describe('toStore', () => {
625625
assert.deepEqual(log, [0]);
626626

627627
set(count, 1);
628-
$.flush_sync();
628+
$.flushSync();
629629
assert.deepEqual(log, [0, 1]);
630630

631631
store.set(2);
@@ -654,11 +654,11 @@ describe('fromStore', () => {
654654
assert.deepEqual(log, [0]);
655655

656656
store.set(1);
657-
$.flush_sync();
657+
$.flushSync();
658658
assert.deepEqual(log, [0, 1]);
659659

660660
count.current = 2;
661-
$.flush_sync();
661+
$.flushSync();
662662
assert.deepEqual(log, [0, 1, 2]);
663663

664664
assert.equal(get(store), 2);

packages/svelte/types/index.d.ts

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -408,10 +408,6 @@ declare module 'svelte' {
408408
* @deprecated Use [`$effect`](https://svelte.dev/docs/svelte/$effect) instead
409409
* */
410410
export function afterUpdate(fn: () => void): void;
411-
/**
412-
* Synchronously flushes any pending state changes and those that result from it.
413-
* */
414-
export function flushSync(fn?: (() => void) | undefined): void;
415411
/**
416412
* Create a snippet programmatically
417413
* */
@@ -421,6 +417,29 @@ declare module 'svelte' {
421417
}): Snippet<Params>;
422418
/** Anything except a function */
423419
type NotFunction<T> = T extends Function ? never : T;
420+
/**
421+
* Synchronously flush any pending updates.
422+
* Returns void if no callback is provided, otherwise returns the result of calling the callback.
423+
* */
424+
export function flushSync<T = void>(fn?: (() => T) | undefined): T extends void ? void : T;
425+
/**
426+
* Returns a promise that resolves once any pending state changes have been applied.
427+
* */
428+
export function tick(): Promise<void>;
429+
/**
430+
* When used inside a [`$derived`](https://svelte.dev/docs/svelte/$derived) or [`$effect`](https://svelte.dev/docs/svelte/$effect),
431+
* any state read inside `fn` will not be treated as a dependency.
432+
*
433+
* ```ts
434+
* $effect(() => {
435+
* // this will run when `data` changes, but not when `time` changes
436+
* save(data, {
437+
* timestamp: untrack(() => time)
438+
* });
439+
* });
440+
* ```
441+
* */
442+
export function untrack<T>(fn: () => T): T;
424443
/**
425444
* Retrieves the context that belongs to the closest parent component with the specified `key`.
426445
* Must be called during component initialisation.
@@ -494,24 +513,6 @@ declare module 'svelte' {
494513
export function unmount(component: Record<string, any>, options?: {
495514
outro?: boolean;
496515
} | undefined): Promise<void>;
497-
/**
498-
* Returns a promise that resolves once any pending state changes have been applied.
499-
* */
500-
export function tick(): Promise<void>;
501-
/**
502-
* When used inside a [`$derived`](https://svelte.dev/docs/svelte/$derived) or [`$effect`](https://svelte.dev/docs/svelte/$effect),
503-
* any state read inside `fn` will not be treated as a dependency.
504-
*
505-
* ```ts
506-
* $effect(() => {
507-
* // this will run when `data` changes, but not when `time` changes
508-
* save(data, {
509-
* timestamp: untrack(() => time)
510-
* });
511-
* });
512-
* ```
513-
* */
514-
export function untrack<T>(fn: () => T): T;
515516
type Getters<T> = {
516517
[K in keyof T]: () => T[K];
517518
};

0 commit comments

Comments
 (0)