Skip to content

Commit 09fc597

Browse files
committed
Remove Props. Add elArr'
1 parent 53537b9 commit 09fc597

File tree

5 files changed

+62
-117
lines changed

5 files changed

+62
-117
lines changed

src/Concur/Core/DOM.purs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import Data.Functor (class Functor, map)
1010
import Data.Unit (Unit)
1111
import Effect (Effect)
1212

13-
-- A view adapter for when the view is an array
1413
viewAdapter
1514
:: forall ps vs res
1615
. (ps -> vs -> res)
@@ -29,7 +28,7 @@ el
2928
-> m a
3029
el e props = shiftMap (\f w -> mkNodeWidget (\h v -> (e (map (map (h <<< f)) props) v)) w)
3130

32-
-- | el, but for array views
31+
-- | el for array views
3332
elArr
3433
:: forall f p v m a
3534
. ShiftMap (Widget (Array v)) m
@@ -52,7 +51,7 @@ elLeaf
5251
-> m a
5352
elLeaf e props = liftWidget $ mkLeafWidget \h -> e (map (map h) props)
5453

55-
-- | elLeaf but for array views
54+
-- | elLeaf for array views
5655
elLeafArr
5756
:: forall f p v m a
5857
. LiftWidget (Array v) m
@@ -65,6 +64,19 @@ elLeafArr e props = liftWidget $ mkLeafWidget \h -> [ e (map (map h) props) ]
6564

6665
-- | Wrap some widgets with a node that can have eventHandlers attached
6766
el'
67+
:: forall f p v m a
68+
. ShiftMap (Widget v) m
69+
=> MultiAlternative m
70+
=> Functor f
71+
=> Functor p
72+
=> (f (p (Effect Unit)) -> v -> v)
73+
-> f (p a)
74+
-> Array (m a)
75+
-> m a
76+
el' e props = el e props <<< orr
77+
78+
-- | el' for array views
79+
elArr'
6880
:: forall f p v m a
6981
. ShiftMap (Widget (Array v)) m
7082
=> MultiAlternative m
@@ -74,4 +86,5 @@ el'
7486
-> f (p a)
7587
-> Array (m a)
7688
-> m a
77-
el' e props = el (viewAdapter e) props <<< orr
89+
elArr' e props = el (viewAdapter e) props <<< orr
90+

src/Concur/Core/LiftWidget.purs

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,14 @@ module Concur.Core.LiftWidget where
22

33
import Concur.Core.Types (Widget)
44
import Control.Monad (class Monad)
5-
import Control.Monad.Except.Trans (ExceptT)
6-
import Control.Monad.RWS.Trans (RWST)
7-
import Control.Monad.Reader.Trans (ReaderT)
8-
import Control.Monad.State.Trans (StateT)
9-
import Control.Monad.Trans.Class (lift)
10-
import Control.Monad.Writer.Trans (WriterT)
5+
import Control.Monad.Trans.Class (class MonadTrans, lift)
116
import Data.Function (identity, (<<<))
12-
import Data.Monoid (class Monoid)
137

14-
-- | A way to lift widgets into higher monads
158
class LiftWidget v m where
169
liftWidget :: forall a. Widget v a -> m a
1710

18-
-- TODO: LiftWidget instance for all transformers
19-
-- instance liftWidgetTrans :: (Monad m, MonadTrans t, LiftWidget v m) => LiftWidget v (t m) where
20-
-- liftWidget = lift <<< liftWidget
21-
22-
-- Trivial self instance
23-
instance widgetLiftWidget :: LiftWidget v (Widget v) where
11+
instance LiftWidget v (Widget v) where
2412
liftWidget = identity
25-
26-
-- Instances for common transformers
27-
instance exceptLiftWidget :: (Monad m, LiftWidget v m) => LiftWidget v (ExceptT e m) where
13+
else instance (Monad m, MonadTrans t, LiftWidget v m) => LiftWidget v (t m) where
2814
liftWidget = lift <<< liftWidget
2915

30-
instance rwsLiftWidget :: (Monoid w, Monad m, LiftWidget v m) => LiftWidget v (RWST r w s m) where
31-
liftWidget = lift <<< liftWidget
32-
33-
instance readerLiftWidget :: (Monad m, LiftWidget v m) => LiftWidget v (ReaderT r m) where
34-
liftWidget = lift <<< liftWidget
35-
36-
instance stateLiftWidget :: (Monad m, LiftWidget v m) => LiftWidget v (StateT s m) where
37-
liftWidget = lift <<< liftWidget
38-
39-
instance writerLiftWidget :: (Monoid s, Monad m, LiftWidget v m) => LiftWidget v (WriterT s m) where
40-
liftWidget = lift <<< liftWidget

