Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork137
Hook-based API#275
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:master
Are you sure you want to change the base?
Uh oh!
There was an error while loading.Please reload this page.
Hook-based API#275
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -70,7 +70,9 @@ | ||
}, | ||
"peerDependencies": { | ||
"plotly.js": ">1.34.0", | ||
"react": ">0.13.0", | ||
"flyd": ">=0.2.8", | ||
"ramda": ">=0.28.0" | ||
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.
| ||
}, | ||
"browserify-global-shim": { | ||
"react": "React" | ||
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), | ||
[] | ||
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. Shouldn't 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. since | ||
); | ||
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)); | ||
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. Since you've implemented | ||
return () => { | ||
Plotly.purge(internalRef); | ||
observer.unobserve(internalRef); | ||
endAppend.end(true); | ||
endS.end(true); | ||
}; | ||
} | ||
}, [internalRef, plotlyState, updates, appendData]); | ||
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. Shouldn't | ||
return { ref: setRef, updates, appendData }; | ||
} |