Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
purescript-gargantext
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Grégoire Locqueville
purescript-gargantext
Commits
d3f0d66e
Commit
d3f0d66e
authored
Nov 25, 2019
by
Przemyslaw Kaminski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[Loader] useCache with Eq comparison (not working yet)
parent
de71a1b7
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
11 deletions
+32
-11
Loader.purs
src/Gargantext/Hooks/Loader.purs
+7
-4
Reactix.purs
src/Gargantext/Utils/Reactix.purs
+25
-7
No files found.
src/Gargantext/Hooks/Loader.purs
View file @
d3f0d66e
...
@@ -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.useEffect
1 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)
...
...
src/Gargantext/Utils/Reactix.purs
View file @
d3f0d66e
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)
...
@@ -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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment