upgrade descendants and nodesBetween handlers to support aborting a graph scan #75
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.
Context
We need the ability to end the recursive scan (part way through the graph) which is performed by
descendants
andnodesBetween
.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 returningboolean | 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;
Or you want to get the type of the first leaf node;