upgrade descendants and nodesBetween handlers to support aborting a graph scan #75

Closed
st3v0 wants to merge 1 commit from master into master
st3v0 commented 2022-10-25 04:54:30 +02:00 (Migrated from github.com)

Context

We need the ability to end the recursive scan (part way through the graph) which is performed by descendants and nodesBetween.

At the moment if we return false from the node handler; the scan continues to the next sibling in the graph. This is wasteful if there is a large amount of siblings and all we're trying to do is check if a node exists.

Proposed Solution

We could modify the nodesBetween node handler method to support more than returning boolean | void. If anything else is returned then the scan stops and returns that item.

This is a performance improvement solution with the added benefit of being able to use it for finding.

Checklist

Is this solution backwards compatible:
Does this solution include tests:

Examples

Say you want to get a text node from somewhere in the doc, which contains the text 'foobar', you could;

const node = doc.descendants((node) => node.isText && node.text === 'foobar' ? node : true);

Or you want to get the type of the first leaf node;

const type = doc.descendants((node) => node.isLeaf ? node.type : true);
### Context We need the ability to end the recursive scan (part way through the graph) which is performed by `descendants` and `nodesBetween`. At the moment if we return `false` from the node handler; the scan continues to the next sibling in the graph. This is wasteful if there is a large amount of siblings and all we're trying to do is check if a node exists. ### Proposed Solution We could modify the `nodesBetween` node handler method to support more than returning `boolean | void`. If anything else is returned then the scan stops and returns that item. This is a performance improvement solution with the added benefit of being able to use it for finding. ### Checklist Is this solution backwards compatible: ✅ Does this solution include tests: ❌ ### Examples Say you want to get a text node from somewhere in the doc, which contains the text 'foobar', you could; ``` const node = doc.descendants((node) => node.isText && node.text === 'foobar' ? node : true); ``` Or you want to get the type of the first leaf node; ``` const type = doc.descendants((node) => node.isLeaf ? node.type : true); ```
marijnh commented 2022-10-25 09:46:35 +02:00 (Migrated from github.com)

What I've been doing is just setting a flag (or storing a non-null result) when the search has finished, and putting a check for that at the top of the callback, making it return false right away. That seems to cover the cases where this is necessary with less complication.

What I've been doing is just setting a flag (or storing a non-null result) when the search has finished, and putting a check for that at the top of the callback, making it return false right away. That seems to cover the cases where this is necessary with less complication.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
prosemirror/prosemirror-model!75
No description provided.