UpdateEffect.purs 2.21 KB
Newer Older
1 2 3
module Gargantext.Hooks.UpdateEffect
  ( useUpdateEffect, useUpdateEffect'
  , useUpdateEffect1, useUpdateEffect1'
arturo's avatar
arturo committed
4 5
  , useUpdateEffect2, useUpdateEffect2'
  , useUpdateEffect3, useUpdateEffect3'
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
  ) where

import Gargantext.Prelude

import Effect (Effect)
import Gargantext.Hooks.FirstEffect (useFirstMount)
import Reactix (nothing, thenNothing)
import Reactix as R

-- | Hook triggered on update only (not on first mount)
useUpdateEffect :: Effect (Effect Unit) -> R.Hooks Unit
useUpdateEffect e = useFirstMount >>= eff e

-- | Like `useUpdateEffect` but Effect fn does return a cleanup handler
useUpdateEffect' :: forall a. Effect a -> R.Hooks Unit
useUpdateEffect' e = useFirstMount >>= eff (e # thenNothing)

-- | Like `useUpdateEffect` but with a memo value
useUpdateEffect1 :: forall a. a -> Effect (Effect Unit) -> R.Hooks Unit
useUpdateEffect1 a e = useFirstMount >>= eff1 a e

-- | Like `useUpdateEffect` but the provided Effect does not return a
-- | cleanup handler
useUpdateEffect1' :: forall a b. a -> Effect b -> R.Hooks Unit
useUpdateEffect1' a e = useFirstMount >>= eff1 a (e # thenNothing)

arturo's avatar
arturo committed
32 33 34 35 36 37 38 39 40 41 42 43
useUpdateEffect2 :: forall a b. a -> b -> Effect (Effect Unit) -> R.Hooks Unit
useUpdateEffect2 a b e = useFirstMount >>= eff2 a b e

useUpdateEffect2' :: forall a b c. a -> b -> Effect c -> R.Hooks Unit
useUpdateEffect2' a b e = useFirstMount >>= eff2 a b (e # thenNothing)

useUpdateEffect3 :: forall a b c. a -> b -> c -> Effect (Effect Unit) -> R.Hooks Unit
useUpdateEffect3 a b c e = useFirstMount >>= eff3 a b c e

useUpdateEffect3' :: forall a b c d. a -> b -> c -> Effect d -> R.Hooks Unit
useUpdateEffect3' a b c e = useFirstMount >>= eff3 a b c (e # thenNothing)

44
eff :: Effect (Effect Unit) -> Boolean -> R.Hooks Unit
arturo's avatar
arturo committed
45
eff ef cd = R.useEffect if cd then nothing # thenNothing else ef
46 47

eff1 :: forall a. a -> Effect (Effect Unit) -> Boolean -> R.Hooks Unit
arturo's avatar
arturo committed
48 49 50 51 52 53 54
eff1 a ef cd = R.useEffect1 a if cd then nothing # thenNothing else ef

eff2 :: forall a b. a -> b -> Effect (Effect Unit) -> Boolean -> R.Hooks Unit
eff2 a b ef cd = R.useEffect2 a b if cd then nothing # thenNothing else ef

eff3 :: forall a b c. a -> b -> c -> Effect (Effect Unit) -> Boolean -> R.Hooks Unit
eff3 a b c ef cd = R.useEffect3 a b c if cd then nothing # thenNothing else ef