Skip to content

Commit 69bdf03

Browse files
authored
Tidying and refactoring (#4)
1 parent 03ad8ed commit 69bdf03

File tree

4 files changed

+22
-18
lines changed

4 files changed

+22
-18
lines changed

spago.dhall

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ You can edit this file as you like.
66
, license = "BSD-3-Clause"
77
, repository = "https://github.com/robertdp/purescript-react-halo.git"
88
, dependencies =
9-
[ "aff", "free", "freeap", "react-basic-hooks", "wire" ]
9+
[ "aff", "free", "freeap", "react-basic-hooks", "refs", "wire" ]
1010
, packages = ./packages.dhall
1111
, sources = [ "src/**/*.purs" ]
1212
}

src/React/Halo/Internal/Control.purs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import Control.Monad.Trans.Class (class MonadTrans, lift)
1111
import Control.Monad.Writer (class MonadTell, tell)
1212
import Control.Parallel (class Parallel)
1313
import Data.Bifunctor (lmap)
14-
import Data.Newtype (class Newtype, over)
1514
import Data.Tuple (Tuple)
1615
import Effect.Aff.Class (class MonadAff, liftAff)
1716
import Effect.Class (class MonadEffect, liftEffect)
@@ -90,13 +89,13 @@ instance monadRecHaloM :: MonadRec (HaloM props state action m) where
9089
Done y -> pure y
9190

9291
instance monadAskHaloM :: MonadAsk r m => MonadAsk r (HaloM props state action m) where
93-
ask = HaloM $ liftF $ Lift ask
92+
ask = lift ask
9493

9594
instance monadTellHaloM :: MonadTell w m => MonadTell w (HaloM props state action m) where
96-
tell = HaloM <<< liftF <<< Lift <<< tell
95+
tell = lift <<< tell
9796

9897
instance monadThrowHaloM :: MonadThrow e m => MonadThrow e (HaloM props state action m) where
99-
throwError = HaloM <<< liftF <<< Lift <<< throwError
98+
throwError = lift <<< throwError
10099

101100
-- | The Halo parallel evaluation applicative. It lifts `HaloM` into a free applicative.
102101
-- |
@@ -108,8 +107,6 @@ instance monadThrowHaloM :: MonadThrow e m => MonadThrow e (HaloM props state ac
108107
newtype HaloAp props state action m a
109108
= HaloAp (FreeAp (HaloM props state action m) a)
110109

111-
derive instance newtypeHaloAp :: Newtype (HaloAp props state action m a) _
112-
113110
derive newtype instance functorHaloAp :: Functor (HaloAp props state action m)
114111

115112
derive newtype instance applyHaloAp :: Apply (HaloAp props state action m)
@@ -131,10 +128,14 @@ hoist nat (HaloM component) = HaloM (hoistFree go component)
131128
Subscribe event k -> Subscribe event k
132129
Unsubscribe sid a -> Unsubscribe sid a
133130
Lift m -> Lift (nat m)
134-
Par par -> Par (over HaloAp (hoistFreeAp (hoist nat)) par)
131+
Par par -> Par (hoistAp nat par)
135132
Fork m k -> Fork (hoist nat m) k
136133
Kill fid a -> Kill fid a
137134

135+
-- | Hoist (transform) the base applicative of a `HaloAp` expression.
136+
hoistAp :: forall props state action m m'. Functor m => (m ~> m') -> HaloAp props state action m ~> HaloAp props state action m'
137+
hoistAp nat (HaloAp component) = HaloAp (hoistFreeAp (hoist nat) component)
138+
138139
-- | Read the current props.
139140
props :: forall props m action state. HaloM props state action m props
140141
props = HaloM (liftF (Props identity))

src/React/Halo/Internal/Eval.purs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
module React.Halo.Internal.Eval where
22

