types.go 1.52 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
package notifications

// Topic is a topic that events appear on
type Topic interface{}

// Event is a publishable event
type Event interface{}

// Subscriber is a subscriber that can receive events
type Subscriber interface {
	OnNext(Topic, Event)
	OnClose(Topic)
}

// MappableSubscriber is a subscriber that remaps events received to other topics
// and events
type MappableSubscriber interface {
	Subscriber
	Map(sourceID Topic, destinationID Topic)
}

// Subscribable is a stream that can be subscribed to
type Subscribable interface {
	Subscribe(topic Topic, sub Subscriber) bool
	Unsubscribe(sub Subscriber) bool
}

// Publisher is an publisher of events that can be subscribed to
type Publisher interface {
	Close(Topic)
	Publish(Topic, Event)
	Shutdown()
33
	Startup()
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
	Subscribable
}

// EventTransform if a fucntion transforms one kind of event to another
type EventTransform func(Event) Event

// Notifee is a mappable suscriber where you want events to appear
// on this specified topic (used to call SubscribeOn to setup a remapping)
type Notifee struct {
	Topic      Topic
	Subscriber MappableSubscriber
}

// SubscribeOn subscribes to the given subscribe on the given topic, but
// maps to a differnt topic specified in a notifee which has a mappable
// subscriber
func SubscribeOn(p Subscribable, topic Topic, notifee Notifee) {
	notifee.Subscriber.Map(topic, notifee.Topic)
	p.Subscribe(topic, notifee.Subscriber)
}

// IdentityTransform sets up an event transform that makes no changes
func IdentityTransform(ev Event) Event { return ev }