Skip to content

Commit 1102528

Browse files
committed
Liberate el functions from React
1 parent 0135377 commit 1102528

File tree

5 files changed

+82
-56
lines changed

5 files changed

+82
-56
lines changed

src/Concur/Core.purs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ module Concur.Core
66
)
77
where
88

9+
import Prelude (class Monoid, Unit, bind, map, pure, show, void, ($), (<<<), (<>))
10+
911
import Concur.Core.Discharge (discharge)
1012
import Concur.Core.Types (Widget(..), WidgetStep(..), resume, unWidget)
1113
import Concur.Core.LiftWidget (class LiftWidget, liftWidget)
@@ -20,7 +22,6 @@ import Effect.AVar (empty, tryPut) as EVar
2022
import Effect.Aff.AVar (take) as AVar
2123
import Effect.Aff.Class (liftAff)
2224
import Effect.Console (log)
23-
import Prelude (class Monoid, Unit, bind, map, pure, show, void, ($), (<<<), (<>))
2425

2526
-- Helpers for some very common use of unsafe blocking io
2627

src/Concur/Core/DOM.purs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
module Concur.Core.DOM where
2+
3+
import Concur.Core (mkLeafWidget, wrapViewEvent)
4+
import Concur.Core.LiftWidget (class LiftWidget, liftWidget)
5+
import Concur.Core.Props (Props, mkProp)
6+
import Concur.Core.Types (Widget)
7+
import Control.MultiAlternative (class MultiAlternative, orr)
8+
import Control.ShiftMap (class ShiftMap, shiftMap)
9+
import Data.Function (($), (<<<))
10+
import Data.Functor (map)
11+
12+
-- | Wrap a single widget with a node that can have eventHandlers attached
13+
el
14+
:: forall m a p v
15+
. ShiftMap (Widget (Array v)) m
16+
=> (Array p -> Array v -> Array v)
17+
-> Array (Props p a)
18+
-> m a
19+
-> m a
20+
el e props = shiftMap (\f w -> wrapViewEvent (\h v -> (e (map (mkProp h <<< map f) props) v)) w)
21+
22+
-- | Promote a leaf node to a widget
23+
elLeaf
24+
:: forall p v m a
25+
. LiftWidget (Array v) m
26+
=> (Array p -> Array v)
27+
-> Array (Props p a)
28+
-> m a
29+
elLeaf e props = liftWidget $ mkLeafWidget \h -> e (map (mkProp h) props)
30+
31+
-- | Wrap some widgets with a node that can have eventHandlers attached
32+
el'
33+
:: forall m a p v
34+
. ShiftMap (Widget (Array v)) m
35+
=> MultiAlternative m
36+
=> (Array p -> Array v -> Array v)
37+
-> Array (Props p a)
38+
-> Array (m a)
39+
-> m a
40+
el' e props = el e props <<< orr

src/Concur/React.purs

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,64 +2,16 @@ module Concur.React where
22

33
import Prelude
44

5-
import Concur.Core (mkLeafWidget, wrapViewEvent)
65
import Concur.Core.Discharge (discharge, dischargePartialEffect)
7-
import Concur.Core.LiftWidget (class LiftWidget, liftWidget)
8-
import Concur.Core.Props (mkProp)
96
import Concur.Core.Types (Widget)
10-
import Concur.React.Props (ReactProps)
11-
import Control.MultiAlternative (class MultiAlternative, orr)
12-
import Control.ShiftMap (class ShiftMap, shiftMap)
137
import Data.Either (Either(..))
148
import Data.Tuple (Tuple(..))
159
import Effect.Console (log)
1610
import React as R
17-
import React.DOM.Props as P
1811

1912
type HTML
2013
= Array R.ReactElement
2114

