Browse Source

mol-state: use PD.normalizeParams first time a cell is evaluated

dsehnal 4 years ago
parent
commit
87c504f9a8
3 changed files with 24 additions and 10 deletions
  1. 2 0
      src/mol-state/object.ts
  2. 15 6
      src/mol-state/state.ts
  3. 7 4
      src/mol-util/param-definition.ts

+ 2 - 0
src/mol-state/object.ts

@@ -80,6 +80,8 @@ interface StateObjectCell<T extends StateObject = StateObject, F extends StateTr
         values: any
     } | undefined,
 
+    paramsNormalized: boolean,
+
     dependencies: {
         dependentBy: StateObjectCell[],
         dependsOn: StateObjectCell[]

+ 15 - 6
src/mol-state/state.ts

@@ -378,6 +378,7 @@ class State {
                 definition: {},
                 values: {}
             },
+            paramsNormalized: true,
             dependencies: { dependentBy: [], dependsOn: [] },
             cache: { }
         });
@@ -662,6 +663,7 @@ function addCellsVisitor(transform: StateTransform, _: any, { ctx, added, visite
         state: { ...transform.state },
         errorText: void 0,
         params: void 0,
+        paramsNormalized: false,
         dependencies: { dependentBy: [], dependsOn: [] },
         cache: void 0
     };
@@ -840,13 +842,20 @@ async function updateSubtree(ctx: UpdateContext, root: Ref) {
     }
 }
 
-function resolveParams(ctx: UpdateContext, transform: StateTransform, src: StateObject) {
+function resolveParams(ctx: UpdateContext, transform: StateTransform, src: StateObject, cell: StateObjectCell) {
     const prms = transform.transformer.definition.params;
     const definition = prms ? prms(src, ctx.parent.globalContext) : {};
-    const defaultValues = ParamDefinition.getDefaultValues(definition);
-    (transform.params as any) = transform.params
-        ? assignIfUndefined(transform.params, defaultValues)
-        : defaultValues;
+
+    if (!cell.paramsNormalized) {
+        (transform.params as any) = ParamDefinition.normalizeParams(definition, transform.params, true);
+        cell.paramsNormalized = true;
+    } else {
+        const defaultValues = ParamDefinition.getDefaultValues(definition);
+        (transform.params as any) = transform.params
+            ? assignIfUndefined(transform.params, defaultValues)
+            : defaultValues;
+    }
+
     ParamDefinition.resolveRefs(definition, transform.params, ctx.getCellData);
     return { definition, values: transform.params };
 }
@@ -873,7 +882,7 @@ async function updateNode(ctx: UpdateContext, currentRef: Ref): Promise<UpdateNo
     const parent = parentCell.obj!;
     current.sourceRef = parentCell.transform.ref;
 
-    const params = resolveParams(ctx, transform, parent);
+    const params = resolveParams(ctx, transform, parent, current);
 
     if (!oldTree.transforms.has(currentRef) || !current.params) {
         current.params = params;

+ 7 - 4
src/mol-util/param-definition.ts

@@ -555,11 +555,14 @@ export namespace ParamDefinition {
             return defaultIfUndefined ? p.defaultValue : void 0;
         }
 
-        if (typeof p.defaultValue !== typeof value) {
-            return p.defaultValue;
-        }
+        // TODO: is this a good idea and will work well?
+        // if (typeof p.defaultValue !== typeof value) {
+        //     return p.defaultValue;
+        // }
 
-        if (p.type === 'group') {
+        if (p.type === 'value') {
+            return value;
+        } else if (p.type === 'group') {
             const ret = { ...value };
             for (const key of Object.keys(p.params)) {
                 const param = p.params[key];