have callback additions use a mutex

This commit is contained in:
Liam Stanley 2016-11-18 20:11:13 -05:00
parent ea9381b0ce
commit 78105514d1
2 changed files with 12 additions and 1 deletions

@ -50,12 +50,16 @@ func (c *Client) RunCallbacks(event *Event) {
// AddCallbackHandler registers a callback (matching the Callback
// interface) for the given command.
func (c *Client) AddCallbackHandler(cmd string, callback Callback) {
c.cbMux.Lock()
c.callbacks[cmd] = append(c.callbacks[cmd], callback)
c.cbMux.Unlock()
}
// AddCallback registers the callback function for the given command.
func (c *Client) AddCallback(cmd string, callback func(c *Client, e Event)) {
c.cbMux.Lock()
c.callbacks[cmd] = append(c.callbacks[cmd], CallbackFunc(callback))
c.cbMux.Unlock()
}
// AddBgCallback registers the callback function for the given command
@ -63,7 +67,9 @@ func (c *Client) AddCallback(cmd string, callback func(c *Client, e Event)) {
//
// Runs after all other callbacks have been ran.
func (c *Client) AddBgCallback(cmd string, callback func(c *Client, e Event)) {
c.cbMux.Lock()
c.callbacks["routine_"+cmd] = append(c.callbacks["routine_"+cmd], CallbackFunc(callback))
c.cbMux.Unlock()
}
// Callback is lower level implementation of Client.AddCallback().

@ -24,6 +24,7 @@ import (
"io/ioutil"
"log"
"net"
"sync"
"time"
)
@ -37,12 +38,16 @@ type Client struct {
// Sender is a Sender{} interface implementation.
Sender Sender
// state represents the internal state
// state represents the throw-away state for the irc session.
state *state
// initTime represents the creation time of the client.
initTime time.Time
// cbLock is the internal locking mechanism for the callbacks map.
cbMux sync.Mutex
// callbacks is an internal mapping of COMMAND -> callback.
callbacks map[string][]Callback
// reader is the socket buffer reader from the IRC server.
reader *Decoder
// reader is the socket buffer write to the IRC server.