Skip to content
Snippets Groups Projects
Commit bc07256d authored by Alexander Rose's avatar Alexander Rose
Browse files

improved shallowEqual

- old shallowEqual is now shallowEqualObjects
- new shallowEqual supports objects, primitives and arrays
parent 3826bdb0
No related branches found
No related tags found
No related merge requests found
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
import { PluginCommands } from '../../mol-plugin/command'; import { PluginCommands } from '../../mol-plugin/command';
import * as React from 'react'; import * as React from 'react';
import { PluginUIComponent, PurePluginUIComponent } from '../base'; import { PluginUIComponent, PurePluginUIComponent } from '../base';
import { shallowEqual } from '../../mol-util'; import { shallowEqualObjects } from '../../mol-util';
import { OrderedMap } from 'immutable'; import { OrderedMap } from 'immutable';
import { ParameterControls } from '../controls/parameters'; import { ParameterControls } from '../controls/parameters';
import { ParamDefinition as PD} from '../../mol-util/param-definition'; import { ParamDefinition as PD} from '../../mol-util/param-definition';
...@@ -81,7 +81,7 @@ class LocalStateSnapshots extends PluginUIComponent< ...@@ -81,7 +81,7 @@ class LocalStateSnapshots extends PluginUIComponent<
} }
shouldComponentUpdate(nextProps: any, nextState: any) { shouldComponentUpdate(nextProps: any, nextState: any) {
return !shallowEqual(this.props, nextProps) || !shallowEqual(this.state, nextState); return !shallowEqualObjects(this.props, nextProps) || !shallowEqualObjects(this.state, nextState);
} }
render() { render() {
......
...@@ -11,7 +11,7 @@ import { PluginContext } from '../../mol-plugin/context'; ...@@ -11,7 +11,7 @@ import { PluginContext } from '../../mol-plugin/context';
import { PluginCommand } from '../command'; import { PluginCommand } from '../command';
import { Observable } from 'rxjs'; import { Observable } from 'rxjs';
import { ParamDefinition } from '../../mol-util/param-definition'; import { ParamDefinition } from '../../mol-util/param-definition';
import { shallowEqual } from '../../mol-util'; import { shallowEqualObjects } from '../../mol-util';
export { PluginBehavior } export { PluginBehavior }
...@@ -128,7 +128,7 @@ namespace PluginBehavior { ...@@ -128,7 +128,7 @@ namespace PluginBehavior {
this.subs = []; this.subs = [];
} }
update(params: P): boolean | Promise<boolean> { update(params: P): boolean | Promise<boolean> {
if (shallowEqual(params, this.params)) return false; if (shallowEqualObjects(params, this.params)) return false;
this.params = params; this.params = params;
return true; return true;
} }
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
*/ */
import { StateTransformer } from '../../../mol-state'; 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 { ParamDefinition as PD } from '../../../mol-util/param-definition';
import { PluginStateObject as SO, PluginStateTransform } from '../objects'; import { PluginStateObject as SO, PluginStateTransform } from '../objects';
...@@ -25,7 +25,7 @@ const CreateGroup = PluginStateTransform.BuiltIn({ ...@@ -25,7 +25,7 @@ const CreateGroup = PluginStateTransform.BuiltIn({
return new SO.Group({}, params); return new SO.Group({}, params);
}, },
update({ oldParams, newParams, b }) { 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.label = newParams.label;
b.description = newParams.description; b.description = newParams.description;
return StateTransformer.UpdateResult.Updated; return StateTransformer.UpdateResult.Updated;
......
/** /**
* 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 David Sehnal <david.sehnal@gmail.com>
* @author Alexander Rose <alexander.rose@weirdbyte.de> * @author Alexander Rose <alexander.rose@weirdbyte.de>
...@@ -79,19 +79,36 @@ export function deepEqual(a: any, b: any) { ...@@ -79,19 +79,36 @@ export function deepEqual(a: any, b: any) {
return false return false
} }
export function shallowEqual<T>(a: T, b: T) { export function shallowEqual(a: any, b: any) {
if (!a) { if (a === b) return true;
if (!b) return true; const arrA = Array.isArray(a)
return false; 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)
}
return false
} }
if (!b) 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; 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; 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; return true;
} }
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
*/ */
import { Color as ColorData } from './color'; 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 { Vec2 as Vec2Data, Vec3 as Vec3Data } from '../mol-math/linear-algebra';
import { deepClone } from './object'; import { deepClone } from './object';
import { Script as ScriptData } from '../mol-script/script'; import { Script as ScriptData } from '../mol-script/script';
...@@ -356,7 +356,7 @@ export namespace ParamDefinition { ...@@ -356,7 +356,7 @@ export namespace ParamDefinition {
} }
return true; return true;
} else if (typeof a === 'object' && typeof b === 'object') { } else if (typeof a === 'object' && typeof b === 'object') {
return shallowEqual(a, b); return shallowEqualObjects(a, b);
} }
// a === b was checked at the top. // a === b was checked at the top.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment