Skip to content

Commit efa0c78

Browse files
committed
simplifying filtered handlers
1 parent dbea9a6 commit efa0c78

File tree

1 file changed

+28
-146
lines changed

1 file changed

+28
-146
lines changed

seb.go

+28-146
Original file line numberDiff line numberDiff line change
@@ -236,14 +236,14 @@ func (b *Bus) AttachHandler(id string, fn EventHandler) (string, bool) {
236236
return id, replaced
237237
}
238238

239-
// AttachFilteredHandler attaches a handler that will only be called when events are published to specific workers.
239+
// AttachFilteredHandler attaches a handler that will only be called when events are published to specific topics.
240240
// You may provide either a string to be used as an exact match, or an instance of *regexp.Regexp to use for
241241
// fuzzy matching. Exact string matches are tested first, followed by fuzzy matches.
242-
func (b *Bus) AttachFilteredHandler(id string, fn EventHandler, topics ...any) (string, bool) {
243-
if len(topics) == 0 {
242+
func (b *Bus) AttachFilteredHandler(id string, fn EventHandler, topicFilters ...any) (string, bool) {
243+
if len(topicFilters) == 0 {
244244
return b.AttachHandler(id, fn)
245245
}
246-
return b.AttachHandler(id, eventFilterFunc(topics, fn))
246+
return b.AttachHandler(id, eventFilterFunc(topicFilters, fn))
247247
}
248248

249249
// AttachChannel immediately adds the provided channel to the list of recipients for new
@@ -257,17 +257,17 @@ func (b *Bus) AttachChannel(id string, ch EventChannel) (string, bool) {
257257
if ch == nil {
258258
panic(fmt.Sprintf("AttachChannel called with id %q and nil channel", id))
259259
}
260-
return b.AttachHandler(id, eventChanFunc(ch))
260+
return b.AttachHandler(id, func(event Event) { ch <- event })
261261
}
262262

263263
// AttachFilteredChannel attaches a channel will only have events pushed to it when they are published to specific
264-
// workers. You may provide either a string to be used as an exact match, or an instance of *regexp.Regexp to use for
264+
// topics. You may provide either a string to be used as an exact match, or an instance of *regexp.Regexp to use for
265265
// fuzzy matching. Exact string matches are tested first, followed by fuzzy matches.
266-
func (b *Bus) AttachFilteredChannel(id string, ch EventChannel, topics ...any) (string, bool) {
267-
if len(topics) == 0 {
266+
func (b *Bus) AttachFilteredChannel(id string, ch EventChannel, topicFilters ...any) (string, bool) {
267+
if len(topicFilters) == 0 {
268268
return b.AttachChannel(id, ch)
269269
}
270-
return b.AttachHandler(id, eventChanFilterFunc(topics, ch))
270+
return b.AttachHandler(id, eventChanFilterFunc(topicFilters, ch))
271271
}
272272

273273
// DetachRecipient immediately removes the provided recipient from receiving any new events, returning true if a
@@ -396,140 +396,52 @@ func (b *Bus) doRequest(ctx context.Context, to, topic string, data any) (Reply,
396396
}
397397
}
398398

399-
func eventChanFunc(ch EventChannel) EventHandler {
400-
return func(event Event) {
401-
ch <- event
402-
}
403-
}
404-
405-
func eventStringFilterFunc(st []string, fn EventHandler) EventHandler {
406-
if len(st) == 1 {
407-
st0 := st[0]
408-
return func(event Event) {
409-
if st0 == event.Topic {
410-
fn(event)
411-
}
412-
}
413-
}
414-
415-
return func(event Event) {
416-
for i := range st {
417-
if st[i] == event.Topic {
418-
fn(event)
419-
return
420-
}
421-
}
422-
}
423-
}
424-
425-
func eventRegexpFilterFunc(rt []*regexp.Regexp, fn EventHandler) EventHandler {
426-
if len(rt) == 0 {
427-
rt0 := rt[0]
428-
return func(event Event) {
429-
if rt0.MatchString(event.Topic) {
430-
fn(event)
431-
}
432-
}
433-
}
434-
435-
return func(event Event) {
436-
for i := range rt {
437-
if rt[i].MatchString(event.Topic) {
438-
fn(event)
439-
return
440-
}
441-
}
442-
}
443-
}
444-
445-
func eventCombinedFilterFunc(st []string, rt []*regexp.Regexp, fn EventHandler) EventHandler {
446-
return func(event Event) {
447-
for i := range st {
448-
if st[i] == event.Topic {
449-
fn(event)
450-
return
451-
}
452-
}
453-
for i := range rt {
454-
if rt[i].MatchString(event.Topic) {
455-
fn(event)
456-
return
457-
}
458-
}
459-
}
460-
}
461-
462399
func eventFilterFunc(topics []any, fn EventHandler) EventHandler {
463400
var (
464-
stl int
465-
st []string
466-
rtl int
467-
rt []*regexp.Regexp
401+
st []string
402+
rt []*regexp.Regexp
468403
)
469404
for i := range topics {
470405
if s, ok := topics[i].(string); ok {
471406
st = append(st, s)
472-
stl++
473407
} else if r, ok := topics[i].(*regexp.Regexp); ok {
474408
rt = append(rt, r)
475-
rtl++
476409
} else {
477410
panic(fmt.Sprintf("cannot handle filter of type %T, expected %T or %T", topics[i], "", (*regexp.Regexp)(nil)))
478411
}
479412
}
480413

481-
if stl > 0 && rtl > 0 {
482-
return eventCombinedFilterFunc(st, rt, fn)
483-
} else if stl > 0 {
484-
return eventStringFilterFunc(st, fn)
485-
} else if rtl > 0 {
486-
return eventRegexpFilterFunc(rt, fn)
487-
} else {
488-
return func(_ Event) {}
489-
}
490-
}
491-
492-
func eventChanStringFilterFunc(st []string, ch EventChannel) EventHandler {
493-
if len(st) == 1 {
494-
st0 := st[0]
495-
return func(event Event) {
496-
if st0 == event.Topic {
497-
ch <- event
498-
}
499-
}
500-
}
501-
502414
return func(event Event) {
503415
for i := range st {
504416
if st[i] == event.Topic {
505-
ch <- event
417+
fn(event)
506418
return
507419
}
508420
}
509-
}
510-
}
511-
512-
func eventChanRegexpFilterFunc(rt []*regexp.Regexp, ch EventChannel) EventHandler {
513-
if len(rt) == 0 {
514-
rt0 := rt[0]
515-
return func(event Event) {
516-
if rt0.MatchString(event.Topic) {
517-
ch <- event
518-
}
519-
}
520-
}
521-
522-
return func(event Event) {
523421
for i := range rt {
524422
if rt[i].MatchString(event.Topic) {
525-
ch <- event
423+
fn(event)
526424
return
527425
}
528426
}
529427
}
530428
}
531429

532-
func eventChanCombinedFilterFunc(st []string, rt []*regexp.Regexp, ch EventChannel) EventHandler {
430+
func eventChanFilterFunc(topicFilters []any, ch EventChannel) EventHandler {
431+
var (
432+
st []string
433+
rt []*regexp.Regexp
434+
)
435+
for i := range topicFilters {
436+
if s, ok := topicFilters[i].(string); ok {
437+
st = append(st, s)
438+
} else if r, ok := topicFilters[i].(*regexp.Regexp); ok {
439+
rt = append(rt, r)
440+
} else {
441+
panic(fmt.Sprintf("cannot handle filter of type %T, expected %T or %T", topicFilters[i], "", (*regexp.Regexp)(nil)))
442+
}
443+
}
444+
533445
return func(event Event) {
534446
for i := range st {
535447
if st[i] == event.Topic {
@@ -545,33 +457,3 @@ func eventChanCombinedFilterFunc(st []string, rt []*regexp.Regexp, ch EventChann
545457
}
546458
}
547459
}
548-
549-
func eventChanFilterFunc(topics []any, ch EventChannel) EventHandler {
550-
var (
551-
stl int
552-
st []string
553-
rtl int
554-
rt []*regexp.Regexp
555-
)
556-
for i := range topics {
557-
if s, ok := topics[i].(string); ok {
558-
st = append(st, s)
559-
stl++
560-
} else if r, ok := topics[i].(*regexp.Regexp); ok {
561-
rt = append(rt, r)
562-
rtl++
563-
} else {
564-
panic(fmt.Sprintf("cannot handle filter of type %T, expected %T or %T", topics[i], "", (*regexp.Regexp)(nil)))
565-
}
566-
}
567-
568-
if stl > 0 && rtl > 0 {
569-
return eventChanCombinedFilterFunc(st, rt, ch)
570-
} else if stl > 0 {
571-
return eventChanStringFilterFunc(st, ch)
572-
} else if rtl > 0 {
573-
return eventChanRegexpFilterFunc(rt, ch)
574-
} else {
575-
return func(_ Event) {}
576-
}
577-
}

0 commit comments

Comments
 (0)