Ver código fonte

improved shallowEqual

- old shallowEqual is now shallowEqualObjects
- new shallowEqual supports objects, primitives and arrays
Alexander Rose 5 anos atrás
pai
commit
bc07256d8e

+ 2 - 2
src/mol-plugin-ui/state/snapshots.tsx

@@ -7,7 +7,7 @@
 import { PluginCommands } from '../../mol-plugin/command';
 import * as React from 'react';
 import { PluginUIComponent, PurePluginUIComponent } from '../base';
-import { shallowEqual } from '../../mol-util';
+import { shallowEqualObjects } from '../../mol-util';
 import { OrderedMap } from 'immutable';
 import { ParameterControls } from '../controls/parameters';
 import { ParamDefinition as PD} from '../../mol-util/param-definition';
@@ -81,7 +81,7 @@ class LocalStateSnapshots extends PluginUIComponent<
     }
 
     shouldComponentUpdate(nextProps: any, nextState: any) {
-        return !shallowEqual(this.props, nextProps) || !shallowEqual(this.state, nextState);
+        return !shallowEqualObjects(this.props, nextProps) || !shallowEqualObjects(this.state, nextState);
     }
 
     render() {

+ 2 - 2
src/mol-plugin/behavior/behavior.ts

@@ -11,7 +11,7 @@ import { PluginContext } from '../../mol-plugin/context';
 import { PluginCommand } from '../command';
 import { Observable } from 'rxjs';
 import { ParamDefinition } from '../../mol-util/param-definition';
-import { shallowEqual } from '../../mol-util';
+import { shallowEqualObjects } from '../../mol-util';
 
 export { PluginBehavior }
 
@@ -128,7 +128,7 @@ namespace PluginBehavior {
             this.subs = [];
         }
         update(params: P): boolean | Promise<boolean> {
-            if (shallowEqual(params, this.params)) return false;
+            if (shallowEqualObjects(params, this.params)) return false;
             this.params = params;
             return true;
         }

+ 2 - 2
src/mol-plugin/state/transforms/misc.ts

@@ -5,7 +5,7 @@
  */
 
 import { StateTransformer } from '../../../mol-state';
-import { shallowEqual } from '../../../mol-util';
+import { shallowEqualObjects } from '../../../mol-util';
 import { ParamDefinition as PD } from '../../../mol-util/param-definition';
 import { PluginStateObject as SO, PluginStateTransform } from '../objects';
 
@@ -25,7 +25,7 @@ const CreateGroup = PluginStateTransform.BuiltIn({
         return new SO.Group({}, params);
     },
     update({ oldParams, newParams, b }) {
-        if (shallowEqual(oldParams, newParams)) return StateTransformer.UpdateResult.Unchanged;
+        if (shallowEqualObjects(oldParams, newParams)) return StateTransformer.UpdateResult.Unchanged;
         b.label = newParams.label;
         b.description = newParams.description;
         return StateTransformer.UpdateResult.Updated;

+ 25 - 8
src/mol-util/index.ts

@@ -1,5 +1,5 @@
 /**
- * Copyright (c) 2017-2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
+ * Copyright (c) 2017-2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  *
  * @author David Sehnal <david.sehnal@gmail.com>
  * @author Alexander Rose <alexander.rose@weirdbyte.de>
@@ -79,19 +79,36 @@ export function deepEqual(a: any, b: any) {
     return false
 }
 
-export function shallowEqual<T>(a: T, b: T) {
-    if (!a) {
-        if (!b) return true;
-        return false;
+export function shallowEqual(a: any, b: any) {
+    if (a === b) return true;
+    const arrA = Array.isArray(a)
+    const arrB = Array.isArray(b)
+    if (arrA && arrB) return shallowEqualArrays(a, b)
+    if (arrA !== arrB) return false
+    if (a && b && typeof a === 'object' && typeof b === 'object') {
+        return shallowEqualObjects(a, b)
     }
-    if (!b) return false;
+    return false
+}
 
-    let keys = Object.keys(a);
+export function shallowEqualObjects(a: {}, b: {}) {
+    if (a === b) return true;
+    if (!a || !b) return false;
+    const keys = Object.keys(a);
     if (Object.keys(b).length !== keys.length) return false;
-    for (let k of keys) {
+    for (const k of keys) {
         if (!hasOwnProperty.call(a, k) || (a as any)[k] !== (b as any)[k]) return false;
     }
+    return true;
+}
 
+export default function shallowEqualArrays(a: any[], b: any[]) {
+    if (a === b) return true;
+    if (!a || !b) return false;
+    if (a.length !== b.length) return false;
+    for (let i = 0, il = a.length; i < il; ++i) {
+        if (a[i] !== b[i]) return false;
+    }
     return true;
 }
 

+ 2 - 2
src/mol-util/param-definition.ts

@@ -6,7 +6,7 @@
  */
 
 import { Color as ColorData } from './color';
-import { shallowEqual } from './index';
+import { shallowEqualObjects } from './index';
 import { Vec2 as Vec2Data, Vec3 as Vec3Data } from '../mol-math/linear-algebra';
 import { deepClone } from './object';
 import { Script as ScriptData } from '../mol-script/script';
@@ -356,7 +356,7 @@ export namespace ParamDefinition {
             }
             return true;
         } else if (typeof a === 'object' && typeof b === 'object') {
-            return shallowEqual(a, b);
+            return shallowEqualObjects(a, b);
         }
 
         // a === b was checked at the top.