View Single Post
08-02-16, 10:49 AM   #4
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
It works almost exactly like the built-in RegisterEvent/OnEvent system:

lua Code:
  1. local MyFrame = CreateFrame("Frame")
  2.  
  3. MyFrame:RegisterEvent("SOME_EVENT")
  4.  
  5. MyFrame:SetScript("OnEvent", function(self, event, arg1, arg2)
  6.     print(event .. " fired with arguments " .. arg1 .. " and " .. arg2)
  7. end)

versus:

lua Code:
  1. local MyFrame = CreateFrame("Frame")
  2.  
  3. local LibSomething = LibStub("LibSomething-1.0")
  4. LibSomething.RegisterCallback(MyFrame, "SomethingHappened")
  5.  
  6. function MyAddon:SomethingHappened(event, arg1, arg2)
  7.     print(event .. " fired with arguments " .. arg1 .. " and " ... arg2)
  8. end

If you're asking how to use CallbackHandler directly, eg. in a library:

lua Code:
  1. local LibSomething = LibStub:NewLibrary("LibSomething-1.0", 1)
  2.  
  3. -- Set up the callback system:
  4. local CallbackHandler = LibStub("CallbackHandler-1.0")
  5. LibSomething.callbacks = CallbackHandler:New(LibSomething)
  6.  
  7. -- Fire an event named "SomethingHappened" with arguments 52 and "lolcats":
  8. LibSomething.callbacks:Fire("SomethingHappened", 52, "lolcats")

If you want more details about how it works internally, just read the code for CallbackHandler-1.0 and/or any library that uses it. It's pretty straightforward.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote