Mirror of github.com/gissleh/irc
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

28 lines
996 B

7 years ago
7 years ago
7 years ago
  1. package irc
  2. // A Handler is a function that is part of the irc event loop. It will receive all
  3. // events.
  4. type Handler func(event *Event, client *Client)
  5. var eventHandler struct {
  6. handlers []Handler
  7. }
  8. func emit(event *Event, client *Client) {
  9. for _, handler := range eventHandler.handlers {
  10. handler(event, client)
  11. }
  12. }
  13. // Handle adds a new handler to the irc handling. The handler may be called from multiple threads at the same
  14. // time, so external resources should be locked if there are multiple clients. Adding handlers is not thread
  15. // safe and should be done prior to clients being created.Handle. Also, this handler will block the individual
  16. // client's event loop, so long operations that include network requests and the like should be done in a
  17. // goroutine with the needed data **copied** from the handler function.
  18. func Handle(handler Handler) {
  19. eventHandler.handlers = append(eventHandler.handlers, handler)
  20. }
  21. func init() {
  22. eventHandler.handlers = make([]Handler, 0, 8)
  23. }