1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-09 03:37:37 -05:00
v2fly/app/pubsub/pubsub.go

46 lines
1006 B
Go
Raw Normal View History

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