-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Fix GC memory leak #4510
Merged
Fix GC memory leak #4510
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
packages/lexical/src/nodes/__tests__/unit/LexicalGC.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
|
||
import { | ||
$createParagraphNode, | ||
$createTextNode, | ||
$getNodeByKey, | ||
$getRoot, | ||
} from 'lexical'; | ||
|
||
import { | ||
$createTestElementNode, | ||
initializeUnitTest, | ||
} from '../../../__tests__/utils'; | ||
|
||
describe('LexicalGC tests', () => { | ||
initializeUnitTest((testEnv) => { | ||
test('RootNode.clear() with a child and subchild', async () => { | ||
const {editor} = testEnv; | ||
await editor.update(() => { | ||
$getRoot().append( | ||
$createParagraphNode().append($createTextNode('foo')), | ||
); | ||
}); | ||
expect(editor.getEditorState()._nodeMap.size).toBe(3); | ||
await editor.update(() => { | ||
$getRoot().clear(); | ||
}); | ||
expect(editor.getEditorState()._nodeMap.size).toBe(1); | ||
}); | ||
|
||
test('RootNode.clear() with a child and three subchildren', async () => { | ||
const {editor} = testEnv; | ||
await editor.update(() => { | ||
const text1 = $createTextNode('foo'); | ||
const text2 = $createTextNode('bar').toggleUnmergeable(); | ||
const text3 = $createTextNode('zzz').toggleUnmergeable(); | ||
const paragraph = $createParagraphNode(); | ||
paragraph.append(text1, text2, text3); | ||
$getRoot().append(paragraph); | ||
}); | ||
expect(editor.getEditorState()._nodeMap.size).toBe(5); | ||
await editor.update(() => { | ||
$getRoot().clear(); | ||
}); | ||
expect(editor.getEditorState()._nodeMap.size).toBe(1); | ||
}); | ||
|
||
for (let i = 0; i < 3; i++) { | ||
test(`RootNode.clear() with a child and three subchildren, subchild ${i} deleted first`, async () => { | ||
const {editor} = testEnv; | ||
await editor.update(() => { | ||
const text1 = $createTextNode('foo'); // 1 | ||
const text2 = $createTextNode('bar').toggleUnmergeable(); // 2 | ||
const text3 = $createTextNode('zzz').toggleUnmergeable(); // 3 | ||
const paragraph = $createParagraphNode(); // 4 | ||
paragraph.append(text1, text2, text3); | ||
$getRoot().append(paragraph); | ||
}); | ||
expect(editor.getEditorState()._nodeMap.size).toBe(5); | ||
await editor.update(() => { | ||
const root = $getRoot(); | ||
const subchild = root.getFirstChild().getChildAtIndex(i); | ||
expect(subchild.getTextContent()).toBe(['foo', 'bar', 'zzz'][i]); | ||
subchild.remove(); | ||
root.clear(); | ||
}); | ||
expect(editor.getEditorState()._nodeMap.size).toBe(1); | ||
}); | ||
} | ||
|
||
for (let i = 0; i < 7; i++) { | ||
/** | ||
* R | ||
* P | ||
* T TE T | ||
* T T | ||
*/ | ||
test(`RootNode.clear() with a complex tree, element ${i} node first`, async () => { | ||
const {editor} = testEnv; | ||
await editor.update(() => { | ||
const testElement = $createTestElementNode(); // 1 | ||
const testElementText1 = $createTextNode('te1').toggleUnmergeable(); // 2 | ||
const testElementText2 = $createTextNode('te2').toggleUnmergeable(); // 3 | ||
const text1 = $createTextNode('a').toggleUnmergeable(); // 4 | ||
const text2 = $createTextNode('b').toggleUnmergeable(); // 5 | ||
const paragraph = $createParagraphNode(); // 6 | ||
testElement.append(testElementText1, testElementText2); | ||
paragraph.append(text1, testElement, text2); | ||
$getRoot().append(paragraph); | ||
}); | ||
expect(editor.getEditorState()._nodeMap.size).toBe(7); | ||
await editor.update(() => { | ||
const node = $getNodeByKey('1'); | ||
node.remove(); | ||
$getRoot().clear(); | ||
}); | ||
expect(editor.getEditorState()._nodeMap.size).toBe(1); | ||
}); | ||
} | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not related to PR changes, but wondering why would we have undefined check here, it would throw one line above when accessing __key, and we have null check in while loop