GenEvent
Event Handling Principles
See: Erlang gen_event Behaviour
GenEvent Typeclass
class GenEvent e st | e -> st, st -> e where
handleEvent :: HandleEvent e st
Event Manager Example
module Demo.Event
( Event(..)
, start
, notify
) where
import Prelude
import Control.Behaviour.GenEvent
( class GenEvent
, Init
, initOk
, HandleEvent
, startLinkWith
)
import Control.Behaviour.GenEvent as E
data Event = EventA | EventB
data State = State [Event]
instance GenEvent Event State where
handleEvent = handleEvent
name :: Atom
name = :event
start :: Process Pid
start = startLinkWith name init
notify :: Event -> Process ()
notify = E.notify name
init :: Init State
init = initOk (State [])
handleEvent :: HandleEvent Event State
handleEvent e (State events) = do
println "Event"
return $ State [e|events]
Start a Event Manager process
-- | Start a standalone Event Manager process.
start :: forall e st. GenEvent e st => (Init st) -> Process Pid
startWith :: forall e st. GenEvent e st => Name -> (Init st) -> Process Pid
-- | Start a Event Manager process as part of a supervision tree.
startLink :: forall e st. GenEvent e st => (Init st) -> Process Pid
startLinkWith :: forall e st. GenEvent e st => Name -> (Init st) -> Process Pid
Init callback
data InitResult st
= InitOk st
| InitOkHib st
| InitError ExitReason
-- | Init callback
type Init st = Process (InitResult st)
HandleEvent Callback
-- | HandleEvent callback
type HandleEvent e st = e -> st -> Process st
Client APIs
notify :: forall e. Name -> e -> Process ()
syncNotify :: forall e. Name -> e -> Process ()
当前内容版权归 hamler-lang 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 hamler-lang .