-
-
Notifications
You must be signed in to change notification settings - Fork 139
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
Hook-based API #275
Changes from all 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 |
---|---|---|
|
@@ -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" | ||
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.
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. Are 2 news deps really required?
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 question @floriancargoet - 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. While yes 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. Right, deep merge is annoying to reimplement. I have no problem adding this (and |
||
}, | ||
"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), | ||
[] | ||
|
||
); | ||
|
||
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]); | ||
|
||
|
||
return { ref: setRef, updates, appendData }; | ||
} |