src/Concur/Core/Patterns.purs

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ retryUntilLoop :: forall m a. Monad m => (a -> Boolean) -> (a -> m a) -> a -> m
3737
retryUntilLoop p w a = w a >>= \a' -> if p a' then pure a' else retryUntilLoop p w a'
3838

3939
-- | The Elm Architecture
40-
tea :: forall a s m x. Monad m
40+
tea
41+
:: forall a s m x
42+
. Monad m
4143
=> s
4244
-> (s -> m a)
4345
-> (a -> s -> s)
@@ -54,7 +56,7 @@ type RemoteInterface v a =
5456
-- | Separate the UI from the widget result
5557
remoteWidget
5658
:: forall m v a
57-
. Monoid v
59+
. Monoid v
5860
=> MonadEffect m
5961
=> Widget v a
6062
-> m (RemoteInterface v a)
@@ -63,20 +65,20 @@ remoteWidget axn = do
6365
pure { yield: yield remoteCb, render: render remoteCb }
6466
where
6567
yield remoteCb = mkWidget \cb -> do
66-
Ref.modify_ (_ <> [cb]) remoteCb
68+
Ref.modify_ (_ <> [ cb ]) remoteCb
6769
pure do
6870
Ref.modify_ (Array.deleteBy unsafeRefEq cb) remoteCb
6971
render remoteCb = do
7072
val <- axn
7173
liftEffect do
72-
Ref.read remoteCb >>= traverse_ \cb -> cb (Completed val)
74+
Ref.read remoteCb >>= traverse_ \cb -> cb (Completed val)
7375

7476
-- | Internalise state
7577
-- The resulting widget can be rescheduled multiple
7678
-- times and will retain its state internally
7779
internalise
7880
:: forall m v a b
79-
. Monoid v
81+
. Monoid v
8082
=> MonadEffect m
8183
=> (a -> Widget v a)
8284
-> a
@@ -106,28 +108,32 @@ derive instance newtypeWire :: Newtype (Wire m a) _
106108
-- | Map a Lens over a Wire
107109
mapWire :: forall m s a. Alt m => MonadEffect m => Plus m => Lens' s a -> Wire m s -> Wire m a
108110
mapWire lens wire =
109-
let wirerec = un Wire wire
110-
in Wire { value: L.view lens <$> wirerec.value
111-
, send: \a -> wirerec.value >>= (wirerec.send <<< L.set lens a)
112-
, receive: L.view lens <$> wirerec.receive
113-
}
111+
let
112+
wirerec = un Wire wire
113+
in
114+
Wire
115+
{ value: L.view lens <$> wirerec.value
116+
, send: \a -> wirerec.value >>= (wirerec.send <<< L.set lens a)
117+
, receive: L.view lens <$> wirerec.receive
118+
}
114119

115120
-- | Setup a local environment with a wire
116121
local :: forall v r a. Monoid v => a -> (Wire (Widget v) a -> Widget v r) -> Widget v r
117122
local ainit f = do
118123
currentVal <- liftEffect $ Ref.new ainit
119124
remoteCb <- liftEffect $ Ref.new $ pure $ case _ of
120-
Completed a -> Ref.write a currentVal
121-
_ -> pure unit
122-
let wire = Wire
123-
{ value: Ref.read currentVal
124-
, send: \a -> do
125-
Ref.read remoteCb >>= traverse_ \cb -> cb (Completed a)
126-
, receive: mkWidget \cb -> do
127-
Ref.modify_ (_ <> [cb]) remoteCb
128-
pure do
129-
Ref.modify_ (Array.deleteBy unsafeRefEq cb) remoteCb
130-
}
125+
Completed a -> Ref.write a currentVal
126+
_ -> pure unit
127+
let
128+
wire = Wire
129+
{ value: Ref.read currentVal
130+
, send: \a -> do
131+
Ref.read remoteCb >>= traverse_ \cb -> cb (Completed a)
132+
, receive: mkWidget \cb -> do
133+
Ref.modify_ (_ <> [ cb ]) remoteCb
134+
pure do
135+
Ref.modify_ (Array.deleteBy unsafeRefEq cb) remoteCb
136+
}
131137
f wire
132138

133139
send :: forall m a. MonadEffect m => Plus m => Wire m a -> a -> m Unit

src/Concur/Core/Props.purs

Lines changed: 0 additions & 47 deletions
This file was deleted.

src/Concur/Core/Types.purs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import Effect (Effect)
2424
import Effect.Class (class MonadEffect)
2525
import Effect.Ref as Ref
2626

27-
-- | Callback -> Effect Canceler (returns the unused effect)
2827
newtype Callback a = Callback (Callback' a)
2928

3029
derive instance Newtype (Callback a) _
@@ -39,7 +38,7 @@ result f g = case _ of
3938
View v -> f v
4039
Completed a -> g a
4140

42-
instance functorResult :: Functor (Result v) where
41+
instance Functor (Result v) where
4342
map _ (View v) = View v
4443
map f (Completed a) = Completed (f a)
4544

@@ -54,7 +53,7 @@ mkCallback = Callback
5453
runCallback :: forall a. Callback a -> Callback' a
5554
runCallback (Callback f) = f
5655

57-
instance functorCallback :: Functor Callback where
56+
instance Functor Callback where
5857
map f c = mkCallback \cb -> runCallback c (cb <<< f)
5958

6059
-- | A callback that will never be resolved
@@ -64,7 +63,7 @@ never = mkCallback mempty
6463
-- NOTE: We currently have no monadic instance for callbacks
6564
-- Remember: The monadic instance *must* agree with the applicative instance
6665

67-
instance widgetShiftMap :: ShiftMap (Widget v) (Widget v) where
66+
instance ShiftMap (Widget v) (Widget v) where
6867
shiftMap f = f identity
6968

7069
-- A Widget is basically a callback that returns a view or a return value
@@ -86,20 +85,20 @@ mkWidget e = Widget (Callback e)
8685
mapView :: forall u v a. (u -> v) -> Widget u a -> Widget v a
8786
mapView f (Widget w) = Widget (map (mapViewResult f) w)
8887

89-
instance applyWidget :: Apply (Widget v) where
88+
instance Apply (Widget v) where
9089
apply = ap
9190

92-
instance widgetMonad :: Monad (Widget v)
91+
instance Monad (Widget v)
9392

94-
instance applicativeWidget :: Applicative (Widget v) where
93+
instance Applicative (Widget v) where
9594
pure a = mkWidget \cb -> cb (Completed a) $> pure mempty
9695

97-
instance monadRecWidget :: MonadRec (Widget v) where
96+
instance MonadRec (Widget v) where
9897
tailRecM k a = k a >>= case _ of
9998
Loop x -> tailRecM k x
10099
Done y -> pure y
101100

102-
instance bindWidget :: Bind (Widget v) where
101+
instance Bind (Widget v) where
103102
bind m f = mkWidget \cb -> do
104103
-- CancelerRef starts out as a canceler for A, then becomes canceler for B
105104
cancelerRef <- Ref.new mempty
@@ -140,7 +139,7 @@ initialised = case _ of
140139
Initialising -> Initialised
141140
a -> a
142141

143-
instance widgetMultiAlternative :: Monoid v => MultiAlternative (Widget v) where
142+
instance Monoid v => MultiAlternative (Widget v) where
144143
orr :: forall a. Array (Widget v a) -> Widget v a
145144
orr widgets = mkWidget \cb -> do
146145
viewsRef <- Ref.new (A.replicate (A.length widgets) mempty)
@@ -169,22 +168,21 @@ instance widgetMultiAlternative :: Monoid v => MultiAlternative (Widget v) where
169168
cb (View (fold views))
170169
pure runCancelers
171170

172-
instance widgetSemigroup :: (Monoid v) => Semigroup (Widget v a) where
171+
instance (Monoid v) => Semigroup (Widget v a) where
173172
append w1 w2 = orr [ w1, w2 ]
174173

175-
instance widgetMonoid :: (Monoid v) => Monoid (Widget v a) where
174+
instance (Monoid v) => Monoid (Widget v a) where
176175
mempty = empty
177176

178-
instance widgetAlt :: (Monoid v) => Alt (Widget v) where
177+
instance (Monoid v) => Alt (Widget v) where
179178
alt = append
180179

181-
instance widgetPlus :: (Monoid v) => Plus (Widget v) where
180+
instance (Monoid v) => Plus (Widget v) where
182181
empty = display mempty
183182

184-
instance widgetAlternative :: (Monoid v) => Alternative (Widget v)
183+
instance (Monoid v) => Alternative (Widget v)
185184

186-
-- Sync eff
187-
instance widgetMonadEff :: (Monoid v) => MonadEffect (Widget v) where
185+
instance (Monoid v) => MonadEffect (Widget v) where
188186
liftEffect eff = mkWidget \cb -> do
189187
a <- eff
190188
cb (Completed a)

0 commit comments

Comments
 (0)