Course map
miso // 16

Subscriptions

Read

A Sub is a source of actions

Some events do not come from an element you render: the clock ticking, the window resizing, a key pressed anywhere. For these a component declares subscriptions: functions that are given a sink and may call it with an action whenever they like.

data Action = Tick

app :: App Int Action
app = (component 0 update view)
  { subs = [ everySecond ] }

everySecond :: Sub Int Action
everySecond sink _ = forever $ do
  threadDelay 1000000
  sink Tick

update :: Action -> Effect () () Int Action
update Tick = this += 1

A Sub starts when the component mounts, runs in its own thread, and is stopped when the component unmounts. forever is from Control.Monad. The second argument (ignored here) lets a sub read the current model.

miso ships subs for the common cases: keyboardSub in Miso.Subscription for keys held down, windowSub for any window event, rAFSub for animation frames.

01 / 04