Skip to content

Commit

Permalink
v0.4.1 - use{Layout,}EffectFn{1-5}{',}
Browse files Browse the repository at this point in the history
  • Loading branch information
jjl committed Jul 7, 2019
1 parent 8787abe commit b27dead
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 17 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ Not in any particular order
<!-- * `R.React.provide` - provider a value through a `Provider` -->
<!-- * `R.React.consume` - consume a value through a `Consumer` -->

### 0.4.1

New:

* `useEffectFn{1-5}` (+ layout and prime variants) - variants taking
functions from memo values to effect, to better aid code reuse

### 0.4.0

Breaking:
Expand Down
118 changes: 101 additions & 17 deletions src/Reactix/Hooks.purs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ module Reactix.Hooks
, useRef
, useDebugValue, useDebugValue'
, nothing, thenNothing
, useEffect, useEffect', useEffect1, useEffect1'
, useEffect2, useEffect2', useEffect3, useEffect3'
, useEffect4, useEffect4', useEffect5, useEffect5'
, useEffect, useEffect'
, useEffect1, useEffect1', useEffectFn1, useEffectFn1'
, useEffect2, useEffect2', useEffectFn2, useEffectFn2'
, useEffect3, useEffect3', useEffectFn3, useEffectFn3'
, useEffect4, useEffect4', useEffectFn4, useEffectFn4'
, useEffect5, useEffect5', useEffectFn5, useEffectFn5'
, useLayoutEffect, useLayoutEffect'
, useLayoutEffect1, useLayoutEffect1'
, useLayoutEffect2, useLayoutEffect2'
, useLayoutEffect3, useLayoutEffect3'
, useLayoutEffect4, useLayoutEffect4'
, useLayoutEffect5, useLayoutEffect5'
, useLayoutEffect1, useLayoutEffect1', useLayoutEffectFn1, useLayoutEffectFn1'
, useLayoutEffect2, useLayoutEffect2', useLayoutEffectFn2, useLayoutEffectFn2'
, useLayoutEffect3, useLayoutEffect3', useLayoutEffectFn3, useLayoutEffectFn3'
, useLayoutEffect4, useLayoutEffect4', useLayoutEffectFn4, useLayoutEffectFn4'
, useLayoutEffect5, useLayoutEffect5', useLayoutEffectFn5, useLayoutEffectFn5'
, useMemo, useMemo1, useMemo2, useMemo3, useMemo4, useMemo5
, useCallback, useCallback1, useCallback2
, useCallback3, useCallback4, useCallback5
Expand All @@ -22,7 +25,7 @@ module Reactix.Hooks
)
where

import Prelude
import Prelude hiding (div)
import Data.Function.Uncurried (Fn2, mkFn2, runFn2)
import Data.Tuple (Tuple(..))
import Effect (Effect)
Expand All @@ -33,13 +36,11 @@ import Effect.Uncurried
import FFI.Simple ((...), (..), delay, args2, args3, args4, args5, setProperty)
import FFI.Simple.PseudoArray as Array
import DOM.Simple.Console
import Reactix.Utils (tuple, currySecond, hook)
import Reactix.Utils (tuple, currySecond, hook, splay1, splay2, splay3, splay4, splay5)
import Reactix.React (Context, Ref, Hooks, react, unsafeHooksEffect)

--- useState



-- | A state hook is a tuple of value and setter
type State state = Tuple state ((state -> state) -> Effect Unit)

Expand All @@ -50,7 +51,7 @@ useState s =
pure $ currySecond $ tuple $ react ... "useState" $ [ delay unit (pure <<< s) ]

useState' :: forall s. s -> Hooks (State s)
useState' s = useState $ const s
useState' = useState <<< const
-- useReducer

-- | A reducer hook is a tuple of value and reducer-setter
Expand Down Expand Up @@ -154,6 +155,47 @@ useEffect4' a b c d = useEffect4 a b c d <<< thenNothing
useEffect5' :: forall a b c d e f. a -> b -> c -> d -> e -> Effect f -> Hooks Unit
useEffect5' a b c d e = useEffect5 a b c d e <<< thenNothing

-- | Like useEffect1, but takes a function from memo value to effect
useEffectFn1 :: forall a. a -> (a -> Effect (Effect Unit)) -> Hooks Unit
useEffectFn1 a f = splay1 useEffect1 f a

