/
Props.purs
46 lines (40 loc) · 1.12 KB
/
Props.purs
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
45
46
module Concur.Core.Props where
import Control.Applicative (pure)
import Data.Function ((<<<))
import Data.Functor (class Functor)
import Data.Unit (Unit, unit)
import Effect (Effect)
data Props p a
= PrimProp p
| Handler ((a -> Effect Unit) -> p)
instance functorProps :: Functor (Props p) where
map _ (PrimProp p) = PrimProp p
map f (Handler h) = Handler \k -> h (k <<< f)
-- | Internal. Do not use. Use unsafeMkProp, or unsafeMkPropHandler instead.
mkProp
:: forall p a
. (a -> Effect Unit)
-> Props p a
-> p
mkProp _ (PrimProp a) = a
mkProp h (Handler f) = f h
-- | Use `handleProp` to handle an event manually
handleProp
:: forall p a b
. (a -> Effect Unit)
-> Props p a
-> Props p b
handleProp _ (PrimProp p) = PrimProp p
handleProp f (Handler g) = PrimProp (g f)
-- | Use this to filter the output of an event handler prop.
-- | For example, to only handle the enter key - `filterProp isEnterEvent onKeyDown`
filterProp ::
forall p a.
(a -> Boolean) ->
Props p a ->
Props p a
filterProp _ p@(PrimProp _) = p
filterProp ok (Handler g) = Handler \h ->
(g \a -> if ok a
then h a
else pure unit)