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

Feature: perf: reduce listeners by current selection path #6101

Open
1yasa opened this issue May 14, 2024 · 0 comments
Open

Feature: perf: reduce listeners by current selection path #6101

1yasa opened this issue May 14, 2024 · 0 comments
Labels
enhancement Improvement over existing feature

Comments

@1yasa
Copy link

1yasa commented May 14, 2024

In a text content exceeding 10M, it contains hundreds of thousands of blocks, with each block monitoring a series of events in real time (which can lead to excessive memory and CPU usage). However, there is actually room for optimization.

That is, by monitoring changes in selection to obtain the node path where the selection is located, only the blocks on the free node path execute some monitoring events.

Here is my solution:

export default class Index {
	editor = null as LexicalEditor
	path = []

	constructor() {}

	init(editor: Index['editor']) {
		this.editor = editor
	}

	watcher() {
		const selection = $getSelection()

		if (!$isRangeSelection(selection)) return

		const anchor = selection.anchor
		const focus = selection.focus

		if (anchor.offset !== focus.offset) return

		const parents = anchor.getNode().getParents()
		const path = parents.map(item => ({ type: item.getType(), key: item.getKey() }))

		if (path === this.path) return

		this.editor.dispatchCommand(SELECTION_ELEMENTS_CHANGE, path)

		console.log(path)
	}

	register() {
		return mergeRegister(
			this.editor.registerCommand(
				SELECTION_CHANGE_COMMAND,
				this.watcher.bind(this),
				COMMAND_PRIORITY_CRITICAL
			)
		)
	}
}
@1yasa 1yasa added the enhancement Improvement over existing feature label May 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improvement over existing feature
Projects
None yet
Development

No branches or pull requests

1 participant