33
import Prelude
4-
import Control.Applicative.Free (hoistFreeAp, retractFreeAp)
4+
import Control.Applicative.Free (foldFreeAp)
55
import Control.Monad.Free (foldFree)
66
import Data.Either (either)
77
import Data.Foldable (sequence_, traverse_)
88
import Data.Map as Map
99
import Data.Maybe (Maybe(..))
1010
import Data.Tuple (Tuple(..))
1111
import Effect (Effect)
12-
import Effect.Aff (Aff, finally, parallel, sequential, throwError)
12+
import Effect.Aff (Aff, ParAff, finally, parallel, sequential, throwError)
1313
import Effect.Aff as Aff
14-
import Effect.Aff.Class (liftAff)
1514
import Effect.Class (liftEffect)
1615
import Effect.Ref as Ref
1716
import React.Halo.Internal.Control (HaloAp(..), HaloF(..), HaloM(..))
@@ -21,9 +20,13 @@ import React.Halo.Internal.Types (ForkId(..), Lifecycle(..), SubscriptionId(..))
2120
import Unsafe.Reference (unsafeRefEq)
2221
import Wire.Event as Event
2322

24-
-- | Interprets `HaloM` into the base monad `Aff`.
23+
-- | Interprets `HaloM` into the base monad `Aff` for asynchronous effects.
2524
evalHaloM :: forall props state action. HaloState props state action -> HaloM props state action Aff ~> Aff
26-
evalHaloM hs@(HaloState s) (HaloM halo) = foldFree (evalHaloF hs) halo
25+
evalHaloM hs (HaloM halo) = foldFree (evalHaloF hs) halo
26+
27+
-- | Interprets `HaloAp` into the base applicative `ParAff` for parallel effects.
28+
evalHaloAp :: forall props state action. HaloState props state action -> HaloAp props state action Aff ~> ParAff
29+
evalHaloAp hs (HaloAp halo) = foldFreeAp (parallel <<< evalHaloM hs) halo
2730

2831
-- | Interprets `HaloF` into the base monad `Aff`, keeping track of state in `HaloState`.
2932
evalHaloF :: forall props state action. HaloState props state action -> HaloF props state action Aff ~> Aff
@@ -54,8 +57,8 @@ evalHaloF hs@(HaloState s) = case _ of
5457
canceller <- Map.lookup sid <$> Ref.read s.subscriptions
5558
sequence_ canceller
5659
pure a
57-
Lift m -> liftAff m
58-
Par (HaloAp p) -> sequential $ retractFreeAp $ hoistFreeAp (parallel <<< evalHaloM hs) p
60+
Lift m -> m
61+
Par p -> sequential (evalHaloAp hs p)
5962
Fork fh k ->
6063
liftEffect do
6164
fid <- State.fresh ForkId hs

test/Main.purs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ runPropsTests = do
5252
initialProps = { value: "" }
5353

5454
expect x = liftEffect (Ref.read count) >>= shouldEqual x
55-
state <- State.createInitialState { props: initialProps, initialState: unit, eval, update: mempty }
55+
state <- State.createInitialState { props: initialProps, state: unit, eval, update: mempty }
5656
Eval.runInitialize state
5757
pure { state, initialProps, expect }
5858

@@ -88,7 +88,7 @@ runStateTests = do
8888
eval = case _ of
8989
Halo.Action f -> Halo.modify_ f
9090
_ -> pure unit
91-
state <- State.createInitialState { props: unit, initialState, eval, update }
91+
state <- State.createInitialState { props: unit, state: initialState, eval, update }
9292
Eval.runInitialize state
9393
let
9494
modify = liftEffect <<< Eval.handleAction state
@@ -106,7 +106,7 @@ runParallelismTests = do
106106
state <-
107107
State.createInitialState
108108
{ props: unit
109-
, initialState: 0
109+
, state: 0
110110
, update: \x -> Ref.write (Just x) internalState
111111
, eval:
112112
\_ -> do

0 commit comments

Comments
 (0)