22-
type NodeName
23-
= Array R.ReactElement -> R.ReactElement
24-
25-
type NodeTag
26-
= Array P.Props -> Array R.ReactElement -> R.ReactElement
27-
28-
type LeafTag
29-
= Array P.Props -> R.ReactElement
30-
31-
-- | Wrap a widget with a node that can have eventHandlers attached
32-
el ::
33-
forall m a.
34-
ShiftMap (Widget HTML) m =>
35-
NodeTag ->
36-
Array (ReactProps a) ->
37-
m a ->
38-
m a
39-
el e props = shiftMap (\f w -> wrapViewEvent (\h v ->
40-
[e (map (mkProp h <<< map f) props) v]) w)
41-
42-
-- | Promote a leaf node to a widget
43-
elLeaf ::
44-
forall m a.
45-
LiftWidget HTML m =>
46-
LeafTag ->
47-
Array (ReactProps a) ->
48-
m a
49-
elLeaf e props = liftWidget $ mkLeafWidget \h ->
50-
[e (map (mkProp h) props)]
51-
52-
-- | Wrap some widgets with a node that can have eventHandlers attached
53-
el' ::
54-
forall m a.
55-
ShiftMap (Widget HTML) m =>
56-
MultiAlternative m =>
57-
NodeTag ->
58-
Array (ReactProps a) ->
59-
Array (m a) ->
60-
m a
61-
el' e props = el e props <<< orr
62-
6315
-- React apparently requires wrapping state inside an object
6416
type ComponentState
6517
= {view :: HTML}

src/Concur/React/DOM.purs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,50 @@ module Concur.React.DOM where
22

33
import Prelude hiding (div,map,sub)
44

5+
import Concur.Core.DOM (el, el', elLeaf) as CD
56
import Concur.Core.LiftWidget (class LiftWidget, liftWidget)
7+
import Concur.Core.Props (Props)
68
import Concur.Core.Types (Widget, display)
7-
import Concur.React (HTML, el, el', elLeaf)
9+
import Concur.React (HTML)
810
import Concur.React.Props (ReactProps)
911
import Control.MultiAlternative (class MultiAlternative)
1012
import Control.ShiftMap (class ShiftMap)
1113
import React.DOM as D
1214

15+
-- The signature of el, el', and elLeaf changed recently
16+
viewAdapter
17+
:: forall ps vs res
18+
. (ps -> vs -> res)
19+
-> (ps -> vs -> Array res)
20+
viewAdapter f = \ps vs -> [f ps vs]
21+
22+
el
23+
:: forall m a p v
24+
. ShiftMap (Widget (Array v)) m
25+
=> (Array p -> Array v -> v)
26+
-> Array (Props p a)
27+
-> m a
28+
-> m a
29+
el f = CD.el (viewAdapter f)
30+
31+
el'
32+
:: forall m a p v
33+
. ShiftMap (Widget (Array v)) m
34+
=> MultiAlternative m
35+
=> (Array p -> Array v -> v)
36+
-> Array (Props p a)
37+
-> Array (m a)
38+
-> m a
39+
el' f = CD.el' (viewAdapter f)
40+
41+
elLeaf
42+
:: forall p v m a
43+
. LiftWidget (Array v) m
44+
=> (Array p -> v)
45+
-> Array (Props p a)
46+
-> m a
47+
elLeaf f = CD.elLeaf (pure <<< f)
48+
1349
-- Wrappers for all DOM elements from purescript-react
1450
-- TODO: Generate these mechanically somehow
1551
type El1

src/Concur/React/SVG.purs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
module Concur.React.SVG where
22

3-
import Prelude hiding (div, map, sub)
4-
5-
import Concur.React (el')
6-
import Concur.React.DOM (El, El')
3+
import Prelude hiding (div,map,sub)
74

5+
import Concur.React.DOM (El, El', el')
86
import React.DOM.SVG as S
97

108
-------------------------------------------------------------------------------------------------------------------
11-
circle ::
12-
El
9+
circle :: El
1310
circle = el' S.circle
1411

1512
circle' :: El'

0 commit comments

Comments
 (0)