Haskell Language Reactive-banana Injecting external events into the library


Example

This example is not tied to any concrete GUI toolkit, like reactive-banana-wx does, for instance. Instead it shows how to inject arbitary IO actions into FRP machinery.

The Control.Event.Handler module provides an addHandler function which creates a pair of AddHandler a and a -> IO () values. The former is used by reactive-banana itself to obtain an Event a value, while the latter is a plain function that is used to trigger the corresponding event.

import Data.Char (toUpper)

import Control.Event.Handler
import Reactive.Banana

main = do
    (inputHandler, inputFire) <- newAddHandler

In our case the a parameter of the handler is of type String, but the code that lets compiler infer that will be written later.

Now we define the EventNetwork that describes our FRP-driven system. This is done using compile function:

main = do
    (inputHandler, inputFire) <- newAddHandler
    compile $ do
        inputEvent <- fromAddHandler inputHandler

The fromAddHandler function transforms AddHandler a value into a Event a, which is covered in the next example.

Finally, we launch our "event loop", that would fire events on user input:

main = do
    (inputHandler, inputFire) <- newAddHandler
    compile $ do
        ...
    forever $ do
        input <- getLine
        inputFire input