-- | like useEffectFn1, but with two memo values
useEffectFn2 :: forall a b. a -> b -> (a -> b -> Effect (Effect Unit)) -> Hooks Unit
useEffectFn2 a b f = splay2 useEffect2 f a b

-- | like useEffectFn1, but with three memo values
useEffectFn3 :: forall a b c. a -> b -> c -> (a -> b -> c -> Effect (Effect Unit)) -> Hooks Unit
useEffectFn3 a b c f = splay3 useEffect3 f a b c

-- | like useEffectFn1, but with four memo values
useEffectFn4 :: forall a b c d. a -> b -> c -> d -> (a -> b -> c -> d -> Effect (Effect Unit)) -> Hooks Unit
useEffectFn4 a b c d f = splay4 useEffect4 f a b c d

-- | like useEffectFn1, but with five memo values
useEffectFn5 :: forall a b c d e. a -> b -> c -> d -> e -> (a -> b -> c -> d -> e -> Effect (Effect Unit)) -> Hooks Unit
useEffectFn5 a b c d e f = splay5 useEffect5 f a b c d e

-- | Like useEffect1', but takes a function from memo value to effect
useEffectFn1' :: forall a. a -> (a -> Effect Unit) -> Hooks Unit
useEffectFn1' a f = splay1 useEffect1' f a

-- | like useEffectFn1', but with two memo values
useEffectFn2' :: forall a b. a -> b -> (a -> b -> Effect Unit) -> Hooks Unit
useEffectFn2' a b f = splay2 useEffect2' f a b

-- | like useEffectFn1', but with three memo values
useEffectFn3' :: forall a b c. a -> b -> c -> (a -> b -> c -> Effect Unit) -> Hooks Unit
useEffectFn3' a b c f = splay3 useEffect3' f a b c

-- | like useEffectFn1', but with four memo values
useEffectFn4' :: forall a b c d. a -> b -> c -> d -> (a -> b -> c -> d -> Effect Unit) -> Hooks Unit
useEffectFn4' a b c d f = splay4 useEffect4' f a b c d

-- | like useEffectFn1', but with five memo values
useEffectFn5' :: forall a b c d e. a -> b -> c -> d -> e -> (a -> b -> c -> d -> e -> Effect Unit) -> Hooks Unit
useEffectFn5' a b c d e f = splay5 useEffect5' f a b c d e


-- useLayoutEffect

-- | Given an Effect function which returns a cleanup Effect function,
Expand Down Expand Up @@ -190,19 +232,19 @@ useLayoutEffect' = useLayoutEffect <<< thenNothing
useLayoutEffect1' :: forall a b. a -> Effect b -> Hooks Unit
useLayoutEffect1' a = useLayoutEffect1 a <<< thenNothing

-- | Like useLayoutEffect2, but the provided Effect fn does not return a cleanup handler
-- | Like useLayoutEffect1' but with 2 memo values
useLayoutEffect2' :: forall a b c. a -> b -> Effect c -> Hooks Unit
useLayoutEffect2' a b = useLayoutEffect2 a b <<< thenNothing

-- | Like useLayoutEffect3, but the provided Effect fn does not return a cleanup handler
-- | Like useLayoutEffect1' but with 3 memo values
useLayoutEffect3' :: forall a b c d. a -> b -> c -> Effect d -> Hooks Unit
useLayoutEffect3' a b c = useLayoutEffect3 a b c <<< thenNothing

-- | Like useLayoutEffect4, but the provided Effect fn does not return a cleanup handler
-- | Like useLayoutEffect1' but with 4 memo values
useLayoutEffect4' :: forall a b c d e. a -> b -> c -> d -> Effect e -> Hooks Unit
useLayoutEffect4' a b c d = useLayoutEffect4 a b c d <<< thenNothing

-- | Like useLayoutEffect5, but the provided Effect fn does not return a cleanup handler
-- | Like useLayoutEffect1' but with 5 memo values
useLayoutEffect5' :: forall a b c d e f. a -> b -> c -> d -> e -> Effect f -> Hooks Unit
useLayoutEffect5' a b c d f = useLayoutEffect5 a b c d f <<< thenNothing

Expand All @@ -212,6 +254,48 @@ _useLayoutEffect e a =
pure $ react ... "useLayoutEffect" $
args2 e (Array.from a)

-- | Like useLayoutEffect1, but takes a function from memo value to effect
useLayoutEffectFn1 :: forall a. a -> (a -> Effect (Effect Unit)) -> Hooks Unit
useLayoutEffectFn1 a f = splay1 useLayoutEffect1 f a

