Skip to content

Commit

Permalink
Merge pull request #194 from dmjio/initial-action
Browse files Browse the repository at this point in the history
Added initial action
  • Loading branch information
dmjio authored Jul 20, 2017
2 parents 633ece5 + 2dc7d5b commit 72e9e48
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 12 deletions.
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,22 +82,28 @@ type Model = Int
data Action
= AddOne
| SubtractOne
| NoOp
| SayHelloWorld
deriving (Show, Eq)

-- | Entry point for a miso application
main :: IO ()
main = startApp App {..}
where
model = 0 -- initial model
update = updateModel -- update function
view = viewModel -- view function
events = defaultEvents -- default delegated events
subs = [] -- empty subscription list
initialAction = SayHelloWorld -- initial action to be executed on application load
model = 0 -- initial model
update = updateModel -- update function
view = viewModel -- view function
events = defaultEvents -- default delegated events
subs = [] -- empty subscription list

-- | Updates model, optionally introduces side effects
updateModel :: Action -> Model -> Effect Model Action
updateModel AddOne m = noEff (m + 1)
updateModel SubtractOne m = noEff (m - 1)
updateModel NoOp m = noEff m
updateModel SayHelloWorld m = m <# do
putStrLn "Hello World" >> pure NoOp

-- | Constructs a virtual DOM from a model
viewModel :: Model -> View Action
Expand Down
4 changes: 3 additions & 1 deletion examples/mario/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ data Action
= GetArrows !Arrows
| Time !Double
| WindowCoords !(Int,Int)
| NoOp

foreign import javascript unsafe "$r = performance.now();"
now :: IO Double
Expand All @@ -23,7 +24,7 @@ main :: IO ()
main = do
time <- now
let m = mario { time = time }
startApp App { model = m, ..}
startApp App { model = m, initialAction = NoOp, ..}
where
update = updateMario
view = display
Expand Down Expand Up @@ -63,6 +64,7 @@ mario = Model
}

updateMario :: Action -> Model -> Effect Model Action
updateMario NoOp m = noEff m
updateMario (GetArrows arrs) m = step newModel
where
newModel = m { arrows = arrs }
Expand Down
2 changes: 1 addition & 1 deletion examples/router/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ data Action
main :: IO ()
main = do
currentURI <- getCurrentURI
startApp App { model = Model currentURI, ..}
startApp App { model = Model currentURI, initialAction = NoOp, ..}
where
update = updateModel
events = defaultEvents
Expand Down
2 changes: 1 addition & 1 deletion examples/todo-mvc/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ data Msg
deriving Show

main :: IO ()
main = startApp App{..}
main = startApp App { initialAction = NoOp, ..}
where
model = emptyModel
update = updateModel
Expand Down
2 changes: 1 addition & 1 deletion examples/websocket/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import Miso.String (MisoString)
import qualified Miso.String as S

main :: IO ()
main = startApp App {..}
main = startApp App { initialAction = Id, ..}
where
model = Model mempty mempty
events = defaultEvents
Expand Down
7 changes: 6 additions & 1 deletion exe/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Miso
type Model = Int

main :: IO ()
main = startApp App {..}
main = startApp App { initialAction = SayHelloWorld, ..}
where
model = 0
update = updateModel
Expand All @@ -17,10 +17,15 @@ main = startApp App {..}
updateModel :: Action -> Model -> Effect Model Action
updateModel AddOne m = noEff (m + 1)
updateModel SubtractOne m = noEff (m - 1)
updateModel NoOp m = noEff m
updateModel SayHelloWorld m = m <# do
putStrLn "Hello World!" >> pure NoOp

data Action
= AddOne
| SubtractOne
| NoOp
| SayHelloWorld
deriving (Show, Eq)

viewModel :: Int -> View Action
Expand Down
6 changes: 4 additions & 2 deletions ghcjs-src/Miso.hs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ startApp App {..} = do
sub (readIORef modelRef) writeEvent
-- init event application thread
void . forkIO . forever $ do
action <- getEvent
newAction <- getEvent
modifyMVar_ actionsMVar $! \actions ->
pure (actions |> action)
pure (actions |> newAction)
-- Hack to get around `BlockedIndefinitelyOnMVar` exception
-- that occurs when no event handlers are present on a template
-- and `notify` is no longer in scope
Expand All @@ -70,6 +70,8 @@ startApp App {..} = do
viewRef <- newIORef initialVTree
-- Begin listening for events in the virtual dom
delegator viewRef events
-- Process initial action of application
writeEvent initialAction
-- Program loop, blocking on SkipChan
forever $ wait >> do
-- Apply actions to model
Expand Down
2 changes: 2 additions & 0 deletions ghcjs-src/Miso/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,7 @@ data App model action = App
-- ^ List of subscriptions to run during application lifetime
, events :: M.Map MisoString Bool
-- ^ List of delegated events that the body element will listen for
, initialAction :: action
-- ^ Initial action that is run after the application has loaded
}

0 comments on commit 72e9e48

Please sign in to comment.