Skip to content
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

Adjust imaginary tickvals default to fix react #5992

Merged
merged 4 commits into from
Oct 29, 2021
Merged

Conversation

archmoj
Copy link
Contributor

@archmoj archmoj commented Oct 19, 2021

The magical default for imaginary tickvals added in #5956 (comment) broke the react ("noCi" plotl_api_react_test).

@plotly/plotly_js

@archmoj archmoj added bug something broken status: reviewable labels Oct 19, 2021
@archmoj archmoj requested a review from alexcjohnson October 19, 2021 17:46
@archmoj archmoj added this to the v2.6.0 milestone Oct 20, 2021
var imagTickvalsDflt =
realTickvals.slice().reverse().map(function(x) { return -x; })
.concat([0])
.concat(realTickvals);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I imagine the problem with this is we get a new array each time, so Plotly.react thinks this has changed and needs replotting? I do think as a default this behavior makes sense, can we fix it with a simple memoizer? Something like:

function memoize(fn, keyFn) {
    var cache = {};
    return function(val) {
        var newKey = keyFn ? keyFn(val) : val;
        if (newKey in cache) { return cache[newKey]; }
        out = fn(val);
        cache[newKey] = out;
        return out;
    }
}

var makeImagDflt = memoizeOne(function(realTickvals) {
    return realTickvals.slice().reverse().map(function(x) { return -x; })
        .concat([0])
        .concat(realTickvals);
}, String);

var a = makeImagDflt([2,4,5])
var b = makeImagDflt([2,4,5])
a===b // true

var c = makeImagDflt([1,2,3])
var d = makeImagDflt([2,4,5])
d===b // true

A memoizer might be useful in other contexts too... we use them quite a lot in Dash anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a kind of magic!
Applied in 844e24c

Copy link
Collaborator

@alexcjohnson alexcjohnson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💃 🧙

@archmoj archmoj changed the title Simplify imaginary tickvals default to fix react Adjust imaginary tickvals default to fix react Oct 29, 2021
@archmoj archmoj merged commit d38232d into master Oct 29, 2021
@archmoj archmoj deleted the fixup-smith-react-test branch October 29, 2021 20:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug something broken
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants