Skip to content

Commit 53537b9

Browse files
committed
Add some more interfaces to concur-core
1 parent 0a95fc2 commit 53537b9

File tree

5 files changed

+75
-27
lines changed

5 files changed

+75
-27
lines changed

spago.dhall

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ You can edit this file as you like.
1515
, "maybe"
1616
, "newtype"
1717
, "prelude"
18+
, "profunctor-lenses"
1819
, "refs"
1920
, "tailrec"
2021
, "transformers"

src/Concur/Core.purs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,16 @@ import Data.Monoid (mempty)
1010
import Effect (Effect)
1111
import Prelude (Unit, discard, pure, ($))
1212

13-
-- Helpers for some very common use of unsafe blocking io
14-
1513
-- | Construct a widget, by wrapping an existing widget in a view event
1614
mkNodeWidget
1715
:: forall a v
1816
. ((a -> Effect Unit) -> v -> v)
1917
-> Widget v a
2018
-> Widget v a
21-
mkNodeWidget mkView w = mkWidget \cb -> do
22-
runWidget w (f cb)
23-
where
24-
f cb = \x -> case x of
25-
View vc -> cb (View $ vp vc cb)
19+
mkNodeWidget mkView w = mkWidget \cb ->
20+
runWidget w $ case _ of
21+
View v -> cb (View $ mkView (\a -> cb (Completed a)) v)
2622
Completed a -> cb (Completed a)
27-
vp vc cb = mkView (\a -> cb (Completed a)) vc
2823

2924
-- | Construct a widget with just props
3025
mkLeafWidget
@@ -36,3 +31,4 @@ mkLeafWidget mkView = mkWidget \cb -> do
3631
pure mempty
3732
where
3833
v cb = mkView (\a -> cb (Completed a))
34+

src/Concur/Core/DOM.purs

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,76 @@ module Concur.Core.DOM where
22

33
import Concur.Core (mkLeafWidget, mkNodeWidget)
44
import Concur.Core.LiftWidget (class LiftWidget, liftWidget)
5-
import Concur.Core.Props (Props, mkProp)
65
import Concur.Core.Types (Widget)
76
import Control.MultiAlternative (class MultiAlternative, orr)
87
import Control.ShiftMap (class ShiftMap, shiftMap)
98
import Data.Function (($), (<<<))
109
import Data.Functor (class Functor, map)
10+
import Data.Unit (Unit)
11+
import Effect (Effect)
12+
13+
-- A view adapter for when the view is an array
14+
viewAdapter
15+
:: forall ps vs res
16+
. (ps -> vs -> res)
17+
-> (ps -> vs -> Array res)
18+
viewAdapter f = \ps vs -> [ f ps vs ]
1119

1220
-- | Wrap a single widget with a node that can have eventHandlers attached
1321
el
1422
:: forall f p v m a
1523
. ShiftMap (Widget v) m
1624
=> Functor f
17-
=> (f p -> v -> v)
18-
-> f (Props p a)
25+
=> Functor p
26+
=> (f (p (Effect Unit)) -> v -> v)
27+
-> f (p a)
28+
-> m a
29+
-> m a
30+
el e props = shiftMap (\f w -> mkNodeWidget (\h v -> (e (map (map (h <<< f)) props) v)) w)
31+
32+
-- | el, but for array views
33+
elArr
34+
:: forall f p v m a
35+
. ShiftMap (Widget (Array v)) m
36+
=> Functor f
37+
=> Functor p
38+
=> (f (p (Effect Unit)) -> Array v -> v)
39+
-> f (p a)
1940
-> m a
2041
-> m a
21-
el e props = shiftMap (\f w -> mkNodeWidget (\h v -> (e (map (mkProp h <<< map f) props) v)) w)
42+
elArr e props = shiftMap (\f w -> mkNodeWidget (\h v -> (viewAdapter e (map (map (h <<< f)) props) v)) w)
2243

2344
-- | Promote a leaf node to a widget
2445
elLeaf
2546
:: forall f p v m a
2647
. LiftWidget v m
2748
=> Functor f
28-
=> (f p -> v)
29-
-> f (Props p a)
49+
=> Functor p
50+
=> (f (p (Effect Unit)) -> v)
51+
-> f (p a)
3052
-> m a
31-
elLeaf e props = liftWidget $ mkLeafWidget \h -> e (map (mkProp h) props)
53+
elLeaf e props = liftWidget $ mkLeafWidget \h -> e (map (map h) props)
54+
55+
-- | elLeaf but for array views
56+
elLeafArr
57+
:: forall f p v m a
58+
. LiftWidget (Array v) m
59+
=> Functor f
60+
=> Functor p
61+
=> (f (p (Effect Unit)) -> v)
62+
-> f (p a)
63+
-> m a
64+
elLeafArr e props = liftWidget $ mkLeafWidget \h -> [ e (map (map h) props) ]
3265

