-
-
Notifications
You must be signed in to change notification settings - Fork 137
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 our terms of service and privacy 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?
Hook-based API #275
Changes from 3 commits
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,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 }; | ||
} |
Uh oh!
There was an error while loading. Please reload this page.