Thread Tools Display Modes
08-02-16, 03:18 AM   #1
Layback_
An Onyxian Warder
Join Date: Feb 2016
Posts: 358
About CallbackHandler

Hi all,

I'm not sure whether this question is okay to be asked at this forum, so if I'm violating rules, please let me know.

So, I have been studying about addons for about a week and noticed that many of (probably most of) the addons utilizes a library called 'CallbackHandler'.

According to CallbackHandler overview page, it says...

CallbackHandler is a back-end utility library that makes it easy for a library to fire its events to interested parties. It removes the need for addons to be aware of e.g. AceEvent.

The one remaining use for AceEvent Messages is messages that do not have a fixed source - ones that multiple libraries or addons can fire.
With my lack of understanding, I still don't get how this works and what it actually does.

Could anyone please provide me some brief explanation with an example?

Thank you.
  Reply With Quote
08-02-16, 04:28 AM   #2
p3lim
A Pyroguard Emberseer
 
p3lim's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2007
Posts: 1,710
In short, fake events.

When a library does something and the addon using the library wants to know when that finished or had results, it can assign a callback function using CallbackHandler (if the library has support for it).

For example, the library LibResInfo will fire callbacks whenever it thinks that a resurrect is in progress or has finished in some way, and addons can use CallbackHandler to assign a function to respond to such an event.

More information: https://en.wikipedia.org/wiki/Callba...programming%29
  Reply With Quote
08-02-16, 08:23 AM   #3
Layback_
An Onyxian Warder
Join Date: Feb 2016
Posts: 358
Originally Posted by p3lim View Post
In short, fake events.

When a library does something and the addon using the library wants to know when that finished or had results, it can assign a callback function using CallbackHandler (if the library has support for it).

For example, the library LibResInfo will fire callbacks whenever it thinks that a resurrect is in progress or has finished in some way, and addons can use CallbackHandler to assign a function to respond to such an event.

More information: https://en.wikipedia.org/wiki/Callba...programming%29
Hi p3lim,

So, is it like a particular function can accept a callback function as one of its parameter (or argument) and this callback function can be triggered(?) based on which event has been fired?
  Reply With Quote
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
08-02-16, 11:56 AM   #5
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
Even addons use it, not just libraries. Especially addons with a plug-in system. You can look at my sStats addon, too, if you wish.
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
08-03-16, 06:11 AM   #6
Layback_
An Onyxian Warder
Join Date: Feb 2016
Posts: 358
Thank you guys!

I honestly didn't get 100%, but I got the concept of it.

Should do some practice by myself and see how I go
  Reply With Quote
08-03-16, 07:29 AM   #7
Lombra
A Molten Giant
 
Lombra's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 554
You can also think of it as a simple function call, but instead of calling a specific function, it calls all functions that are named appropriately.

Fire("doStuff") will call all functions that you registered using RegisterCallback("doStuff").
__________________
Grab your sword and fight the Horde!
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » About CallbackHandler


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off