Переглянути джерело

mol-state: added StateObject.Null

David Sehnal 6 роки тому
батько
коміт
157f811a40
3 змінених файлів з 21 додано та 3 видалено
  1. 3 1
      src/mol-plugin/ui/state-tree.tsx
  2. 9 1
      src/mol-state/object.ts
  3. 9 1
      src/mol-state/state.ts

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

@@ -6,7 +6,7 @@
 
 import * as React from 'react';
 import { PluginStateObject } from 'mol-plugin/state/objects';
-import { State } from 'mol-state'
+import { State, StateObject } from 'mol-state'
 import { PluginCommands } from 'mol-plugin/command';
 import { PluginComponent } from './base';
 
@@ -60,6 +60,8 @@ class StateTreeNode extends PluginComponent<{ nodeRef: string, state: State }, {
     }
 
     render() {
+        if (this.props.state.cells.get(this.props.nodeRef)!.obj === StateObject.Null) return null;
+
         const cellState = this.cellState;
 
         const children = this.props.state.tree.children.get(this.props.nodeRef);

+ 9 - 1
src/mol-state/object.ts

@@ -9,7 +9,7 @@ import { Transform } from './transform';
 
 export { StateObject, StateObjectCell }
 
-interface StateObject<D = any, T extends StateObject.Type = { name: string, typeClass: any }> {
+interface StateObject<D = any, T extends StateObject.Type = StateObject.Type<any>> {
     readonly id: UUID,
     readonly type: T,
     readonly data: D,
@@ -39,6 +39,14 @@ namespace StateObject {
             }
         }
     }
+
+    /** A special object indicating a transformer result has no value. */
+    export const Null: StateObject<any, any> = {
+        id: UUID.create22(),
+        type: { name: 'Null', typeClass: 'Null' },
+        data: void 0,
+        label: 'Null'
+    };
 }
 
 interface StateObjectCell {

+ 9 - 1
src/mol-state/state.ts

@@ -441,6 +441,7 @@ type UpdateNodeResult =
 async function updateSubtree(ctx: UpdateContext, root: Ref) {
     setCellStatus(ctx, root, 'processing');
 
+    let isNull = false;
     try {
         const start = now();
         const update = await updateNode(ctx, root);
@@ -451,10 +452,13 @@ async function updateSubtree(ctx: UpdateContext, root: Ref) {
         setCellStatus(ctx, root, 'ok');
         ctx.results.push(update);
         if (update.action === 'created') {
+            isNull = update.obj === StateObject.Null;
             ctx.parent.events.log.next(LogEntry.info(`Created ${update.obj.label} in ${formatTimespan(time)}.`));
         } else if (update.action === 'updated') {
+            isNull = update.obj === StateObject.Null;
             ctx.parent.events.log.next(LogEntry.info(`Updated ${update.obj.label} in ${formatTimespan(time)}.`));
         } else if (update.action === 'replaced') {
+            isNull = update.obj === StateObject.Null;
             ctx.parent.events.log.next(LogEntry.info(`Updated ${update.obj.label} in ${formatTimespan(time)}.`));
         }
     } catch (e) {
@@ -464,6 +468,10 @@ async function updateSubtree(ctx: UpdateContext, root: Ref) {
         return;
     }
 
+    // Do not continue the updates if the object is null
+    // TODO: set the states to something "nicer"?
+    if (isNull) return;
+
     const children = ctx.tree.children.get(root).values();
     while (true) {
         const next = children.next();
@@ -497,7 +505,7 @@ async function updateNode(ctx: UpdateContext, currentRef: Ref): Promise<UpdateNo
     } else {
         const oldParams = oldTree.transforms.get(currentRef)!.params;
 
-        const updateKind = !!current.obj
+        const updateKind = !!current.obj && current.obj !== StateObject.Null
             ? await updateObject(ctx, currentRef, transform.transformer, parent, current.obj!, oldParams, transform.params)
             : Transformer.UpdateResult.Recreate;