diff --git a/util/event.go b/util/event.go new file mode 100644 index 0000000..21a9d0e --- /dev/null +++ b/util/event.go @@ -0,0 +1,41 @@ +package dataswamp + +type Event struct { + eventName string + payload interface {} +} + +func NewEvent(eventName string, payload interface{}) *Event{ + return &Event{ + eventName, + payload, + } +} + +func (e *Event) EventName() string { + return e.eventName +} + +func (e *Event) Payload() interface{} { + return e.payload +} + +type EventHandler func(e Event) + +type EventBus struct { + handlers map[string][]EventHandler +} + +func NewEventBus() *EventBus{ + return &EventBus{ make(map[string][]EventHandler) } +} + +func (eb *EventBus) Register(eventName string, handler EventHandler) { + eb.handlers[eventName] = append(eb.handlers[eventName], handler) +} + +func (eb *EventBus) Handle(e Event){ + for _, handler := range eb.handlers[e.EventName()] { + handler(e) + } +} diff --git a/util/logging.go b/util/logging.go new file mode 100644 index 0000000..c28c75f --- /dev/null +++ b/util/logging.go @@ -0,0 +1,7 @@ +package dataswamp + +type Logger interface { + Debug(format string, a ...interface{}) + Info(format string, a ...interface{}) + Warn(format string, a ...interface{}) +}