@@ -236,14 +236,14 @@ func (b *Bus) AttachHandler(id string, fn EventHandler) (string, bool) {
236
236
return id , replaced
237
237
}
238
238
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 .
240
240
// You may provide either a string to be used as an exact match, or an instance of *regexp.Regexp to use for
241
241
// 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 {
244
244
return b .AttachHandler (id , fn )
245
245
}
246
- return b .AttachHandler (id , eventFilterFunc (topics , fn ))
246
+ return b .AttachHandler (id , eventFilterFunc (topicFilters , fn ))
247
247
}
248
248
249
249
// 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) {
257
257
if ch == nil {
258
258
panic (fmt .Sprintf ("AttachChannel called with id %q and nil channel" , id ))
259
259
}
260
- return b .AttachHandler (id , eventChanFunc ( ch ) )
260
+ return b .AttachHandler (id , func ( event Event ) { ch <- event } )
261
261
}
262
262
263
263
// 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
265
265
// 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 {
268
268
return b .AttachChannel (id , ch )
269
269
}
270
- return b .AttachHandler (id , eventChanFilterFunc (topics , ch ))
270
+ return b .AttachHandler (id , eventChanFilterFunc (topicFilters , ch ))
271
271
}
272
272
273
273
// 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,
396
396
}
397
397
}
398
398
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
-
462
399
func eventFilterFunc (topics []any , fn EventHandler ) EventHandler {
463
400
var (
464
- stl int
465
- st []string
466
- rtl int
467
- rt []* regexp.Regexp
401
+ st []string
402
+ rt []* regexp.Regexp
468
403
)
469
404
for i := range topics {
470
405
if s , ok := topics [i ].(string ); ok {
471
406
st = append (st , s )
472
- stl ++
473
407
} else if r , ok := topics [i ].(* regexp.Regexp ); ok {
474
408
rt = append (rt , r )
475
- rtl ++
476
409
} else {
477
410
panic (fmt .Sprintf ("cannot handle filter of type %T, expected %T or %T" , topics [i ], "" , (* regexp .Regexp )(nil )))
478
411
}
479
412
}
480
413
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
-
502
414
return func (event Event ) {
503
415
for i := range st {
504
416
if st [i ] == event .Topic {
505
- ch <- event
417
+ fn ( event )
506
418
return
507
419
}
508
420
}
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 ) {
523
421
for i := range rt {
524
422
if rt [i ].MatchString (event .Topic ) {
525
- ch <- event
423
+ fn ( event )
526
424
return
527
425
}
528
426
}
529
427
}
530
428
}
531
429
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
+
533
445
return func (event Event ) {
534
446
for i := range st {
535
447
if st [i ] == event .Topic {
@@ -545,33 +457,3 @@ func eventChanCombinedFilterFunc(st []string, rt []*regexp.Regexp, ch EventChann
545
457
}
546
458
}
547
459
}
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