Commit d3f0d66e authored by Przemyslaw Kaminski's avatar Przemyslaw Kaminski

[Loader] useCache with Eq comparison (not working yet)

parent de71a1b7
...@@ -2,8 +2,9 @@ module Gargantext.Hooks.Loader where ...@@ -2,8 +2,9 @@ module Gargantext.Hooks.Loader where
import Gargantext.Prelude import Gargantext.Prelude
import Data.Maybe (Maybe(..), isNothing, maybe, maybe') import Data.Maybe (Maybe(..), isNothing, maybe, maybe')
import Data.Tuple (fst) import Data.Tuple (fst, Tuple(..))
import Data.Tuple.Nested ((/\)) import Data.Tuple.Nested ((/\))
import DOM.Simple.Console (log2)
import Effect.Aff (Aff) import Effect.Aff (Aff)
import Effect.Class (liftEffect) import Effect.Class (liftEffect)
import Reactix as R import Reactix as R
...@@ -14,18 +15,20 @@ useAff :: forall st. ...@@ -14,18 +15,20 @@ useAff :: forall st.
Aff st -> R.Hooks (Maybe st) Aff st -> R.Hooks (Maybe st)
useAff loader = do useAff loader = do
(loaded /\ setLoaded) <- R.useState' Nothing (loaded /\ setLoaded) <- R.useState' Nothing
R.useEffect1 loader $ do R.useEffect' $ do
if isNothing loaded then if isNothing loaded then
R2.affEffect "G.H.Loader.useAff" $ R2.affEffect "G.H.Loader.useAff" $
loader >>= (liftEffect <<< setLoaded <<< const <<< Just) loader >>= (liftEffect <<< setLoaded <<< const <<< Just)
else pure R.nothing else pure R.nothing
pure loaded pure loaded
useLoader :: forall path st. useLoader :: forall path st. Eq path =>
path -> (path -> Aff st) -> (st -> R.Element) -> R.Hooks R.Element path -> (path -> Aff st) -> (st -> R.Element) -> R.Hooks R.Element
useLoader path loader render useLoader path loader render
= maybe' (\_ -> loadingSpinner {}) render = maybe' (\_ -> loadingSpinner {}) render
<$> (useAff =<< R.useMemo2 path loader (\_ -> loader path)) -- <$> (useAff =<< R.useMemo2 path loader (\_ -> loader path))
-- <$> (useAff =<< R2.useCache path (\p -> pure $ loader p))
<$> (useAff =<< (loader path))
useLoader2 :: forall path st. useLoader2 :: forall path st.
R.State path -> (path -> Aff st) R.State path -> (path -> Aff st)
......
module Gargantext.Utils.Reactix where module Gargantext.Utils.Reactix where
import Prelude import Prelude
import Data.Maybe (Maybe(..))
import Data.Nullable (Nullable, null, toMaybe)
import Data.Tuple (Tuple)
import Data.Tuple.Nested ((/\))
import DOM.Simple as DOM import DOM.Simple as DOM
import DOM.Simple.Console (log, log2) import DOM.Simple.Console (log, log2)
import DOM.Simple.Document (document) import DOM.Simple.Document (document)
import DOM.Simple.Event as DE
import DOM.Simple.Element as Element import DOM.Simple.Element as Element
import DOM.Simple.Event as DE
import DOM.Simple.Types (class IsNode) import DOM.Simple.Types (class IsNode)
import Data.Maybe (Maybe(..))
import Data.Nullable (Nullable, null, toMaybe)
import Data.Tuple (Tuple)
import Data.Tuple.Nested ((/\))
import Effect (Effect) import Effect (Effect)
import Effect.Class (liftEffect)
import Effect.Aff (Aff, launchAff, launchAff_, killFiber) import Effect.Aff (Aff, launchAff, launchAff_, killFiber)
import Effect.Class (liftEffect)
import Effect.Exception (error) import Effect.Exception (error)
import Effect.Uncurried (EffectFn1, runEffectFn1, mkEffectFn1, mkEffectFn2) import Effect.Uncurried (EffectFn1, runEffectFn1, mkEffectFn1, mkEffectFn2)
import FFI.Simple ((...), defineProperty, delay, args2, args3) import FFI.Simple ((...), defineProperty, delay, args2, args3)
...@@ -134,7 +135,7 @@ getElementById :: String -> Effect (Maybe DOM.Element) ...@@ -134,7 +135,7 @@ getElementById :: String -> Effect (Maybe DOM.Element)
getElementById = (flip delay) h getElementById = (flip delay) h
where where
h id = pure $ toMaybe $ document ... "getElementById" $ [id] h id = pure $ toMaybe $ document ... "getElementById" $ [id]
-- We just assume it works, so make sure it's in the html -- We just assume it works, so make sure it's in the html
getPortalHost :: R.Hooks DOM.Element getPortalHost :: R.Hooks DOM.Element
getPortalHost = R.unsafeHooksEffect $ delay unit $ \_ -> pure $ document ... "getElementById" $ ["portal"] getPortalHost = R.unsafeHooksEffect $ delay unit $ \_ -> pure $ document ... "getElementById" $ ["portal"]
...@@ -187,3 +188,20 @@ appendChildToParentId ps c = delay unit $ \_ -> do ...@@ -187,3 +188,20 @@ appendChildToParentId ps c = delay unit $ \_ -> do
case parentEl of case parentEl of
Nothing -> pure unit Nothing -> pure unit
Just el -> appendChild el c Just el -> appendChild el c
useCache :: forall i o. Eq i => i -> (i -> R.Hooks o) -> R.Hooks o
useCache i f = do
iRef <- R.useRef Nothing
oRef <- R.useRef Nothing
let currI = R.readRef iRef
let currO = R.readRef oRef
if currI == Just i then
case currO of
Nothing -> f i -- this one shouldn't happen, but purescript
Just v -> pure v
else do
new <- f i
R.unsafeHooksEffect (R.setRef oRef $ Just new)
R.unsafeHooksEffect (R.setRef iRef $ Just i)
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