ability to stack onClose and onSubscribe options
This commit is contained in:
57
options.go
57
options.go
@@ -1,11 +1,11 @@
|
||||
package gubgub
|
||||
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
import "sync"
|
||||
|
||||
// TopicOptions holds common options for topics.
|
||||
type TopicOptions struct {
|
||||
mu sync.Mutex
|
||||
|
||||
// onClose is called after the Topic is closed and all messages have been delivered. Even
|
||||
// though you might call Close multiple times, topics are effectively closed only once thus
|
||||
// this should be called only once.
|
||||
@@ -15,16 +15,63 @@ type TopicOptions struct {
|
||||
onSubscribe func()
|
||||
}
|
||||
|
||||
func (to *TopicOptions) TriggerClose() {
|
||||
to.mu.Lock()
|
||||
defer to.mu.Unlock()
|
||||
|
||||
if to.onClose == nil {
|
||||
return
|
||||
}
|
||||
|
||||
to.onClose()
|
||||
}
|
||||
|
||||
func (to *TopicOptions) TriggerSubscribe() {
|
||||
to.mu.Lock()
|
||||
defer to.mu.Unlock()
|
||||
|
||||
if to.onSubscribe == nil {
|
||||
return
|
||||
}
|
||||
|
||||
to.onSubscribe()
|
||||
}
|
||||
|
||||
func (to *TopicOptions) Apply(opts ...TopicOption) {
|
||||
to.mu.Lock()
|
||||
defer to.mu.Unlock()
|
||||
|
||||
for _, opt := range opts {
|
||||
opt(to)
|
||||
}
|
||||
}
|
||||
|
||||
type TopicOption func(*TopicOptions)
|
||||
|
||||
func WithOnClose(fn func()) TopicOption {
|
||||
return func(opts *TopicOptions) {
|
||||
opts.onClose = sync.OnceFunc(fn)
|
||||
if opts.onClose == nil {
|
||||
opts.onClose = fn
|
||||
} else {
|
||||
oldFn := opts.onClose // preserve previous onClose handler
|
||||
opts.onClose = func() {
|
||||
fn()
|
||||
oldFn()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func WithOnSubscribe(fn func()) TopicOption {
|
||||
return func(opts *TopicOptions) {
|
||||
opts.onSubscribe = fn
|
||||
if opts.onSubscribe == nil {
|
||||
opts.onSubscribe = fn
|
||||
} else {
|
||||
oldFn := opts.onSubscribe // preserve previous onSubscribe handler
|
||||
opts.onSubscribe = func() {
|
||||
fn()
|
||||
oldFn()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user