Skip to content

Commit

Permalink
feat(Carpenter): allow update functions to dispatch actions by adding…
Browse files Browse the repository at this point in the history
… 'dispatch' as argument

BREAKING CHANGE: `Update` function now takes an extra `Dispatcher` as second argument
  • Loading branch information
arthurxavierx committed Sep 14, 2016
1 parent a1827e6 commit aea8112
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
7 changes: 5 additions & 2 deletions src/Carpenter.purs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type Yielder state eff = (state -> state) -> Aff (state :: React.ReactState Reac
-- | The supplied `yield` function asynchronously updates the component's state.
type Update state props action eff
= Yielder state eff
-> Dispatcher action
-> action
-> props
-> state
Expand Down Expand Up @@ -79,7 +80,9 @@ spec' state action update render = (React.spec state (getReactRender update rend
props <- React.getProps this
state <- React.readState this
let yield = mkYielder this
unsafeInterleaveEff (launchAff (update yield action props state))
let dispatch :: Dispatcher action
dispatch action = void $ unsafeInterleaveEff (launchAff (update yield dispatch action props state))
unsafeInterleaveEff (launchAff (update yield dispatch action props state))

mkYielder :: props state eff. React.ReactThis props state -> Yielder state eff
mkYielder this = yield
Expand All @@ -99,5 +102,5 @@ getReactRender update render this = do
children <- React.getChildren this
let yield = mkYielder this
let dispatch :: Dispatcher action
dispatch action = void $ unsafeInterleaveEff (launchAff (update yield action props state))
dispatch action = void $ unsafeInterleaveEff (launchAff (update yield dispatch action props state))
pure $ render dispatch props state children
27 changes: 16 additions & 11 deletions src/Carpenter/Cedar.purs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module Carpenter.Cedar

import Prelude
import React as React
import Carpenter (Dispatcher, mkYielder, Render, Update, EventHandler)
import Carpenter (Yielder, Dispatcher, mkYielder, Render, Update, EventHandler)
import Control.Monad.Aff (launchAff)
import Control.Monad.Eff.Class (liftEff)
import Control.Monad.Eff.Unsafe (unsafeInterleaveEff)
Expand Down Expand Up @@ -88,7 +88,8 @@ cedarSpec' action update render = reactSpec
props <- React.getProps this
state <- React.readState this
let yield = mkYielder this
unsafeInterleaveEff (launchAff (update yield action props state))
let dispatch = mkDispatcher update props state yield
unsafeInterleaveEff (launchAff (update yield dispatch action props state))

-- | Creates an element of the specificed React class with initial state
-- | and children, and captures its dispatched actions.
Expand Down Expand Up @@ -164,13 +165,17 @@ getReactRender update render this = do
state <- React.readState this
children <- React.getChildren this
let yield = mkYielder this
let dispatch :: Dispatcher action
dispatch action = void $ do
unsafeInterleaveEff $ launchAff do
new <- update yield action props state
liftEff $ case props.handler of
Capture f -> f action
Watch f -> f new
WatchAndCapture f -> f action new
_ -> pure unit
let dispatch = mkDispatcher update props state yield
pure $ render dispatch props state children

mkDispatcher :: state action eff. Update state (CedarProps state action) action eff -> (CedarProps state action) -> state -> Yielder state eff -> Dispatcher action
mkDispatcher update props state yield = dispatch
where
dispatch :: Dispatcher action
dispatch action = void $ unsafeInterleaveEff $ launchAff do
new <- update yield dispatch action props state
liftEff $ case props.handler of
Capture f -> f action
Watch f -> f new
WatchAndCapture f -> f action new
_ -> pure unit

0 comments on commit aea8112

Please sign in to comment.