Skip to content

Commit 150aac2

Browse files
fantactukaacywatson
authored andcommitted
Add flushSync option to update() (#3119)
* Add flushSync option to update() * Rename flushSync->discrete
1 parent 90c0a99 commit 150aac2

File tree

5 files changed

+29
-8
lines changed

5 files changed

+29
-8
lines changed

packages/lexical-playground/src/nodes/TableComponent.tsx

+2-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
*/
88

9-
import type {EditorState, RangeSelection, TextFormatType} from 'lexical';
9+
import type {RangeSelection, TextFormatType} from 'lexical';
1010

1111
import {
1212
$generateJSONFromSelectedNodes,
@@ -204,13 +204,7 @@ function $updateCells(
204204
const editorState = cellEditor.parseEditorState(cell.json);
205205
cellEditor._headless = true;
206206
cellEditor.setEditorState(editorState);
207-
cellEditor.update(() => {
208-
// Complete hack for now
209-
const pendingEditorState =
210-
cellEditor._pendingEditorState as EditorState;
211-
pendingEditorState._flushSync = true;
212-
fn();
213-
});
207+
cellEditor.update(fn, {discrete: true});
214208
cellEditor._headless = false;
215209
const newJSON = JSON.stringify(cellEditor.getEditorState());
216210
updateTableNode((tableNode) => {

packages/lexical/flow/Lexical.js.flow

+1
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ type EditorUpdateOptions = {
182182
onUpdate?: () => void,
183183
tag?: string,
184184
skipTransforms?: true,
185+
discrete?: true,
185186
};
186187
type EditorFocusOptions = {
187188
defaultSelection?: 'rootStart' | 'rootEnd',

packages/lexical/src/LexicalEditor.ts

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export type EditorUpdateOptions = {
6060
onUpdate?: () => void;
6161
skipTransforms?: true;
6262
tag?: string;
63+
discrete?: true;
6364
};
6465

6566
export type EditorSetOptions = {

packages/lexical/src/LexicalUpdates.ts

+3
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,7 @@ function beginUpdate(
765765
let onUpdate;
766766
let tag;
767767
let skipTransforms = false;
768+
let discrete = false;
768769

769770
if (options !== undefined) {
770771
onUpdate = options.onUpdate;
@@ -775,6 +776,7 @@ function beginUpdate(
775776
}
776777

777778
skipTransforms = options.skipTransforms || false;
779+
discrete = options.discrete || false;
778780
}
779781

780782
if (onUpdate) {
@@ -790,6 +792,7 @@ function beginUpdate(
790792
cloneEditorState(currentEditorState);
791793
editorStateWasCloned = true;
792794
}
795+
pendingEditorState._flushSync = discrete;
793796

794797
const previousActiveEditorState = activeEditorState;
795798
const previousReadOnlyMode = isReadOnlyMode;

packages/lexical/src/__tests__/unit/LexicalEditor.test.tsx

+22
Original file line numberDiff line numberDiff line change
@@ -2142,4 +2142,26 @@ describe('LexicalEditor tests', () => {
21422142
expect(nodeTransformListener).toHaveBeenCalledTimes(1);
21432143
expect(mutationListener).toHaveBeenCalledTimes(1);
21442144
});
2145+
2146+
it('can use flushSync for synchronous updates', () => {
2147+
init();
2148+
const onUpdate = jest.fn();
2149+
editor.registerUpdateListener(onUpdate);
2150+
editor.update(
2151+
() => {
2152+
$getRoot().append(
2153+
$createParagraphNode().append($createTextNode('Sync update')),
2154+
);
2155+
},
2156+
{
2157+
discrete: true,
2158+
},
2159+
);
2160+
2161+
const textContent = editor
2162+
.getEditorState()
2163+
.read(() => $getRoot().getTextContent());
2164+
expect(textContent).toBe('Sync update');
2165+
expect(onUpdate).toHaveBeenCalledTimes(1);
2166+
});
21452167
});

0 commit comments

Comments
 (0)