Thread Tools Display Modes
06-04-16, 04:10 AM   #1
zork
A Pyroguard Emberseer
 
zork's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 1,740
RegisterCallback

I just wrote myself a function that triggers a callback on any given event. On top of that you have access to two sets of arguments. One which you define on init. One which is passed along on event.

Benefit is that you only need one event frame for any future event callback.

Lua Code:
  1. --rLib:RegisterCallback
  2. function rLib:RegisterCallback(event, callback, ...)
  3.   if not self.eventFrame then
  4.     self.eventFrame = CreateFrame("Frame")
  5.     function self.eventFrame:OnEvent(event, ...)
  6.       for callback, args in next, self.callbacks[event] do
  7.         callback(args, ...)
  8.       end
  9.     end
  10.     self.eventFrame:SetScript("OnEvent", self.eventFrame.OnEvent)
  11.   end
  12.   if not self.eventFrame.callbacks then self.eventFrame.callbacks = {} end
  13.   if not self.eventFrame.callbacks[event] then self.eventFrame.callbacks[event] = {} end
  14.   self.eventFrame.callbacks[event][callback] = {...}
  15.   self.eventFrame:RegisterEvent(event)
  16. end
  17.  
  18. --init
  19. local function OnLogin(...)
  20.   print(...)
  21. end
  22. rLib:RegisterCallback("PLAYER_LOGIN",OnLogin,"hello1","hello2")

Arguments you pass along on init are the first argument in the triggered callback packed as a table.

Any thoughts?
__________________
| Simple is beautiful.
| WoWI AddOns | GitHub | Zork (WoW)

"I wonder what the non-pathetic people are doing tonight?" - Rajesh Koothrappali (The Big Bang Theory)
 
06-06-16, 01:47 PM   #2
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,325
It's a bit outdated, but Spydon uses one of my first implementations of an event library. More recently, I've been trying to weigh the impact between the overhead of events firing being redirected to a table of callbacks versus individual frames that are created for their own purposes responding to their own unique list of events. In all, it's one thing if you have code that has no frames responding to events versus code that already has frames tying into existing resources.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)

Last edited by SDPhantom : 06-06-16 at 02:13 PM.
 
 

WoWInterface » Site Forums » Archived Beta Forums » Legion Beta archived threads » RegisterCallback


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