traverseAncestors

Function

Common
fun DelegatableNode.traverseAncestors(key: Any?, block: (TraversableNode) -> Boolean)

Executes block for all ancestors with a matching key.

Note: The parameter block's return boolean value will determine if the traversal will continue (true = continue, false = cancel).

Common
fun <T> T.traverseAncestors(block: (T) -> Boolean) where T : TraversableNode

Executes block for all ancestors of the same class and key.

Note: The parameter block's return boolean value will determine if the traversal will continue (true = continue, false = cancel).

Code Examples

traverseAncestorsWithKeyDemo

/**
 * Simplified example of traverseAncestors with a key. For a full featured sample, look below at
 * [TraverseModifierDemo].
 */
fun traverseAncestorsWithKeyDemo() {
    val customTraversableModifierNode = CustomTraversableModifierNode()
    with(customTraversableModifierNode) {
        traverseAncestors(traverseKey) {
            if (it is CustomTraversableModifierNode) {
                it.doSomethingWithAncestor()
            }
            // Return true to continue searching the tree after a match. If you were looking to
            // match only some of the nodes, you could return false and stop executing the search.
            true
        }
    }
}

traverseAncestorsDemo

/**
 * Simplified example of traverseAncestors. For a full featured sample, look below at
 * [TraverseModifierDemo].
 */
fun traverseAncestorsDemo() {
    val customTraversableModifierNode = CustomTraversableModifierNode()
    with(customTraversableModifierNode) {
        traverseAncestors {
            // Because I use the existing key of the class, I can guarantee 'it' will be of the same
            // type as the class, so I can call my functions directly.
            it.doSomethingWithAncestor()
            // Return true to continue searching the tree after a match. If you were looking to
            // match only some of the nodes, you could return false and stop executing the search.
            true
        }
    }
}