Commit f77ad1c8 authored by Przemyslaw Kaminski's avatar Przemyslaw Kaminski

[Graph] useLoader cache cleanup

useAff cached to aggressively. Added refs for path comparison (this also
adds a requirement of Eq path to useLoader now).

Also, removed lots of duplicated code in Loader.purs and unified to have
only one loader.
parent d3f0d66e
......@@ -169,9 +169,6 @@ docViewCpt :: R.Component Props
docViewCpt = R.hooksComponent "G.C.FacetsTable.DocView" cpt
where
cpt {frontends, session, nodeId, listId, query, totalRecords, chart, container} _ = do
R.useEffect' $ do
log2 "[docViewCpt] query" query
deletions <- R.useState' initialDeletions
path <- R.useState' $ initialPagePath {nodeId, listId, query, session}
......@@ -249,7 +246,6 @@ initialPagePath {session, nodeId, listId, query} = {session, nodeId, listId, que
loadPage :: PagePath -> Aff (Array DocumentsView)
loadPage {session, nodeId, listId, query, params: {limit, offset, orderBy}} = do
liftEffect $ log2 "[loadPage] query" query
let p = Search { listId, offset, limit, orderBy: convOrderBy <$> orderBy } (Just nodeId)
SearchResults res <- post session p $ SearchQuery {query}
pure $ res2corpus <$> res.results
......@@ -285,9 +281,6 @@ pageLayoutCpt :: R.Component PageLayoutProps
pageLayoutCpt = R.hooksComponent "G.C.FacetsTable.PageLayout" cpt
where
cpt {frontends, totalRecords, deletions, container, session, path} _ = do
R.useEffect' $ do
log2 "[pageLayoutCpt] path" $ fst path
useLoader (fst path) loadPage $ \documents ->
page {frontends, totalRecords, deletions, container, session, path, documents}
......@@ -298,9 +291,6 @@ pageCpt :: R.Component PageProps
pageCpt = R.hooksComponent "G.C.FacetsTable.Page" cpt
where
cpt {frontends, totalRecords, container, deletions, documents, session, path: path@({nodeId, listId, query} /\ setPath)} _ = do
R.useEffect' $ do
log2 "[pageCpt] query" query
pure $ T.table { rows, container, colNames, totalRecords, params }
where
setParams f = setPath $ \p@{params: ps} -> p {params = f ps}
......
......@@ -78,8 +78,6 @@ explorerCpt = R.hooksComponent "G.C.GraphExplorer.explorer" cpt
selectedNodeIds <- R.useState' $ Set.empty
R.useEffect' $ do
log2 "[explorerCpt] selectedNodeIds" selectedNodeIds
if fst controls.showSidePanel == GET.InitialClosed && (not Set.isEmpty $ fst selectedNodeIds) then
snd controls.showSidePanel $ \_ -> GET.Opened
else
......
module Gargantext.Hooks.Loader where
import Gargantext.Prelude
import Data.Maybe (Maybe(..), isNothing, maybe, maybe')
import Data.Maybe (Maybe(..), isJust, isNothing, maybe, maybe')
import Data.Tuple (fst, Tuple(..))
import Data.Tuple.Nested ((/\))
import DOM.Simple.Console (log2)
import DOM.Simple.Console (log, log2)
import Effect.Aff (Aff)
import Effect.Class (liftEffect)
import FFI.Simple (delay)
import Reactix as R
import Gargantext.Utils.Reactix as R2
import Gargantext.Components.LoadingSpinner (loadingSpinner)
useAff :: forall st.
Aff st -> R.Hooks (Maybe st)
useAff loader = do
(loaded /\ setLoaded) <- R.useState' Nothing
R.useEffect' $ do
if isNothing loaded then
R2.affEffect "G.H.Loader.useAff" $
loader >>= (liftEffect <<< setLoaded <<< const <<< Just)
else pure R.nothing
pure loaded
useLoader :: forall path st. Eq path =>
path -> (path -> Aff st) -> (st -> R.Element) -> R.Hooks R.Element
useLoader path loader render
= maybe' (\_ -> loadingSpinner {}) render
-- <$> (useAff =<< R.useMemo2 path loader (\_ -> loader path))
-- <$> (useAff =<< R2.useCache path (\p -> pure $ loader p))
<$> (useAff =<< (loader path))
useLoader2 :: forall path st.
R.State path -> (path -> Aff st)
-> (st -> R.Element) -> R.Hooks R.Element
useLoader2 path loader render = do
path -> (path -> Aff st)
-> (st -> R.Element) -> R.Hooks R.Element
useLoader path loader render = do
state <- R.useState' Nothing
useLoaderEffect2 path state loader
useLoaderEffect path state loader
pure $ maybe (loadingSpinner {}) render (fst state)
useLoaderEffect :: forall state.
Aff state -> R.State (Maybe state) -> R.Hooks Unit
useLoaderEffect loader (state /\ setState) = do
R.useEffect2 state loader $ do
if isNothing state then
R2.affEffect "G.H.Loader.useLoader" $ loader >>= (liftEffect <<< setState <<< const <<< Just)
else pure R.nothing
useLoaderEffect' :: forall state.
Aff state -> R.Hooks (R.State (Maybe state))
useLoaderEffect' aff = do
state <- R.useState' Nothing
useLoaderEffect aff state
pure state
useLoaderEffect2 :: forall st path.
R.State path -> R.State (Maybe st)
-> (path -> Aff st) -> R.Hooks Unit
useLoaderEffect2 path state loader = do
aff <- useRepointer path loader
useLoaderEffect aff state
useRepointer :: forall path st.
R.State path -> (path -> Aff st) -> R.Hooks (Aff st)
useRepointer path@(path' /\ _) loader = R.useMemo2 loader path' (\_ -> loader path')
useLoaderEffect :: forall st path. Eq path =>
path -> R.State (Maybe st)
-> (path -> Aff st) -> R.Hooks Unit
useLoaderEffect path state@(state' /\ setState) loader = do
oPath <- R.useRef path
R.useEffect' $ do
if (R.readRef oPath == path) && (isJust state') then
pure $ pure unit
else do
R.setRef oPath path
R2.affEffect "G.H.Loader.useLoaderEffect2" $ do
l <- loader path
liftEffect $ setState $ const $ Just l
......@@ -202,6 +202,6 @@ useCache i f = do
Just v -> pure v
else do
new <- f i
R.unsafeHooksEffect (R.setRef oRef $ Just new)
R.unsafeHooksEffect (R.setRef iRef $ Just i)
R.unsafeHooksEffect (R.setRef oRef $ Just new)
pure new
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment