commit
587f64a87a
16
event.go
Normal file
16
event.go
Normal 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...)
|
||||||
|
}
|
18
micro.go
18
micro.go
@ -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
|
||||||
|
16
publisher.go
16
publisher.go
@ -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...)
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user