Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure to call existing listeners only (not newly added ones) #2573

Merged
merged 1 commit into from
Jul 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions packages/lexical/src/LexicalUpdates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -598,16 +598,16 @@ function triggerMutationListeners(
pendingEditorState: EditorState,
mutatedNodes: MutatedNodes,
): void {
const listeners = editor._listeners.mutation;
listeners.forEach((klass, listener) => {
const mutatedNodesByType = mutatedNodes.get(klass);
const listeners = Array.from(editor._listeners.mutation);
const listenersLength = listeners.length;

if (mutatedNodesByType === undefined) {
return;
for (let i = 0; i < listenersLength; i++) {
const [listener, klass] = listeners[i];
const mutatedNodesByType = mutatedNodes.get(klass);
if (mutatedNodesByType !== undefined) {
listener(mutatedNodesByType);
}

listener(mutatedNodesByType);
});
}
}

export function triggerListeners(
Expand Down Expand Up @@ -652,11 +652,14 @@ export function triggerCommandListeners<P>(
const listenerInPriorityOrder = commandListeners.get(type);

if (listenerInPriorityOrder !== undefined) {
const listeners = listenerInPriorityOrder[i];
const listenersSet = listenerInPriorityOrder[i];

if (listenersSet !== undefined) {
const listeners = Array.from(listenersSet);
const listenersLength = listeners.length;

if (listeners !== undefined) {
for (const listener of listeners) {
if (listener(payload, editor) === true) {
for (let j = 0; j < listenersLength; j++) {
if (listeners[j](payload, editor) === true) {
return true;
}
}
Expand Down
63 changes: 62 additions & 1 deletion packages/lexical/src/__tests__/unit/LexicalEditor.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
$setCompositionKey,
$setSelection,
COMMAND_PRIORITY_EDITOR,
COMMAND_PRIORITY_LOW,
createCommand,
ElementNode,
LexicalEditor,
Expand Down Expand Up @@ -2065,6 +2066,13 @@ describe('LexicalEditor tests', () => {

it('does not add new listeners while triggering existing', async () => {
const updateListener = jest.fn();
const mutationListener = jest.fn();
const nodeTransformListener = jest.fn();
const textContentListener = jest.fn();
const readOnlyListener = jest.fn();
const commandListener = jest.fn();
const TEST_COMMAND = createCommand();

init();

editor.registerUpdateListener(() => {
Expand All @@ -2075,10 +2083,63 @@ describe('LexicalEditor tests', () => {
});
});

editor.registerMutationListener(TextNode, (map) => {
mutationListener();
editor.registerMutationListener(TextNode, () => {
mutationListener();
});
});

editor.registerNodeTransform(ParagraphNode, () => {
nodeTransformListener();
editor.registerNodeTransform(ParagraphNode, () => {
nodeTransformListener();
});
});

editor.registerReadOnlyListener(() => {
readOnlyListener();
editor.registerReadOnlyListener(() => {
readOnlyListener();
});
});

editor.registerTextContentListener(() => {
textContentListener();
editor.registerTextContentListener(() => {
textContentListener();
});
});

editor.registerCommand(
TEST_COMMAND,
(): boolean => {
commandListener();
editor.registerCommand(
TEST_COMMAND,
commandListener,
COMMAND_PRIORITY_LOW,
);
return false;
},
COMMAND_PRIORITY_LOW,
);

await update(() => {
$getRoot().getFirstChild().replace($createParagraphNode());
$getRoot().append(
$createParagraphNode().append($createTextNode('Hello world')),
);
});

editor.dispatchCommand(TEST_COMMAND, false);

editor.setReadOnly(true);

expect(updateListener).toHaveBeenCalledTimes(1);
expect(readOnlyListener).toHaveBeenCalledTimes(1);
expect(commandListener).toHaveBeenCalledTimes(1);
expect(textContentListener).toHaveBeenCalledTimes(1);
expect(nodeTransformListener).toHaveBeenCalledTimes(1);
expect(mutationListener).toHaveBeenCalledTimes(1);
});
});