Skip to content

Commit 001bbce

Browse files
committed
Make useFlushSync do sync flushes
1 parent 4ac7204 commit 001bbce

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

compat/src/index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import {
55
createRef,
66
Component,
77
createContext,
8-
Fragment
8+
Fragment,
9+
options
910
} from 'preact';
1011
import {
1112
useState,
@@ -139,7 +140,13 @@ const unstable_batchedUpdates = (callback, arg) => callback(arg);
139140
* @param {Arg} [arg] Optional argument that can be passed to the callback
140141
* @returns
141142
*/
142-
const flushSync = (callback, arg) => callback(arg);
143+
const flushSync = (callback, arg) => {
144+
const prevDebounce = options.debounceRendering;
145+
options.debounceRendering = cb => cb();
146+
const res = callback(arg);
147+
options.debounceRendering = prevDebounce;
148+
return res;
149+
};
143150

144151
/**
145152
* Strict Mode is not implemented in Preact, so we provide a stand-in for it

compat/test/browser/useSyncExternalStore.test.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -379,11 +379,9 @@ describe('useSyncExternalStore', () => {
379379

380380
// Switch stores and update in the same batch
381381
await act(() => {
382-
ReactDOM.flushSync(() => {
383-
// This update will be disregarded
384-
storeA.set(2);
385-
setStore(storeB);
386-
});
382+
// This update will be disregarded
383+
storeA.set(2);
384+
setStore(storeB);
387385
});
388386
// Now reading from B instead of A
389387
assertLog([0]);
@@ -500,7 +498,7 @@ describe('useSyncExternalStore', () => {
500498
// Should flip back to 0
501499
expect(container.textContent).to.equal('0');
502500

503-
// Preact: Wait for 'Passive effect: 0' to flush from the rAF so it doesn't impact other tests
501+
// Preact: Wait for 'Passive effect: 0' to y from the rAF so it doesn't impact other tests
504502
await new Promise(r => setTimeout(r, 32));
505503
});
506504

@@ -686,10 +684,8 @@ describe('useSyncExternalStore', () => {
686684

687685
// Update the store and getSnapshot at the same time
688686
await act(() => {
689-
ReactDOM.flushSync(() => {
690-
setGetSnapshot(() => getSnapshotB);
691-
store.set({ a: 1, b: 2 });
692-
});
687+
setGetSnapshot(() => getSnapshotB);
688+
store.set({ a: 1, b: 2 });
693689
});
694690
// It should read from B instead of A
695691
assertLog([2]);

0 commit comments

Comments
 (0)