-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added abstract on handler creation for event bus to topic
- Loading branch information
1 parent
ea6bfd7
commit 9b00a02
Showing
3 changed files
with
44 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,48 @@ | ||
package goeventbus | ||
|
||
type Topic struct { | ||
Address string | ||
Handlers []*Handler | ||
Interceptor []*Handler | ||
Address string | ||
Consumers []*Handler | ||
Interceptors []*Handler | ||
} | ||
|
||
func (t *Topic) AddInterceptor(interceptor Handler) *Handler { | ||
t.Interceptor = append(t.Interceptor, &interceptor) | ||
return &interceptor | ||
func (t *Topic) AddHandler(handler Handler) { | ||
switch handler.Type { | ||
case Consumer: | ||
t.addConsumer(handler) | ||
case Interceptor: | ||
t.addInterceptor(handler) | ||
} | ||
} | ||
|
||
func (t *Topic) AddHandler(handler Handler) *Handler { | ||
t.Handlers = append(t.Handlers, &handler) | ||
func (t *Topic) addConsumer(handler Handler) *Handler { | ||
t.Consumers = append(t.Consumers, &handler) | ||
return &handler | ||
} | ||
|
||
func (t *Topic) addInterceptor(interceptor Handler) *Handler { | ||
t.Interceptors = append(t.Interceptors, &interceptor) | ||
return &interceptor | ||
} | ||
|
||
func (t *Topic) GetHandlers() []*Handler { | ||
if len(t.Interceptor) > 0 { | ||
return t.Interceptor | ||
if len(t.Interceptors) > 0 { | ||
return t.Interceptors | ||
} | ||
|
||
return t.Handlers | ||
return t.Consumers | ||
} | ||
|
||
func (t *Topic) GetChannels() []chan Message { | ||
chs := []chan Message{} | ||
|
||
for _, item := range t.Handlers { | ||
for _, item := range t.Consumers { | ||
chs = append(chs, item.Ch) | ||
} | ||
|
||
return chs | ||
} | ||
|
||
func NewTopic(address string) *Topic { | ||
return &Topic{Address: address, Handlers: []*Handler{}, Interceptor: []*Handler{}} | ||
return &Topic{Address: address, Consumers: []*Handler{}, Interceptors: []*Handler{}} | ||
} |