Skip to content

Commit

Permalink
Add flushSync option to update() (facebook#3119)
Browse files Browse the repository at this point in the history
* Add flushSync option to update()

* Rename flushSync->discrete
  • Loading branch information
fantactuka authored Oct 17, 2022
1 parent 2ef877a commit 0dd855a
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 8 deletions.
10 changes: 2 additions & 8 deletions packages/lexical-playground/src/nodes/TableComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
*/

import type {EditorState, RangeSelection, TextFormatType} from 'lexical';
import type {RangeSelection, TextFormatType} from 'lexical';

import {
$generateJSONFromSelectedNodes,
Expand Down Expand Up @@ -204,13 +204,7 @@ function $updateCells(
const editorState = cellEditor.parseEditorState(cell.json);
cellEditor._headless = true;
cellEditor.setEditorState(editorState);
cellEditor.update(() => {
// Complete hack for now
const pendingEditorState =
cellEditor._pendingEditorState as EditorState;
pendingEditorState._flushSync = true;
fn();
});
cellEditor.update(fn, {discrete: true});
cellEditor._headless = false;
const newJSON = JSON.stringify(cellEditor.getEditorState());
updateTableNode((tableNode) => {
Expand Down
1 change: 1 addition & 0 deletions packages/lexical/flow/Lexical.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ type EditorUpdateOptions = {
onUpdate?: () => void,
tag?: string,
skipTransforms?: true,
discrete?: true,
};
type EditorFocusOptions = {
defaultSelection?: 'rootStart' | 'rootEnd',
Expand Down
1 change: 1 addition & 0 deletions packages/lexical/src/LexicalEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export type EditorUpdateOptions = {
onUpdate?: () => void;
skipTransforms?: true;
tag?: string;
discrete?: true;
};

export type EditorSetOptions = {
Expand Down
3 changes: 3 additions & 0 deletions packages/lexical/src/LexicalUpdates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,7 @@ function beginUpdate(
let onUpdate;
let tag;
let skipTransforms = false;
let discrete = false;

if (options !== undefined) {
onUpdate = options.onUpdate;
Expand All @@ -775,6 +776,7 @@ function beginUpdate(
}

skipTransforms = options.skipTransforms || false;
discrete = options.discrete || false;
}

if (onUpdate) {
Expand All @@ -790,6 +792,7 @@ function beginUpdate(
cloneEditorState(currentEditorState);
editorStateWasCloned = true;
}
pendingEditorState._flushSync = discrete;

const previousActiveEditorState = activeEditorState;
const previousReadOnlyMode = isReadOnlyMode;
Expand Down
22 changes: 22 additions & 0 deletions packages/lexical/src/__tests__/unit/LexicalEditor.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2142,4 +2142,26 @@ describe('LexicalEditor tests', () => {
expect(nodeTransformListener).toHaveBeenCalledTimes(1);
expect(mutationListener).toHaveBeenCalledTimes(1);
});

it('can use flushSync for synchronous updates', () => {
init();
const onUpdate = jest.fn();
editor.registerUpdateListener(onUpdate);
editor.update(
() => {
$getRoot().append(
$createParagraphNode().append($createTextNode('Sync update')),
);
},
{
discrete: true,
},
);

const textContent = editor
.getEditorState()
.read(() => $getRoot().getTextContent());
expect(textContent).toBe('Sync update');
expect(onUpdate).toHaveBeenCalledTimes(1);
});
});

0 comments on commit 0dd855a

Please sign in to comment.