thoj/go-ircevent — pull request #24 by darkliquid (2014-02-11 23:49:06+00:00)
Title: Callback tweaks

I've added in the ability to do two things:

1. Adding callbacks now returns with a unique(ish) identifier that you can use to remove/replace the callback
2. You can have "wildcard" callbacks that respond to any/every event code

The rationale behind point 1 is that it's really hard to know what callback to replace/remove without having an identifier for the callback and an index in an array doesn't cut it because removing an item in an array naturally changes the indexes of all subsequent callbacks. Unique string ids however allow for referencing the same callback every time. The downside of the method I've used is that Golang doesn't actually have guaranteed unique function pointers - I've added the random number on the end to work around this, but a collision is still feasible, if highly unlikely. This is the part I'm most suspect about, so if you have any better ideas, let me know.

The rationale behind point 2 is this: in my bot, I have a system for dynamically loading/registering callbacks and commands to be processed. Now, rather than having to manage the events via Add/Remove, it's easier for me to maintain my own event dispatcher and just hook that up as a wildcard callback to the main IRC callback dispatcher - this means that when I want to reload/remove/etc all my dynamic callbacks, I don't need to clear them out of the main callback registry (and also then don't have to make sure I don't clobber any existing callbacks I didn't create dynamically) but instead just detach my wildcard callback and clean up my own, separate callback registry as I see fit.