Browse Source

mol-state: added StateTree.subtreeHasRef

David Sehnal 5 years ago
parent
commit
d1433aaf7b
1 changed files with 17 additions and 0 deletions
  1. 17 0
      src/mol-state/tree/immutable.ts

+ 17 - 0
src/mol-state/tree/immutable.ts

@@ -160,4 +160,21 @@ namespace StateTree {
             ch: (tree.children as ImmutableMap<any, any>).keySeq().toArray()
         });
     }
+
+    function _subtreeHasRef(tree: StateTree, root: StateTransform.Ref, ref: StateTransform.Ref) {
+        if (root === ref) return true;
+        const children = tree.children.get(root);
+        const it = children.values();
+        while (true) {
+            const next = it.next();
+            if (next.done) return false;
+            if (_subtreeHasRef(tree, next.value, ref)) return true;
+        }
+    }
+
+    /** Check if the subtree with the given root has the provided ref */
+    export function subtreeHasRef(tree: StateTree, root: StateTransform.Ref, ref: StateTransform.Ref): boolean {
+        if (!tree.transforms.has(root) || !tree.transforms.has(ref)) return false;
+        return _subtreeHasRef(tree, root, ref);
+    }
 }