-
Notifications
You must be signed in to change notification settings - Fork 11
/
publisher.go
37 lines (30 loc) · 1.09 KB
/
publisher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package courier
import (
"context"
)
// Publisher defines behaviour of an MQTT publisher that can send messages.
type Publisher interface {
// Publish allows to publish messages to an MQTT broker
Publish(ctx context.Context, topic string, message interface{}, options ...Option) error
}
// PublisherFunc defines signature of a Publish function.
type PublisherFunc func(context.Context, string, interface{}, ...Option) error
// Publish implements Publisher interface on PublisherFunc.
func (f PublisherFunc) Publish(
ctx context.Context,
topic string,
message interface{},
opts ...Option,
) error {
return f(ctx, topic, message, opts...)
}
type publishMiddleware interface {
// Middleware helps chain Publisher(s).
Middleware(publisher Publisher) Publisher
}
// PublisherMiddlewareFunc functions are closures that intercept Publisher.Publish calls.
type PublisherMiddlewareFunc func(Publisher) Publisher
// Middleware allows PublisherMiddlewareFunc to implement the publishMiddleware interface.
func (pmw PublisherMiddlewareFunc) Middleware(publisher Publisher) Publisher {
return pmw(publisher)
}