-
-
Notifications
You must be signed in to change notification settings - Fork 2k
fix: Update config diff check method to handle nested arrays #7579
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
b480d04
34a669e
364466a
3b3816f
20c534f
47be541
ef102cc
1b65938
0da832e
2767f07
01e698a
8ceebdf
3bc5b7e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| - Update config diff check method to handle nested arrays [[#7579](https://github.com/plotly/plotly.js/pull/7579)] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -455,7 +455,7 @@ function getParent(attr) { | |
| if (tail > 0) return attr.substr(0, tail); | ||
| } | ||
|
|
||
| /* | ||
| /** | ||
| * hasParent: does an attribute object contain a parent of the given attribute? | ||
| * for example, given 'images[2].x' do we also have 'images' or 'images[2]'? | ||
| * | ||
|
|
@@ -475,6 +475,7 @@ exports.hasParent = function (aobj, attr) { | |
| return false; | ||
| }; | ||
|
|
||
| const AX_LETTERS = ['x', 'y', 'z']; | ||
| /** | ||
| * Empty out types for all axes containing these traces so we auto-set them again | ||
| * | ||
|
|
@@ -483,12 +484,11 @@ exports.hasParent = function (aobj, attr) { | |
| * @param {object} layoutUpdate: any update being done concurrently to the layout, | ||
| * which may supercede clearing the axis types | ||
| */ | ||
| var axLetters = ['x', 'y', 'z']; | ||
| exports.clearAxisTypes = function (gd, traces, layoutUpdate) { | ||
| for (var i = 0; i < traces.length; i++) { | ||
| var trace = gd._fullData[i]; | ||
| for (var j = 0; j < 3; j++) { | ||
| var ax = getFromTrace(gd, trace, axLetters[j]); | ||
| var ax = getFromTrace(gd, trace, AX_LETTERS[j]); | ||
|
|
||
| // do not clear log type - that's never an auto result so must have been intentional | ||
| if (ax && ax.type !== 'log') { | ||
|
|
@@ -507,3 +507,35 @@ exports.clearAxisTypes = function (gd, traces, layoutUpdate) { | |
| } | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * Check if a collection (object or array) has changed given two versions of | ||
| * the collection: old and new. | ||
| * | ||
| * @param {Object|Array} oldCollection: Old version of collection to compare | ||
| * @param {Object|Array} newCollection: New version of collection to compare | ||
| */ | ||
| const hasCollectionChanged = (oldCollection, newCollection) => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just a nit, but I would expect this function to be called something slightly more general like
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was trying to be generic with the term "collection" since you could be passing in an object or an array, but we could go further as you suggest. Would
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, Actually, one related question — what is the reason for ignoring keys starting with an underscore? Is that due to a JavaScript implementation detail, or a Plotly.js one? Maybe that should be captured in the function name (since it's a very particular definition of equality)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a plotly.js detail, but it's also a JavaScript convention. Variables that start with underscore are labeled as such because they are meant to be used internally by the library. So, you wouldn't see a user changing those values, hence they can be ignored. |
||
| const isArrayOrObject = (...vals) => vals.every((v) => Lib.isPlainObject(v)) || vals.every((v) => Array.isArray(v)); | ||
| if ([oldCollection, newCollection].every((a) => Array.isArray(a))) { | ||
| if (oldCollection.length !== newCollection.length) return true; | ||
|
|
||
| for (let i = 0; i < oldCollection.length; i++) { | ||
| const oldVal = oldCollection[i]; | ||
| const newVal = newCollection[i]; | ||
| if (oldVal !== newVal) return isArrayOrObject(oldVal, newVal) ? hasCollectionChanged(oldVal, newVal) : true; | ||
| } | ||
| } else { | ||
| if (Object.keys(oldCollection).length !== Object.keys(newCollection).length) return true; | ||
|
|
||
| for (const k in oldCollection) { | ||
| if (k.startsWith('_')) return false; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this seems wrong, am I missing something? Is it supposed to be
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wait, looking closely this whole loop is problematic; it should only ever return
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch! I did a poor job of converting this loop from it's previous iteration. I updated it to only return when |
||
| const oldVal = oldCollection[k]; | ||
| const newVal = newCollection[k]; | ||
| if (oldVal !== newVal) return isArrayOrObject(oldVal, newVal) ? hasCollectionChanged(oldVal, newVal) : true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| }; | ||
| exports.hasCollectionChanged = hasCollectionChanged; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
put at top of file perhaps? I would normally expect constants not inside a function to be defined at the top of the file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can do.