1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-17 09:56:18 -05:00
v2fly/app/pubsub/pubsub.go
2016-01-31 17:01:28 +01:00

46 lines
1006 B
Go

package pubsub
import (
"github.com/v2ray/v2ray-core/app"
)
const (
APP_ID = app.ID(3)
)
type PubsubMessage []byte
type TopicHandler func(PubsubMessage)
type Pubsub interface {
Publish(topic string, message PubsubMessage)
Subscribe(topic string, handler TopicHandler)
}
type pubsubWithContext interface {
Publish(context app.Context, topic string, message PubsubMessage)
Subscribe(context app.Context, topic string, handler TopicHandler)
}
type contextedPubsub struct {
context app.Context
pubsub pubsubWithContext
}
func (this *contextedPubsub) Publish(topic string, message PubsubMessage) {
this.pubsub.Publish(this.context, topic, message)
}
func (this *contextedPubsub) Subscribe(topic string, handler TopicHandler) {
this.pubsub.Subscribe(this.context, topic, handler)
}
func init() {
app.RegisterApp(APP_ID, func(context app.Context, obj interface{}) interface{} {
pubsub := obj.(pubsubWithContext)
return &contextedPubsub{
context: context,
pubsub: pubsub,
}
})
}