Browse Source

remove global irc event handlers.

master
Gisle Aune 4 years ago
parent
commit
327053703b
  1. 3
      client.go
  2. 8
      client_test.go
  3. 8
      cmd/ircrepl/main.go
  4. 15
      handle.go
  5. 4
      state.go

3
client.go

@ -1224,9 +1224,6 @@ func (client *Client) handleEvent(event *Event) {
clientHandlers := client.handlers clientHandlers := client.handlers
client.mutex.RUnlock() client.mutex.RUnlock()
for _, handler := range globalHandlers {
handler(event, client)
}
for _, handler := range clientHandlers { for _, handler := range clientHandlers {
handler(event, client) handler(event, client)
} }

8
client_test.go

@ -3,18 +3,15 @@ package irc_test
import ( import (
"context" "context"
"errors" "errors"
"github.com/gissleh/irc/handlers"
"testing" "testing"
"github.com/gissleh/irc" "github.com/gissleh/irc"
"github.com/gissleh/irc/handlers"
"github.com/gissleh/irc/internal/irctest" "github.com/gissleh/irc/internal/irctest"
) )
// Integration test below, brace yourself. // Integration test below, brace yourself.
func TestClient(t *testing.T) { func TestClient(t *testing.T) {
irc.AddHandler(handlers.Input)
irc.AddHandler(handlers.MRoleplay)
client := irc.New(context.Background(), irc.Config{ client := irc.New(context.Background(), irc.Config{
Nick: "Test", Nick: "Test",
User: "Tester", User: "Tester",
@ -23,6 +20,9 @@ func TestClient(t *testing.T) {
SendRate: 1000, SendRate: 1000,
}) })
client.AddHandler(handlers.Input)
client.AddHandler(handlers.MRoleplay)
t.Logf("Client.ID = %#+v", client.ID()) t.Logf("Client.ID = %#+v", client.ID())
if client.ID() == "" { if client.ID() == "" {
t.Fail() t.Fail()

8
cmd/ircrepl/main.go

@ -27,9 +27,6 @@ func main() {
flag.Parse() flag.Parse()
irc.AddHandler(handlers.Input)
irc.AddHandler(handlers.MRoleplay)
client := irc.New(ctx, irc.Config{ client := irc.New(ctx, irc.Config{
Nick: *flagNick, Nick: *flagNick,
User: *flagUser, User: *flagUser,
@ -37,13 +34,16 @@ func main() {
Password: *flagPass, Password: *flagPass,
}) })
client.AddHandler(handlers.Input)
client.AddHandler(handlers.MRoleplay)
err := client.Connect(*flagServer, *flagSsl) err := client.Connect(*flagServer, *flagSsl)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "Failed to connect: %s", err) fmt.Fprintf(os.Stderr, "Failed to connect: %s", err)
} }
var target irc.Target var target irc.Target
irc.AddHandler(func(event *irc.Event, client *irc.Client) {
client.AddHandler(func(event *irc.Event, client *irc.Client) {
if event.Name() == "input.target" { if event.Name() == "input.target" {
name := event.Arg(0) name := event.Arg(0)

15
handle.go

@ -3,18 +3,3 @@ package irc
// A Handler is a function that is part of the irc event loop. It will receive all // A Handler is a function that is part of the irc event loop. It will receive all
// events. // events.
type Handler func(event *Event, client *Client) type Handler func(event *Event, client *Client)
var globalHandlers = make([]Handler, 0, 8)
// AddHandler adds a new handler to the irc handling. The handler may be called from multiple threads at the same
// time, so external resources should be locked if there are multiple clients. Adding handlers is not thread
// safe and should be done prior to clients being created. Also, this handler will block the individual
// client's event loop, so long operations that include network requests and the like should be done in a
// goroutine with the needed data **copied** from the handler function.
func AddHandler(handler Handler) {
globalHandlers = append(globalHandlers, handler)
}
func init() {
globalHandlers = make([]Handler, 0, 8)
}

4
state.go

@ -23,3 +23,7 @@ type TargetState struct {
Name string `json:"name"` Name string `json:"name"`
Users []list.User `json:"users,omitempty"` Users []list.User `json:"users,omitempty"`
} }
type EventData struct {
}
Loading…
Cancel
Save