Copyright | (C) 2015 Dimitri Sabadie |
---|---|
License | BSD3 |
Maintainer | Dimitri Sabadie <dimitri.sabadie@gmail.com> |
Stability | experimental |
Portability | portable |
Safe Haskell | Safe |
Language | Haskell2010 |
An
is an object representing an event of type Event
aa
. You can
register actions through it – see the on
function – and detach them later
on.
An Event
has many purposes. The one in mind when writing that package was
to interface over C callback-based reactive system. Consider the
following Haskell wrapper function, which is based on imperative style:
-- Create a new button and register an action to launch when the button’s -- state changes. createButton :: (ButtonState -> IO ()) -> IO Button createButton callback = do -- create the button button <- ... forkIO . forever $ do -- launch a thread in which we can test whether the state has changed when stateHasChanged $ callback newState pure button
We can enhance that by representing the action of registering to the event and detaching from it by immediately returning a value:
createButton :: IO (Button,Event ButtonState)
createButton = do
-- create the button
button <- ...
-- create an Event
(ev,t) <- newEvent
forkIO . forever $
-- check the new state
when stateHasChanged $ trigger t newState
pure (button,ev)
The Trigger
can also be returned to manually invoke the Event
.
Events
An
is a value of type Event
aa
with no direct representation. It lives
in the future. It’s possible to register actions with on
to execute
when data becomes available, and to detach those actions with the
resulting Detach
object by calling detach
on it.
Event
s can be triggered with the trigger
function and the associated
type Trigger
.