1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
module Gargantext.Utils.Reload where
import Gargantext.Prelude
import Data.Tuple.Nested ((/\))
import Effect (Effect)
import Reactix as R
import Toestand as T
type Reload = Int
type ReloadS = R.State Reload
type ReloadSRef = R.Ref
new :: R.Hooks ReloadS
new = R.useState' 0
bump :: ReloadS -> Effect Unit
bump (_ /\ setReload) = setReload (_ + 1)
bumpBox :: T.Box Reload -> Effect Unit
bumpBox c = T.modify_ (_ + 1) c
value :: ReloadS -> Reload
value (val /\ _) = val
-- a ReloadS ref that can be initialized later
data ReloadWithInitialize = Initialize | Ready ReloadS
type ReloadWithInitializeRef = R.Ref ReloadWithInitialize
newI :: R.Hooks ReloadWithInitializeRef
newI = R.useRef Initialize
newIInitialized :: ReloadS -> R.Hooks ReloadWithInitializeRef
newIInitialized reload = R.useRef $ Ready reload
initializeI :: ReloadWithInitializeRef -> ReloadS -> Effect Unit
initializeI ref reloadS = case R.readRef ref of
Initialize -> R.setRef ref $ Ready reloadS
Ready _ -> pure unit
bumpI :: ReloadWithInitializeRef -> Effect Unit
bumpI ref = case R.readRef ref of
Initialize -> pure unit
Ready reload -> bump reload