-- | like useLayoutEffectFn1, but with two memo values
useLayoutEffectFn2 :: forall a b. a -> b -> (a -> b -> Effect (Effect Unit)) -> Hooks Unit
useLayoutEffectFn2 a b f = splay2 useLayoutEffect2 f a b

-- | like useLayoutEffectFn1, but with three memo values
useLayoutEffectFn3 :: forall a b c. a -> b -> c -> (a -> b -> c -> Effect (Effect Unit)) -> Hooks Unit
useLayoutEffectFn3 a b c f = splay3 useLayoutEffect3 f a b c

-- | like useLayoutEffectFn1, but with four memo values
useLayoutEffectFn4 :: forall a b c d. a -> b -> c -> d -> (a -> b -> c -> d -> Effect (Effect Unit)) -> Hooks Unit
useLayoutEffectFn4 a b c d f = splay4 useLayoutEffect4 f a b c d

-- | like useLayoutEffectFn1, but with five memo values
useLayoutEffectFn5 :: forall a b c d e. a -> b -> c -> d -> e -> (a -> b -> c -> d -> e -> Effect (Effect Unit)) -> Hooks Unit
useLayoutEffectFn5 a b c d e f = splay5 useLayoutEffect5 f a b c d e

-- | Like useLayoutEffect1, but takes a function from memo value to effect
useLayoutEffectFn1' :: forall a. a -> (a -> Effect Unit) -> Hooks Unit
useLayoutEffectFn1' a f = splay1 useLayoutEffect1' f a

-- | like useLayoutEffectFn1', but with two memo values
useLayoutEffectFn2' :: forall a b. a -> b -> (a -> b -> Effect Unit) -> Hooks Unit
useLayoutEffectFn2' a b f = splay2 useLayoutEffect2' f a b

-- | like useLayoutEffectFn1', but with three memo values
useLayoutEffectFn3' :: forall a b c. a -> b -> c -> (a -> b -> c -> Effect Unit) -> Hooks Unit
useLayoutEffectFn3' a b c f = splay3 useLayoutEffect3' f a b c

-- | like useLayoutEffectFn1', but with four memo values
useLayoutEffectFn4' :: forall a b c d. a -> b -> c -> d -> (a -> b -> c -> d -> Effect Unit) -> Hooks Unit
useLayoutEffectFn4' a b c d f = splay4 useLayoutEffect4' f a b c d

-- | like useLayoutEffectFn1', but with five memo values
useLayoutEffectFn5' :: forall a b c d e. a -> b -> c -> d -> e -> (a -> b -> c -> d -> e -> Effect Unit) -> Hooks Unit
useLayoutEffectFn5' a b c d e f = splay5 useLayoutEffect5' f a b c d e



-- useMemo

-- | Given a function to compute an expensive value, returns the value
Expand Down
21 changes: 21 additions & 0 deletions src/Reactix/Utils.purs
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,24 @@ hook f = unsafeHooksEffect (delay unit f)
ucFirst :: String -> String
ucFirst = help <<< splitAt 1
where help {before, after} = toUpper before <> after

-- I'm not quite sure of the type of this. Purescript "helpfully" says it's:
-- forall t202 t203 t205 t206. (t203 -> t205 -> t206) -> (t202 -> t203) -> ... -> ...
onemore s f g a = s (f a) (g a)

-- Aka the `s` combinator from the SKI calculus
splay1 :: forall a b c. (a -> b -> c) -> (a -> b) -> a -> c
splay1 c f v = c v (f v)

splay2 :: forall a b c d. (a -> b -> c -> d) -> (a -> b -> c) -> a -> b -> d
splay2 = onemore splay1

splay3 :: forall a b c d e. (a -> b -> c -> d -> e) -> (a -> b -> c -> d) -> a -> b -> c -> e
splay3 = onemore splay2

splay4 :: forall a b c d e f. (a -> b -> c -> d -> e -> f) -> (a -> b -> c -> d -> e) -> a -> b -> c -> d -> f
splay4 = onemore splay3

splay5 :: forall a b c d e f g. (a -> b -> c -> d -> e -> f -> g) -> (a -> b -> c -> d -> e -> f) -> a -> b -> c -> d -> e -> g
splay5 = onemore splay4

0 comments on commit b27dead

Please sign in to comment.