Events - tympanix/artoodetoo GitHub Wiki

Example

package myevent

import "time"

type MyEvent struct {
	event.Base
	Interval int `io:"input"`
        Count    int `io:"output"`
}

func init() {
	event.Register(new(MyEvent))
}

func (e *MyEvent) Describe() string {
	return "An example event"
}

func (e *MyEvent) Listen(stop <-chan struct{}) error {
        seconds := time.Duration(e.Interval) * time.Second
	for range time.NewTicker(seconds) {
                e.Count++
                e.Trigger()
        }
}

The above example triggers for every second defined by the Interval input. Every time the event triggers the Count variable is increased and supplied as output. You must embed the event.Base type to able to register your event. The Listen() function should block, and keep listening in an endless lopp, rendezvous - what have you. You call the Trigger() function every time the event should trigger.