-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Permit multiple data subscriptions per original topic (#128)
* modify transformable event logic to simply add data * permit multiple data topics so sent hook gets calle for every block * rename test to match method rename. Co-authored-by: dirkmc <dirkmdev@gmail.com> Co-authored-by: acruikshank <acruikshank@example.com> Co-authored-by: dirkmc <dirkmdev@gmail.com>
- Loading branch information
1 parent
11d30c6
commit e9653de
Showing
12 changed files
with
188 additions
and
110 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.idea |
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package notifications | ||
|
||
import "sync" | ||
|
||
type TopicDataSubscriber struct { | ||
idMapLk sync.RWMutex | ||
data map[Topic][]TopicData | ||
Subscriber | ||
} | ||
|
||
// NewTopicDataSubscriber produces a subscriber that will transform | ||
// events and topics before passing them on to the given subscriber | ||
func NewTopicDataSubscriber(sub Subscriber) *TopicDataSubscriber { | ||
return &TopicDataSubscriber{ | ||
Subscriber: sub, | ||
data: make(map[Topic][]TopicData), | ||
} | ||
} | ||
|
||
func (m *TopicDataSubscriber) AddTopicData(id Topic, data TopicData) { | ||
m.idMapLk.Lock() | ||
m.data[id] = append(m.data[id], data) | ||
m.idMapLk.Unlock() | ||
} | ||
|
||
func (m *TopicDataSubscriber) getData(id Topic) []TopicData { | ||
m.idMapLk.RLock() | ||
defer m.idMapLk.RUnlock() | ||
|
||
data, ok := m.data[id] | ||
if !ok { | ||
return []TopicData{} | ||
} | ||
newData := make([]TopicData, len(data)) | ||
for i, d := range data { | ||
newData[i] = d | ||
} | ||
return newData | ||
} | ||
|
||
func (m *TopicDataSubscriber) OnNext(topic Topic, ev Event) { | ||
for _, data := range m.getData(topic) { | ||
m.Subscriber.OnNext(data, ev) | ||
} | ||
} | ||
|
||
func (m *TopicDataSubscriber) OnClose(topic Topic) { | ||
for _, data := range m.getData(topic) { | ||
m.Subscriber.OnClose(data) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
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
Oops, something went wrong.