Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@
},
"peerDependencies": {
"plotly.js": ">1.34.0",
"react": ">0.13.0"
"react": ">0.13.0",
"flyd": ">0.2.8",
"ramda": ">0.28.0"
},
"browserify-global-shim": {
"react": "React"
Expand Down
50 changes: 50 additions & 0 deletions src/usePlotly.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { useLayoutEffect, useState, useMemo } from 'react';
import { head, prop, compose, pick, objOf, mergeDeepRight } from 'ramda';
import { stream, scan } from 'flyd';

/**
* A simple debouncing function
*/
const debounce = (fn, delay) => {
let timeout;

return function (...args) {
const functionCall = () => fn.apply(this, args);

timeout && clearTimeout(timeout);
timeout = setTimeout(functionCall, delay);
};
};

const getSizeForLayout = compose(objOf('layout'), pick(['width', 'height']), prop('contentRect'), head);

export default function usePlotly() {
const updates = useMemo(stream, []);
const appendData = useMemo(stream, []);
const plotlyState = useMemo(
() => scan(mergeDeepRight, { data: [], config: {}, layout: {} }, updates),
[]

Choose a reason for hiding this comment

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

Shouldn't updates be added to the dependencies list here since it's used inside the hook?

Copy link
Author

Choose a reason for hiding this comment

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

since updates is memoized with an empty array, it will only be computed once on mount, so adding it to the dependencies array or not for plotlyState will have no effect....

);

const observer = new ResizeObserver(debounce(compose(updates, getSizeForLayout), 100));
const [internalRef, setRef] = useState(null);
useLayoutEffect(() => {
if (internalRef) {
observer.observe(internalRef);
const endS = plotlyState.map(state => {
Plotly.react(internalRef, state);
});

const endAppend = appendData.map(({ data, tracePos }) => Plotly.extendTraces(internalRef, data, tracePos));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since you've implemented extendTraces here, let's finish it by including maxPoints (the optional 4th argument to extendTraces)


return () => {
Plotly.purge(internalRef);
observer.unobserve(internalRef);
endAppend.end(true);
endS.end(true);
};
}
}, [internalRef, plotlyState, updates, appendData]);

Choose a reason for hiding this comment

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

Shouldn't observer be added as a dependency here since it's used inside the hook?


return { ref: setRef, updates, appendData };
}