some progress but still doesn't work

This commit is contained in:
evilsocket 2018-03-01 19:30:10 +01:00
commit b243e67828
4 changed files with 168 additions and 32 deletions

View file

@ -39,21 +39,29 @@ func (e Event) Label() string {
type EventPool struct {
sync.Mutex
NewEvents chan Event
debug bool
silent bool
events []Event
listeners []chan Event
}
func NewEventPool(debug bool, silent bool) *EventPool {
return &EventPool{
NewEvents: make(chan Event, 0xff),
debug: debug,
silent: silent,
events: make([]Event, 0),
listeners: make([]chan Event, 0),
}
}
func (p *EventPool) Listen() <-chan Event {
p.Lock()
defer p.Unlock()
l := make(chan Event)
p.listeners = append(p.listeners, l)
return l
}
func (p *EventPool) SetSilent(s bool) {
p.Lock()
defer p.Unlock()
@ -71,7 +79,11 @@ func (p *EventPool) Add(tag string, data interface{}) {
defer p.Unlock()
e := NewEvent(tag, data)
p.events = append([]Event{e}, p.events...)
p.NewEvents <- e
// broadcast the event to every listener
for _, l := range p.listeners {
l <- e
}
}
func (p *EventPool) Log(level int, format string, args ...interface{}) {