Exemplo n.º 1
0
// PublishEvent publishes an event to all handlers capable of handling it.
func (b *EventBus) PublishEvent(event eh.Event) {
	// Handle the event if there is a handler registered.
	if handlers, ok := b.handlers[event.EventType()]; ok {
		for handler := range handlers {
			handler.HandleEvent(event)
		}
	}

	// Notify all observers about the event.
	b.notify(event)
}
Exemplo n.º 2
0
func (b *EventBus) notify(event eh.Event) {
	conn := b.pool.Get()
	defer conn.Close()
	if err := conn.Err(); err != nil {
		log.Printf("error: event bus publish: %v\n", err)
	}

	// Marshal event data.
	var data []byte
	var err error
	if data, err = bson.Marshal(event); err != nil {
		log.Printf("error: event bus publish: %v\n", ErrCouldNotMarshalEvent)
	}

	// Publish all events on their own channel.
	if _, err = conn.Do("PUBLISH", b.prefix+string(event.EventType()), data); err != nil {
		log.Printf("error: event bus publish: %v\n", err)
	}
}