1
0
forked from MTSR/mapserver
mapserver/server/eventbus/eventbus.go

45 lines
746 B
Go
Raw Normal View History

2019-01-25 18:06:24 +03:00
package eventbus
import (
"sync"
)
2019-01-28 15:31:48 +03:00
const (
2019-01-28 16:33:32 +03:00
//TODO: move to separate package
2019-01-28 15:31:48 +03:00
MAPBLOCK_RENDERED = "mapblock-rendered"
TILE_RENDERED = "rendered-tile"
)
2019-01-25 18:06:24 +03:00
type Listener interface {
OnEvent(eventtype string, o interface{})
}
type Eventbus struct {
mutex *sync.RWMutex
listeners []Listener
}
func New() *Eventbus {
eb := Eventbus{}
eb.mutex = &sync.RWMutex{}
eb.listeners = make([]Listener, 0)
return &eb
}
func (this *Eventbus) Emit(eventtype string, o interface{}) {
this.mutex.RLock()
defer this.mutex.RUnlock()
for _, l := range this.listeners {
l.OnEvent(eventtype, o)
}
}
func (this *Eventbus) AddListener(l Listener) {
this.mutex.Lock()
defer this.mutex.Unlock()
this.listeners = append(this.listeners, l)
}