Browse Source

mol-state: wip

David Sehnal 6 years ago
parent
commit
d3b509a539

+ 1 - 1
src/mol-plugin/ui/state-tree.tsx

@@ -34,7 +34,7 @@ export class StateTreeNode extends PluginComponent<{ nodeRef: string, state: Sta
     }
 
     render() {
-        const n = this.props.state.tree.nodes.get(this.props.nodeRef)!;
+        const n = this.props.state.tree.transforms.get(this.props.nodeRef)!;
         const cell = this.props.state.cells.get(this.props.nodeRef)!;
 
         const remove = <>[<a href='#' onClick={e => {

+ 8 - 8
src/mol-state/state.ts

@@ -265,7 +265,7 @@ function findUpdateRootsVisitor(n: Transform, _: any, s: { roots: Ref[], cells:
 
 type FindDeletesCtx = { newTree: StateTree, cells: State.Cells, deletes: Ref[] }
 function _visitCheckDelete(n: Transform, _: any, ctx: FindDeletesCtx) {
-    if (!ctx.newTree.nodes.has(n.ref) && ctx.cells.has(n.ref)) ctx.deletes.push(n.ref);
+    if (!ctx.newTree.transforms.has(n.ref) && ctx.cells.has(n.ref)) ctx.deletes.push(n.ref);
 }
 function findDeletes(ctx: UpdateContext): Ref[] {
     const deleteCtx: FindDeletesCtx = { newTree: ctx.tree, cells: ctx.cells, deletes: [] };
@@ -288,7 +288,7 @@ function initCellStatusVisitor(t: Transform, _: any, ctx: UpdateContext) {
 
 function initCellStatus(ctx: UpdateContext, roots: Ref[]) {
     for (const root of roots) {
-        StateTree.doPreOrder(ctx.tree, ctx.tree.nodes.get(root), ctx, initCellStatusVisitor);
+        StateTree.doPreOrder(ctx.tree, ctx.tree.transforms.get(root), ctx, initCellStatusVisitor);
     }
 }
 
@@ -314,7 +314,7 @@ function initCellsVisitor(transform: Transform, _: any, ctx: UpdateContext) {
 
 function initCells(ctx: UpdateContext, roots: Ref[]) {
     for (const root of roots) {
-        StateTree.doPreOrder(ctx.tree, ctx.tree.nodes.get(root), ctx, initCellsVisitor);
+        StateTree.doPreOrder(ctx.tree, ctx.tree.transforms.get(root), ctx, initCellsVisitor);
     }
 }
 
@@ -326,7 +326,7 @@ function findNewCurrent(ctx: UpdateContext, start: Ref, deletes: Ref[]) {
 function _findNewCurrent(tree: StateTree, ref: Ref, deletes: Set<Ref>): Ref {
     if (ref === Transform.RootRef) return ref;
 
-    const node = tree.nodes.get(ref)!;
+    const node = tree.transforms.get(ref)!;
     const siblings = tree.children.get(node.parent)!.values();
 
     let prevCandidate: Ref | undefined = void 0, seenRef = false;
@@ -337,7 +337,7 @@ function _findNewCurrent(tree: StateTree, ref: Ref, deletes: Set<Ref>): Ref {
 
         if (deletes.has(s.value)) continue;
 
-        const t = tree.nodes.get(s.value);
+        const t = tree.transforms.get(s.value);
         if (t.props && t.props.isGhost) continue;
         if (s.value === ref) {
             seenRef = true;
@@ -397,7 +397,7 @@ async function updateSubtree(ctx: UpdateContext, root: Ref) {
         if (update.action === 'created') {
             ctx.parent.events.object.created.next({ state: ctx.parent, ref: root, obj: update.obj! });
             if (!ctx.hadError) {
-                const transform = ctx.tree.nodes.get(root);
+                const transform = ctx.tree.transforms.get(root);
                 if (!transform.props || !transform.props.isGhost) ctx.newCurrent = root;
             }
         } else if (update.action === 'updated') {
@@ -436,14 +436,14 @@ async function updateNode(ctx: UpdateContext, currentRef: Ref): Promise<UpdateNo
     const parent = parentCell.obj!;
     current.sourceRef = parentCell.transform.ref;
 
-    if (!oldTree.nodes.has(currentRef)) {
+    if (!oldTree.transforms.has(currentRef)) {
         const obj = await createObject(ctx, currentRef, transform.transformer, parent, transform.params);
         current.obj = obj;
         current.version = transform.version;
 
         return { action: 'created', obj };
     } else {
-        const oldParams = oldTree.nodes.get(currentRef)!.params;
+        const oldParams = oldTree.transforms.get(currentRef)!.params;
 
         const updateKind = !!current.obj
             ? await updateObject(ctx, currentRef, transform.transformer, parent, current.obj!, oldParams, transform.params)

+ 4 - 4
src/mol-state/state/selection.ts

@@ -169,7 +169,7 @@ namespace StateSelection {
     export function subtree(b: Selector) {
         return flatMap(b, (n, s) => {
             const nodes = [] as string[];
-            StateTree.doPreOrder(s.tree, s.tree.nodes.get(n.transform.ref), nodes, (x, _, ctx) => { ctx.push(x.ref) });
+            StateTree.doPreOrder(s.tree, s.tree.transforms.get(n.transform.ref), nodes, (x, _, ctx) => { ctx.push(x.ref) });
             return nodes.map(x => s.cells.get(x)!);
         });
     }
@@ -190,12 +190,12 @@ namespace StateSelection {
     export function ancestorOfType(b: Selector, types: StateObject.Ctor[]) { return unique(mapEntity(b, (n, s) => findAncestorOfType(s.tree, s.cells, n.transform.ref, types))); }
 
     registerModifier('parent', parent);
-    export function parent(b: Selector) { return unique(mapEntity(b, (n, s) => s.cells.get(s.tree.nodes.get(n.transform.ref)!.parent))); }
+    export function parent(b: Selector) { return unique(mapEntity(b, (n, s) => s.cells.get(s.tree.transforms.get(n.transform.ref)!.parent))); }
 
     export function findAncestorOfType(tree: StateTree, cells: State.Cells, root: Transform.Ref, types: StateObject.Ctor[]): StateObjectCell | undefined {
-        let current = tree.nodes.get(root)!, len = types.length;
+        let current = tree.transforms.get(root)!, len = types.length;
         while (true) {
-            current = tree.nodes.get(current.parent)!;
+            current = tree.transforms.get(current.parent)!;
             const cell = cells.get(current.ref)!;
             if (!cell.obj) return void 0;
             const obj = cell.obj;

+ 2 - 2
src/mol-state/tree/builder.ts

@@ -64,7 +64,7 @@ namespace StateTreeBuilder {
         update<T extends Transformer<A, any, any>>(paramsOrTransformer: T, provider?: (old: Transformer.Params<T>) => Transformer.Params<T>) {
             let params: any;
             if (provider) {
-                const old = this.state.tree.nodes.get(this.ref)!;
+                const old = this.state.tree.transforms.get(this.ref)!;
                 params = provider(old.params as any);
             } else {
                 params = paramsOrTransformer;
@@ -85,7 +85,7 @@ namespace StateTreeBuilder {
         getTree(): StateTree { return this.state.tree.asImmutable(); }
 
         constructor(private state: State, private ref: Transform.Ref, private root: Root) {
-            if (!this.state.tree.nodes.has(ref)) {
+            if (!this.state.tree.transforms.has(ref)) {
                 throw new Error(`Could not find node '${ref}'.`);
             }
         }

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

@@ -17,7 +17,7 @@ export { StateTree }
  */
 interface StateTree {
     readonly root: Transform,
-    readonly nodes: StateTree.Nodes,
+    readonly transforms: StateTree.Nodes,
     readonly children: StateTree.Children,
     asTransient(): TransientTree,
     build(): StateTreeBuilder.Root
@@ -40,12 +40,11 @@ namespace StateTree {
         get(ref: Ref): T
     }
 
-    export type Node = Transform
-    export type Nodes = _Map<Transform>
-    export type Children = _Map<ChildSet>
+    export interface Nodes extends _Map<Transform> {}
+    export interface Children extends _Map<ChildSet> { }
 
     class Impl implements StateTree {
-        get root() { return this.nodes.get(Transform.RootRef)! }
+        get root() { return this.transforms.get(Transform.RootRef)! }
 
         asTransient(): TransientTree {
             return new TransientTree(this);
@@ -55,7 +54,7 @@ namespace StateTree {
             return new StateTreeBuilder.Root(this);
         }
 
-        constructor(public nodes: StateTree.Nodes, public children: Children) {
+        constructor(public transforms: StateTree.Nodes, public children: Children) {
         }
     }
 
@@ -71,10 +70,10 @@ namespace StateTree {
         return new Impl(nodes, children);
     }
 
-    type VisitorCtx = { tree: StateTree, state: any, f: (node: Node, tree: StateTree, state: any) => boolean | undefined | void };
+    type VisitorCtx = { tree: StateTree, state: any, f: (node: Transform, tree: StateTree, state: any) => boolean | undefined | void };
 
-    function _postOrderFunc(this: VisitorCtx, c: Ref | undefined) { _doPostOrder(this, this.tree.nodes.get(c!)!); }
-    function _doPostOrder(ctx: VisitorCtx, root: Node) {
+    function _postOrderFunc(this: VisitorCtx, c: Ref | undefined) { _doPostOrder(this, this.tree.transforms.get(c!)!); }
+    function _doPostOrder(ctx: VisitorCtx, root: Transform) {
         const children = ctx.tree.children.get(root.ref);
         if (children && children.size) {
             children.forEach(_postOrderFunc, ctx);
@@ -85,14 +84,14 @@ namespace StateTree {
     /**
      * Visit all nodes in a subtree in "post order", meaning leafs get visited first.
      */
-    export function doPostOrder<S>(tree: StateTree, root: Node, state: S, f: (node: Node, tree: StateTree, state: S) => boolean | undefined | void): S {
+    export function doPostOrder<S>(tree: StateTree, root: Transform, state: S, f: (node: Transform, tree: StateTree, state: S) => boolean | undefined | void): S {
         const ctx: VisitorCtx = { tree, state, f };
         _doPostOrder(ctx, root);
         return ctx.state;
     }
 
-    function _preOrderFunc(this: VisitorCtx, c: Ref | undefined) { _doPreOrder(this, this.tree.nodes.get(c!)!); }
-    function _doPreOrder(ctx: VisitorCtx, root: Node) {
+    function _preOrderFunc(this: VisitorCtx, c: Ref | undefined) { _doPreOrder(this, this.tree.transforms.get(c!)!); }
+    function _doPreOrder(ctx: VisitorCtx, root: Transform) {
         const ret = ctx.f(root, ctx.tree, ctx.state);
         if (typeof ret === 'boolean' && !ret) return;
         const children = ctx.tree.children.get(root.ref);
@@ -105,24 +104,24 @@ namespace StateTree {
      * Visit all nodes in a subtree in "pre order", meaning leafs get visited last.
      * If the visitor function returns false, the visiting for that branch is interrupted.
      */
-    export function doPreOrder<S>(tree: StateTree, root: Node, state: S, f: (node: Node, tree: StateTree, state: S) => boolean | undefined | void): S {
+    export function doPreOrder<S>(tree: StateTree, root: Transform, state: S, f: (node: Transform, tree: StateTree, state: S) => boolean | undefined | void): S {
         const ctx: VisitorCtx = { tree, state, f };
         _doPreOrder(ctx, root);
         return ctx.state;
     }
 
-    function _subtree(n: Node, _: any, subtree: Node[]) { subtree.push(n); }
+    function _subtree(n: Transform, _: any, subtree: Transform[]) { subtree.push(n); }
     /**
      * Get all nodes in a subtree, leafs come first.
      */
-    export function subtreePostOrder<T>(tree: StateTree, root: Node) {
-        return doPostOrder<Node[]>(tree, root, [], _subtree);
+    export function subtreePostOrder<T>(tree: StateTree, root: Transform) {
+        return doPostOrder<Transform[]>(tree, root, [], _subtree);
     }
 
 
     // function _visitChildToJson(this: Ref[], ref: Ref) { this.push(ref); }
     // interface ToJsonCtx { nodes: Transform.Serialized[] }
-    function _visitNodeToJson(node: Node, tree: StateTree, ctx: Transform.Serialized[]) {
+    function _visitNodeToJson(node: Transform, tree: StateTree, ctx: Transform.Serialized[]) {
         // const children: Ref[] = [];
         // tree.children.get(node.ref).forEach(_visitChildToJson as any, children);
         ctx.push(Transform.toJSON(node));
@@ -140,7 +139,7 @@ namespace StateTree {
     }
 
     export function fromJSON<T>(data: Serialized): StateTree {
-        const nodes = ImmutableMap<Ref, Node>().asMutable();
+        const nodes = ImmutableMap<Ref, Transform>().asMutable();
         const children = ImmutableMap<Ref, OrderedSet<Ref>>().asMutable();
 
         for (const s of data.nodes) {

+ 17 - 17
src/mol-state/tree/transient.ts

@@ -15,7 +15,7 @@ import { UUID } from 'mol-util';
 export { TransientTree }
 
 class TransientTree implements StateTree {
-    nodes = this.tree.nodes as ImmutableMap<Transform.Ref, Transform>;
+    transforms = this.tree.transforms as ImmutableMap<Transform.Ref, Transform>;
     children = this.tree.children as ImmutableMap<Transform.Ref, OrderedSet<Transform.Ref>>;
 
     private changedNodes = false;
@@ -29,7 +29,7 @@ class TransientTree implements StateTree {
         return this._childMutations;
     }
 
-    get root() { return this.nodes.get(Transform.RootRef)! }
+    get root() { return this.transforms.get(Transform.RootRef)! }
 
     build(): StateTreeBuilder.Root {
         return new StateTreeBuilder.Root(this);
@@ -86,8 +86,8 @@ class TransientTree implements StateTree {
     add(transform: Transform) {
         const ref = transform.ref;
 
-        if (this.nodes.has(transform.ref)) {
-            const node = this.nodes.get(transform.ref);
+        if (this.transforms.has(transform.ref)) {
+            const node = this.transforms.get(transform.ref);
             if (node.parent !== transform.parent) alreadyPresent(transform.ref);
         }
 
@@ -108,18 +108,18 @@ class TransientTree implements StateTree {
 
         if (!this.changedNodes) {
             this.changedNodes = true;
-            this.nodes = this.nodes.asMutable();
+            this.transforms = this.transforms.asMutable();
         }
 
-        this.nodes.set(ref, transform);
+        this.transforms.set(ref, transform);
         return this;
     }
 
     /** Calls Transform.definition.params.areEqual if available, otherwise uses shallowEqual to check if the params changed */
     setParams(ref: Transform.Ref, params: unknown) {
-        ensurePresent(this.nodes, ref);
+        ensurePresent(this.transforms, ref);
 
-        const transform = this.nodes.get(ref)!;
+        const transform = this.transforms.get(ref)!;
         const def = transform.transformer.definition;
         if (def.params && def.params.areEqual) {
             if (def.params.areEqual(transform.params, params)) return false;
@@ -141,7 +141,7 @@ class TransientTree implements StateTree {
     }
 
     setCellState(ref: Transform.Ref, state: Partial<StateObjectCell.State>) {
-        ensurePresent(this.nodes, ref);
+        ensurePresent(this.transforms, ref);
 
         if (this._transformMutations && this._transformMutations.has(ref)) {
             const transform = this._transformMutations.get(ref)!;
@@ -149,7 +149,7 @@ class TransientTree implements StateTree {
             (transform.cellState as StateObjectCell.State) = { ...old, ...state };
             return transform;
         } else {
-            const transform = this.nodes.get(ref);
+            const transform = this.transforms.get(ref);
             const newT = Transform.withCellState(transform, state);
             this.set(newT);
             return newT;
@@ -157,11 +157,11 @@ class TransientTree implements StateTree {
     }
 
     private set(transform: Transform) {
-        ensurePresent(this.nodes, transform.ref);
+        ensurePresent(this.transforms, transform.ref);
 
         if (!this.changedNodes) {
             this.changedNodes = true;
-            this.nodes = this.nodes.asMutable();
+            this.transforms = this.transforms.asMutable();
         }
 
         if (!this._transformMutations) {
@@ -169,12 +169,12 @@ class TransientTree implements StateTree {
         }
         this._transformMutations.set(transform.ref, transform);
 
-        this.nodes.set(transform.ref, transform);
+        this.transforms.set(transform.ref, transform);
         return this;
     }
 
     remove(ref: Transform.Ref): Transform[] {
-        const node = this.nodes.get(ref);
+        const node = this.transforms.get(ref);
         if (!node) return [];
 
         const st = StateTree.subtreePostOrder(this, node);
@@ -189,11 +189,11 @@ class TransientTree implements StateTree {
 
         if (!this.changedNodes) {
             this.changedNodes = true;
-            this.nodes = this.nodes.asMutable();
+            this.transforms = this.transforms.asMutable();
         }
 
         for (const n of st) {
-            this.nodes.delete(n.ref);
+            this.transforms.delete(n.ref);
             this.children.delete(n.ref);
             if (this._childMutations) this._childMutations.delete(n.ref);
         }
@@ -205,7 +205,7 @@ class TransientTree implements StateTree {
         if (!this.changedNodes && !this.changedChildren && !this._childMutations) return this.tree;
         if (this._childMutations) this._childMutations.forEach(fixChildMutations, this.children);
         return StateTree.create(
-            this.changedNodes ? this.nodes.asImmutable() : this.nodes,
+            this.changedNodes ? this.transforms.asImmutable() : this.transforms,
             this.changedChildren ? this.children.asImmutable() : this.children);
     }