-
-
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 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 |
---|---|---|
@@ -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 }; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
flyd
andramda
seem like they'd be better asdependencies
rather thanpeerDependencies
- and then let's change from>=
to^
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are 2 news deps really required?
ramda
seems to be used just to write this code in the functional style.The author himself said in the related issue that it could be replaced with:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question @floriancargoet -
ramda
has a couple of other uses in this PR (and we do use it a good deal elsewhere at Plotly), but to my mind yourgetSizeForLayout
rewrite is more readable than the full functional version.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While yes
getSizeForLayout
can be written easily withoutramda
(happy to do this), the main reason it was included was formergeDeepRight
which is a relatively complex function to implement.We could go for an independent implementation but my argument would be that
ramda
is relatively small and compatible with tree shakingThere was a problem hiding this comment.
Choose a reason for hiding this comment
The 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
flyd
) but I do think they belong independencies
.