publisher => event

This commit is contained in:
Asim Aslam 2019-12-17 23:05:46 +00:00
parent dda96cb87e
commit cb9c4c3aef
3 changed files with 29 additions and 21 deletions

16
event.go Normal file
View File

@ -0,0 +1,16 @@
package micro
import (
"context"
"github.com/micro/go-micro/client"
)
type event struct {
c client.Client
topic string
}
func (e *event) Publish(ctx context.Context, msg interface{}, opts ...client.PublishOption) error {
return e.c.Publish(ctx, e.c.NewMessage(e.topic, msg), opts...)
}

View File

@ -62,12 +62,15 @@ type Resource interface {
} }
*/ */
// Publisher is uses to publish messages to a topic // Event is used to publish messages to a topic
type Publisher interface { type Event interface {
// Publish publishes a message to the event topic // Publish publishes a message to the event topic
Publish(ctx context.Context, msg interface{}, opts ...client.PublishOption) error Publish(ctx context.Context, msg interface{}, opts ...client.PublishOption) error
} }
// Type alias to satisfy the deprecation
type Publisher = Event
type Option func(*Options) type Option func(*Options)
var ( var (
@ -95,12 +98,17 @@ func NewFunction(opts ...Option) Function {
return newFunction(opts...) return newFunction(opts...)
} }
// NewPublisher returns a new Publisher // NewEvent creates a new event publisher
func NewPublisher(topic string, c client.Client) Publisher { func NewEvent(topic string, c client.Client) Event {
if c == nil { if c == nil {
c = client.NewClient() c = client.NewClient()
} }
return &publisher{c, topic} return &event{c, topic}
}
// Deprecated: NewPublisher returns a new Publisher
func NewPublisher(topic string, c client.Client) Event {
return NewEvent(topic, c)
} }
// RegisterHandler is syntactic sugar for registering a handler // RegisterHandler is syntactic sugar for registering a handler

View File

@ -1,16 +0,0 @@
package micro
import (
"context"
"github.com/micro/go-micro/client"
)
type publisher struct {
c client.Client
topic string
}
func (p *publisher) Publish(ctx context.Context, msg interface{}, opts ...client.PublishOption) error {
return p.c.Publish(ctx, p.c.NewMessage(p.topic, msg), opts...)
}