types.go 1.4 KB
Newer Older
1 2 3 4 5 6 7 8
package notifications

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

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

9 10 11
// TopicData is data added to every message broadcast on a topic
type TopicData interface{}

12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
// Subscriber is a subscriber that can receive events
type Subscriber interface {
	OnNext(Topic, Event)
	OnClose(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()
29
	Startup()
30 31 32 33 34 35
	Subscribable
}

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

36 37
// Notifee is a topic data subscriber plus a set of data you want to add to any topics subscribed to
// (used to call SubscribeWithData to inject data when events for a given topic emit)
38
type Notifee struct {
39 40
	Data      TopicData
	Subscriber *TopicDataSubscriber
41 42
}

43 44 45 46
// SubscribeWithData subscribes to the given subscriber on the given topic, and adds the notifies
// custom data into the list of data injected into callbacks when events occur on that topic
func SubscribeWithData(p Subscribable, topic Topic, notifee Notifee) {
	notifee.Subscriber.AddTopicData(topic, notifee.Data)
47 48
	p.Subscribe(topic, notifee.Subscriber)
}