Skip to content

Commit

Permalink
refactor: simplify the communication for offline caching
Browse files Browse the repository at this point in the history
It should be enough to use isParentCached for knowing when to start
recording and remove the cache in the plugin.
  • Loading branch information
edoardo committed May 27, 2024
1 parent 6d49e55 commit ea99422
Showing 1 changed file with 11 additions and 17 deletions.
28 changes: 11 additions & 17 deletions src/PluginWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,9 @@ const LoadingMask = () => {
)
}

const CacheableSectionWrapper = ({
id,
children,
cacheNow,
isParentCached,
}) => {
const CacheableSectionWrapper = ({ id, children, isParentCached }) => {
const { startRecording, isCached, remove } = useCacheableSection(id)

/*
useEffect(() => {
if (cacheNow) {
startRecording({ onError: console.error })
Expand All @@ -34,11 +29,15 @@ const CacheableSectionWrapper = ({
// an infinite recording loop as-is (probably need to memoize it)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [cacheNow])

*/
useEffect(() => {
// Synchronize cache state on load or prop update
// -- a back-up to imperative `removeCachedData`
if (!isParentCached && isCached) {
if (isParentCached && !isCached) {
console.log('LL p: start recording')
startRecording({ onError: console.error })
} else if (!isParentCached && isCached) {
// Synchronize cache state on load or prop update
// -- a back-up to imperative `removeCachedData`
console.log('LL p: remove cache')
remove()
}

Expand All @@ -52,14 +51,13 @@ const CacheableSectionWrapper = ({
)
}
CacheableSectionWrapper.propTypes = {
cacheNow: PropTypes.bool,
children: PropTypes.node,
id: PropTypes.string,
isParentCached: PropTypes.bool,
}

const PluginWrapper = (props) => {
const { onInstallationStatusChange, onPropsReceived, ...otherProps } = props
const { onInstallationStatusChange, ...otherProps } = props
const [propsFromParent, setPropsFromParent] = useState(otherProps)

const onDataSorted = useCallback(
Expand Down Expand Up @@ -94,8 +92,6 @@ const PluginWrapper = (props) => {
}, [onInstallationStatusChange])

if (propsFromParent) {
onPropsReceived()

return (
<div
style={{
Expand All @@ -106,7 +102,6 @@ const PluginWrapper = (props) => {
>
<CacheableSectionWrapper
id={propsFromParent.cacheId}
cacheNow={propsFromParent.recordOnNextLoad}
isParentCached={propsFromParent.isParentCached}
>
<Visualization
Expand All @@ -124,7 +119,6 @@ const PluginWrapper = (props) => {

PluginWrapper.propTypes = {
onInstallationStatusChange: PropTypes.func,
onPropsReceived: PropTypes.func,
}

export default PluginWrapper

0 comments on commit ea99422

Please sign in to comment.