2020-08-28 10:57:42 +03:00
|
|
|
package micro
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2021-10-02 19:55:07 +03:00
|
|
|
"go.unistack.org/micro/v3/client"
|
2020-08-28 10:57:42 +03:00
|
|
|
)
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
// Event is used to publish messages to a topic
|
|
|
|
type Event interface {
|
|
|
|
// Publish publishes a message to the event topic
|
|
|
|
Publish(ctx context.Context, msg interface{}, opts ...client.PublishOption) error
|
|
|
|
}
|
|
|
|
|
2020-08-28 10:57:42 +03:00
|
|
|
type event struct {
|
|
|
|
c client.Client
|
|
|
|
topic string
|
|
|
|
}
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
// NewEvent creates a new event publisher
|
|
|
|
func NewEvent(topic string, c client.Client) Event {
|
|
|
|
return &event{c, topic}
|
|
|
|
}
|
|
|
|
|
2020-08-28 10:57:42 +03:00
|
|
|
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...)
|
|
|
|
}
|