Skip to content

Commit

Permalink
issue-33: Make Events.Lock() private
Browse files Browse the repository at this point in the history
  • Loading branch information
lowitea committed Apr 29, 2022
1 parent b9a5586 commit 8ddcec3
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions bell.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ type Message interface{}

// Events thread safe structure stores events, their handlers and functions for management
type Events struct {
sync.RWMutex
channels map[string][]chan Message
mutex sync.RWMutex
wg sync.WaitGroup
channels map[string][]chan Message
queueSize uint
}

Expand All @@ -34,8 +34,8 @@ func New() *Events {

// Queue set events queue size
func (e *Events) Queue(size uint) *Events {
e.Lock()
defer e.Unlock()
e.mutex.Lock()
defer e.mutex.Unlock()

e.queueSize = size
return e
Expand All @@ -46,8 +46,8 @@ func (e *Events) Queue(size uint) *Events {
// handlerFunc - handler function
// copiesCount - count handlers copies run
func (e *Events) ListenN(event string, handlerFunc func(message Message), copiesCount uint) {
e.Lock()
defer e.Unlock()
e.mutex.Lock()
defer e.mutex.Unlock()

channel := make(chan Message, e.queueSize)

Expand Down Expand Up @@ -78,8 +78,8 @@ func (e *Events) Listen(event string, handlerFunc func(message Message)) {
// event - event name
// message - data that will be passed to the event handler
func (e *Events) Ring(event string, message Message) error {
e.RLock()
defer e.RUnlock()
e.mutex.RLock()
defer e.mutex.RUnlock()

if _, ok := e.channels[event]; !ok {
return fmt.Errorf("channel %s not found", event)
Expand All @@ -94,17 +94,17 @@ func (e *Events) Ring(event string, message Message) error {

// Has Checks if there are listeners for the passed event
func (e *Events) Has(event string) bool {
e.RLock()
defer e.RUnlock()
e.mutex.RLock()
defer e.mutex.RUnlock()

_, ok := e.channels[event]
return ok
}

// List Returns a list of events that listeners are subscribed to
func (e *Events) List() []string {
e.RLock()
defer e.RUnlock()
e.mutex.RLock()
defer e.mutex.RUnlock()

list := make([]string, 0, len(e.channels))
for event := range e.channels {
Expand All @@ -118,8 +118,8 @@ func (e *Events) List() []string {
//
// If you call the function without the "names" parameter, all listeners of all events will be removed.
func (e *Events) Remove(names ...string) {
e.Lock()
defer e.Unlock()
e.mutex.Lock()
defer e.mutex.Unlock()

if len(names) == 0 {
keys := make([]string, 0, len(e.channels))
Expand All @@ -141,8 +141,8 @@ func (e *Events) Remove(names ...string) {

// Wait Blocks the thread until all running events are completed
func (e *Events) Wait() {
e.Lock()
defer e.Unlock()
e.mutex.Lock()
defer e.mutex.Unlock()

e.wg.Wait()
}
Expand Down

0 comments on commit 8ddcec3

Please sign in to comment.