ability to stack onClose and onSubscribe options

This commit is contained in:
2025-11-20 17:07:06 +00:00
parent cfda8bce17
commit b251795d9d
5 changed files with 111 additions and 29 deletions

View File

@@ -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()
}
}
}
}