moved options into their own file and more tests

This commit is contained in:
2024-09-22 22:06:16 +01:00
parent 7ea3d18e08
commit 391900a12a
3 changed files with 106 additions and 5 deletions

View File

@@ -1,23 +1,29 @@
package gubgub
import (
"sync"
)
// TopicOptions holds common options for topics.
type TopicOptions struct {
// onClose is called after the Topic is closed and all messages have been delivered.
// 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.
onClose func()
// onSubscribe is called after a new subscriber is regitered.
onSubscribe func(count int)
onSubscribe func()
}
type TopicOption func(*TopicOptions)
func WithOnClose(fn func()) TopicOption {
return func(opts *TopicOptions) {
opts.onClose = fn
opts.onClose = sync.OnceFunc(fn)
}
}
func WithOnSubscribe(fn func(count int)) TopicOption {
func WithOnSubscribe(fn func()) TopicOption {
return func(opts *TopicOptions) {
opts.onSubscribe = fn
}