Skip to content

Commit 8433242

Browse files
committed
Merge pull request plotly#427 from plotly/test-store-datatypes
Test store data types update & fixes
1 parent 8ba6e3d commit 8433242

File tree

10 files changed

+183
-82
lines changed

10 files changed

+183
-82
lines changed

packages/dash-core-components/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
All notable changes to this project will be documented in this file.
33
This project adheres to [Semantic Versioning](http://semver.org/).
44

5+
## [0.42.1] - 2019-01-07
6+
### Fixed
7+
- Fix `dcc.Store` type changes [#427](https://github.com/plotly/dash-core-components/pull/427)
8+
59
## [0.42.0] - 2018-12-27
610
### Fixed
711
- Fix `dcc.Store` null values in list causing an infinite loop [#424](https://github.com/plotly/dash-core-components/pull/424)

packages/dash-core-components/dash_core_components/dash_core_components.dev.js

Lines changed: 60 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -114609,42 +114609,65 @@ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
114609114609

114610114610

114611114611

114612+
/**
114613+
* Deep equality check to know if the data has changed.
114614+
*
114615+
* @param {*} newData - New data to compare
114616+
* @param {*} oldData - The old data to compare
114617+
* @returns {boolean} The data has changed.
114618+
*/
114612114619

114613-
function dataCheck(data, old) {
114614-
// Assuming data and old are of the same type.
114615-
var oldNull = ramda__WEBPACK_IMPORTED_MODULE_0___default.a.isNil(old);
114616-
var newNull = ramda__WEBPACK_IMPORTED_MODULE_0___default.a.isNil(data);
114620+
function dataChanged(newData, oldData) {
114621+
var oldNull = Object(ramda__WEBPACK_IMPORTED_MODULE_0__["isNil"])(oldData);
114622+
var newNull = Object(ramda__WEBPACK_IMPORTED_MODULE_0__["isNil"])(newData);
114617114623

114618-
if ((oldNull || newNull) && !(oldNull && newNull)) {
114619-
return true;
114624+
if (oldNull || newNull) {
114625+
return newData !== oldData;
114620114626
}
114621114627

114622-
var type = ramda__WEBPACK_IMPORTED_MODULE_0___default.a.type(data);
114628+
var newType = Object(ramda__WEBPACK_IMPORTED_MODULE_0__["type"])(newData);
114623114629

114624-
if (type === 'Array') {
114625-
if (data.length !== old.length) {
114630+
if (newType !== Object(ramda__WEBPACK_IMPORTED_MODULE_0__["type"])(oldData)) {
114631+
return true;
114632+
}
114633+
114634+
if (newType === 'Array') {
114635+
if (newData.length !== oldData.length) {
114626114636
return true;
114627114637
}
114628114638

114629-
for (var i = 0; i < data.length; i++) {
114630-
if (dataCheck(data[i], old[i])) {
114639+
for (var i = 0; i < newData.length; i++) {
114640+
if (dataChanged(newData[i], oldData[i])) {
114631114641
return true;
114632114642
}
114633114643
}
114634-
} else if (ramda__WEBPACK_IMPORTED_MODULE_0___default.a.contains(type, ['String', 'Number'])) {
114635-
return old !== data;
114636-
} else if (type === 'Object') {
114637-
return ramda__WEBPACK_IMPORTED_MODULE_0___default.a.any(function (_ref) {
114644+
} else if (Object(ramda__WEBPACK_IMPORTED_MODULE_0__["contains"])(newType, ['String', 'Number', 'Boolean'])) {
114645+
return oldData !== newData;
114646+
} else if (newType === 'Object') {
114647+
var oldEntries = Object.entries(oldData);
114648+
var newEntries = Object.entries(newData);
114649+
114650+
if (oldEntries.length !== newEntries.length) {
114651+
return true;
114652+
}
114653+
114654+
return Object(ramda__WEBPACK_IMPORTED_MODULE_0__["any"])(function (_ref) {
114638114655
var _ref2 = _slicedToArray(_ref, 2),
114639114656
k = _ref2[0],
114640114657
v = _ref2[1];
114641114658

114642-
return dataCheck(v, old[k]);
114643-
})(Object.entries(data));
114659+
return dataChanged(v, oldData[k]);
114660+
})(newEntries);
114644114661
}
114645114662

114646114663
return false;
114647114664
}
114665+
/**
114666+
* Abstraction for the memory storage_type to work the same way as local/session
114667+
*
114668+
* Each memory Store component get it's own MemStore.
114669+
*/
114670+
114648114671

114649114672
var MemStore =
114650114673
/*#__PURE__*/
@@ -114689,6 +114712,12 @@ function () {
114689114712

114690114713
return MemStore;
114691114714
}();
114715+
/**
114716+
* Abstraction for local/session storage_type.
114717+
*
114718+
* Single instances for localStorage, sessionStorage
114719+
*/
114720+
114692114721

114693114722
var WebStore =
114694114723
/*#__PURE__*/
@@ -114797,7 +114826,7 @@ function (_React$Component) {
114797114826

114798114827
var old = this._backstore.getItem(id);
114799114828

114800-
if (ramda__WEBPACK_IMPORTED_MODULE_0___default.a.isNil(old) && data) {
114829+
if (Object(ramda__WEBPACK_IMPORTED_MODULE_0__["isNil"])(old) && data) {
114801114830
// Initial data mount
114802114831
this._backstore.setItem(id, data);
114803114832

@@ -114810,7 +114839,7 @@ function (_React$Component) {
114810114839
return;
114811114840
}
114812114841

114813-
if (setProps && dataCheck(old, data)) {
114842+
if (setProps && dataChanged(old, data)) {
114814114843
setProps({
114815114844
data: old,
114816114845
modified_timestamp: this._backstore.getModified(id)
@@ -114843,18 +114872,20 @@ function (_React$Component) {
114843114872
modified_timestamp: this._backstore.getModified(id)
114844114873
});
114845114874
}
114846-
} else if (data) {
114847-
var old = this._backstore.getItem(id); // Only set the data if it's not the same data.
114848114875

114876+
return;
114877+
}
114849114878

114850-
if (dataCheck(data, old)) {
114851-
this._backstore.setItem(id, data);
114879+
var old = this._backstore.getItem(id); // Only set the data if it's not the same data.
114852114880

114853-
if (setProps) {
114854-
setProps({
114855-
modified_timestamp: this._backstore.getModified(id)
114856-
});
114857-
}
114881+
114882+
if (dataChanged(data, old)) {
114883+
this._backstore.setItem(id, data);
114884+
114885+
if (setProps) {
114886+
setProps({
114887+
modified_timestamp: this._backstore.getModified(id)
114888+
});
114858114889
}
114859114890
}
114860114891
}
@@ -114892,7 +114923,7 @@ Store.propTypes = {
114892114923
/**
114893114924
* The stored data for the id.
114894114925
*/
114895-
data: prop_types__WEBPACK_IMPORTED_MODULE_2___default.a.oneOfType([prop_types__WEBPACK_IMPORTED_MODULE_2___default.a.object, prop_types__WEBPACK_IMPORTED_MODULE_2___default.a.array, prop_types__WEBPACK_IMPORTED_MODULE_2___default.a.number, prop_types__WEBPACK_IMPORTED_MODULE_2___default.a.string]),
114926+
data: prop_types__WEBPACK_IMPORTED_MODULE_2___default.a.oneOfType([prop_types__WEBPACK_IMPORTED_MODULE_2___default.a.object, prop_types__WEBPACK_IMPORTED_MODULE_2___default.a.array, prop_types__WEBPACK_IMPORTED_MODULE_2___default.a.number, prop_types__WEBPACK_IMPORTED_MODULE_2___default.a.string, prop_types__WEBPACK_IMPORTED_MODULE_2___default.a.bool]),
114896114927

114897114928
/**
114898114929
* Set to true to remove the data contained in `data_key`.

packages/dash-core-components/dash_core_components/dash_core_components.dev.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/dash-core-components/dash_core_components/dash_core_components.min.js

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/dash-core-components/dash_core_components/dash_core_components.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/dash-core-components/dash_core_components/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dash-core-components",
3-
"version": "0.42.0",
3+
"version": "0.42.1",
44
"description": "Core component suite for Dash",
55
"repository": {
66
"type": "git",
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '0.42.0'
1+
__version__ = '0.42.1'

packages/dash-core-components/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dash-core-components",
3-
"version": "0.42.0",
3+
"version": "0.42.1",
44
"description": "Core component suite for Dash",
55
"repository": {
66
"type": "git",

packages/dash-core-components/src/components/Store.react.js

Lines changed: 52 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,51 @@
1-
import R from 'ramda';
1+
import {isNil, type, contains, any} from 'ramda';
22
import React from 'react';
33
import PropTypes from 'prop-types';
44

5-
function dataCheck(data, old) {
6-
// Assuming data and old are of the same type.
7-
const oldNull = R.isNil(old);
8-
const newNull = R.isNil(data);
5+
/**
6+
* Deep equality check to know if the data has changed.
7+
*
8+
* @param {*} newData - New data to compare
9+
* @param {*} oldData - The old data to compare
10+
* @returns {boolean} The data has changed.
11+
*/
12+
function dataChanged(newData, oldData) {
13+
const oldNull = isNil(oldData);
14+
const newNull = isNil(newData);
915
if (oldNull || newNull) {
10-
return oldNull !== newNull;
16+
return newData !== oldData;
17+
}
18+
const newType = type(newData);
19+
if (newType !== type(oldData)) {
20+
return true;
1121
}
12-
const type = R.type(data);
13-
if (type === 'Array') {
14-
if (data.length !== old.length) {
22+
if (newType === 'Array') {
23+
if (newData.length !== oldData.length) {
1524
return true;
1625
}
17-
for (let i = 0; i < data.length; i++) {
18-
if (dataCheck(data[i], old[i])) {
26+
for (let i = 0; i < newData.length; i++) {
27+
if (dataChanged(newData[i], oldData[i])) {
1928
return true;
2029
}
2130
}
22-
} else if (R.contains(type, ['String', 'Number'])) {
23-
return old !== data;
24-
} else if (type === 'Object') {
25-
return R.any(([k, v]) => dataCheck(v, old[k]))(Object.entries(data));
31+
} else if (contains(newType, ['String', 'Number', 'Boolean'])) {
32+
return oldData !== newData;
33+
} else if (newType === 'Object') {
34+
const oldEntries = Object.entries(oldData);
35+
const newEntries = Object.entries(newData);
36+
if (oldEntries.length !== newEntries.length) {
37+
return true;
38+
}
39+
return any(([k, v]) => dataChanged(v, oldData[k]))(newEntries);
2640
}
2741
return false;
2842
}
2943

44+
/**
45+
* Abstraction for the memory storage_type to work the same way as local/session
46+
*
47+
* Each memory Store component get it's own MemStore.
48+
*/
3049
class MemStore {
3150
constructor() {
3251
this._data = {};
@@ -58,6 +77,11 @@ class MemStore {
5877
}
5978
}
6079

80+
/**
81+
* Abstraction for local/session storage_type.
82+
*
83+
* Single instances for localStorage, sessionStorage
84+
*/
6185
class WebStore {
6286
constructor(storage) {
6387
this._storage = storage;
@@ -129,7 +153,7 @@ export default class Store extends React.Component {
129153
}
130154

131155
const old = this._backstore.getItem(id);
132-
if (R.isNil(old) && data) {
156+
if (isNil(old) && data) {
133157
// Initial data mount
134158
this._backstore.setItem(id, data);
135159
if (setProps) {
@@ -140,7 +164,7 @@ export default class Store extends React.Component {
140164
return;
141165
}
142166

143-
if (setProps && dataCheck(old, data)) {
167+
if (setProps && dataChanged(old, data)) {
144168
setProps({
145169
data: old,
146170
modified_timestamp: this._backstore.getModified(id),
@@ -165,16 +189,16 @@ export default class Store extends React.Component {
165189
modified_timestamp: this._backstore.getModified(id),
166190
});
167191
}
168-
} else if (data) {
169-
const old = this._backstore.getItem(id);
170-
// Only set the data if it's not the same data.
171-
if (dataCheck(data, old)) {
172-
this._backstore.setItem(id, data);
173-
if (setProps) {
174-
setProps({
175-
modified_timestamp: this._backstore.getModified(id),
176-
});
177-
}
192+
return;
193+
}
194+
const old = this._backstore.getItem(id);
195+
// Only set the data if it's not the same data.
196+
if (dataChanged(data, old)) {
197+
this._backstore.setItem(id, data);
198+
if (setProps) {
199+
setProps({
200+
modified_timestamp: this._backstore.getModified(id),
201+
});
178202
}
179203
}
180204
}
@@ -213,6 +237,7 @@ Store.propTypes = {
213237
PropTypes.array,
214238
PropTypes.number,
215239
PropTypes.string,
240+
PropTypes.bool,
216241
]),
217242

218243
/**

0 commit comments

Comments
 (0)