3366
-- | Wrap some widgets with a node that can have eventHandlers attached
3467
el'
3568
:: forall f p v m a
36-
. ShiftMap (Widget v) m
69+
. ShiftMap (Widget (Array v)) m
3770
=> MultiAlternative m
3871
=> Functor f
39-
=> (f p -> v -> v)
40-
-> f (Props p a)
72+
=> Functor p
73+
=> (f (p (Effect Unit)) -> Array v -> v)
74+
-> f (p a)
4175
-> Array (m a)
4276
-> m a
43-
el' e props = el e props <<< orr
77+
el' e props = el (viewAdapter e) props <<< orr

src/Concur/Core/Types.purs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import Data.Array as A
1313
import Data.Either (Either(..))
1414
import Data.Foldable (fold)
1515
import Data.Function (identity, ($), (<<<))
16-
import Data.Functor (class Functor, void, ($>))
16+
import Data.Functor (class Functor, void, ($>), map)
1717
import Data.Maybe (Maybe(..))
1818
import Data.Monoid (class Monoid, mempty)
1919
import Data.Newtype (class Newtype)
@@ -26,15 +26,28 @@ import Effect.Ref as Ref
2626

2727
-- | Callback -> Effect Canceler (returns the unused effect)
2828
newtype Callback a = Callback (Callback' a)
29+
30+
derive instance Newtype (Callback a) _
31+
2932
type Callback' a = (a -> Effect Unit) -> Effect Canceler
3033
type Canceler = Effect Unit
3134

3235
data Result v a = View v | Completed a
3336

37+
result :: forall v a r. (v -> r) -> (a -> r) -> Result v a -> r
38+
result f g = case _ of
39+
View v -> f v
40+
Completed a -> g a
41+
3442
instance functorResult :: Functor (Result v) where
3543
map _ (View v) = View v
3644
map f (Completed a) = Completed (f a)
3745

46+
mapViewResult :: forall u v a. (u -> v) -> Result u a -> Result v a
47+
mapViewResult f = case _ of
48+
View u -> View (f u)
49+
Completed a -> Completed a
50+
3851
mkCallback :: forall a. Callback' a -> Callback a
3952
mkCallback = Callback
4053

@@ -57,9 +70,9 @@ instance widgetShiftMap :: ShiftMap (Widget v) (Widget v) where
5770
-- A Widget is basically a callback that returns a view or a return value
5871
newtype Widget v a = Widget (Callback (Result v a))
5972

60-
derive instance functorWidget :: Functor (Widget v)
73+
derive instance Functor (Widget v)
6174

62-
instance newtypeWidget :: Newtype (Widget v a) (Callback (Result v a))
75+
derive instance Newtype (Widget v a) _
6376

6477
unWid :: forall v a. Widget v a -> Callback (Result v a)
6578
unWid (Widget w) = w
@@ -70,6 +83,9 @@ runWidget (Widget (Callback e)) = e
7083
mkWidget :: forall v a. Callback' (Result v a) -> Widget v a
7184
mkWidget e = Widget (Callback e)
7285

86+
mapView :: forall u v a. (u -> v) -> Widget u a -> Widget v a
87+
mapView f (Widget w) = Widget (map (mapViewResult f) w)
88+
7389
instance applyWidget :: Apply (Widget v) where
7490
apply = ap
7591

src/Control/ShiftMap.purs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,18 @@ class ShiftMap s t where
1919
-- Instances for common transformers
2020
-- It's not possible to use the `map*` functions anymore
2121

22-
instance exceptShiftMap :: ShiftMap m (ExceptT e m) where
22+
instance ShiftMap m (ExceptT e m) where
2323
shiftMap f (ExceptT m) = ExceptT do f Right m
2424

25-
instance rwsShiftMap :: Monoid w => ShiftMap m (RWST r w s m) where
25+
instance Monoid w => ShiftMap m (RWST r w s m) where
2626
shiftMap f (RWST g) = RWST \r s -> f (\a -> RWSResult s a mempty) (g r s)
2727

28-
instance readerShiftMap :: ShiftMap m (ReaderT r m) where
28+
instance ShiftMap m (ReaderT r m) where
2929
shiftMap f (ReaderT m) = ReaderT \r -> f identity (m r)
3030

31-
instance stateShiftMap :: Monad m => ShiftMap m (StateT s m) where
31+
instance Monad m => ShiftMap m (StateT s m) where
3232
shiftMap f (StateT g) = StateT \s -> f (\a -> Tuple a s) (g s)
3333

34-
instance writerShiftMap :: Monoid w => ShiftMap m (WriterT w m) where
34+
instance Monoid w => ShiftMap m (WriterT w m) where
3535
shiftMap f (WriterT m) = WriterT do f (\a -> Tuple a mempty) m
36+

0 commit comments

Comments
 (0)