View Single Post
12-30-14, 11:50 AM   #15
ravagernl
Proceritate Corporis
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 1,176
Originally Posted by Phanx View Post
If I'm writing a simple "delay for ADDON_LOADED or PLAYER_LOGIN" frame that only handles that one event by design, I would never add an event check, since it's blatantly obvious that the frame is only handling one event and will only ever handle one event. Adding an event check in this type of code just wastes (imaginary) CPU time and adds useless clutter. I'd argue that the clutter aspect actually makes your code harder to maintain, since when you come back and look at it and see an event check, you assume it must be able to handle other events, and then are confused as to why the hell you included an event check on a frame that only handles one event.
^This The following has somewhat been my personal boilerplate for several years (inspired by several genious people, AFAIK that includes tekkub, haste and p3lim, among others...)

lua Code:
  1. -- events.lua
  2. local addon_name, addon = ...
  3. local event_frame = CreateFrame("Frame")
  4. event_frame:SetScript("onevent", function(self, event, ...)
  5.     if addon[event] then
  6.         addon[event](addon, ...)
  7.     else
  8.         error(addon_name, "unhandled event:", event, ...)
  9.     end
  10. end)
  11. function addon:RegisterEvent(event, function)
  12.     event_frame:RegisterEvent(event)
  13.     if function and not addon[event] then
  14.         addon[event] = function
  15.     -- elseif function then
  16.         -- error(addon_name, "attempt to register handler: handler already exists for event ", event)
  17.     end
  18. end
  19. function addon:RegisterEvents(function, event, ...)
  20.     if event then
  21.         self:RegisterEvent(event, function)
  22.         self:RegisterEvents(function, ...)
  23.     end
  24. end
  25.  
  26. function addon:UnregisterEvent(event, ...)
  27.     if event then
  28.         event_frame:UnregisterEvent(event)
  29.         self:UnregisterEvent(...)
  30.     end
  31. end
  32. addon.UnregisterEvents = addon.UnregisterEvent
  33.  
  34. function addon:KillEvents(event, ...)
  35.      self:UnregisterEvents(event, ...)
  36.      if event then
  37.           self[event] = nil
  38.           self:KillEvents(...)
  39.      end
  40. end
  41. addon.KillEvent = addon.KillEvent
lua Code:
  1. -- core.lua
  2.  
  3. local addon_name, addon = ...
  4.  
  5. function addon:PLAYER_LOGIN()
  6.     -- on login (saved variables loaded + spellbook ready, etc...)
  7.  
  8.     self:KillEvent("PLAYER_LOGIN")
  9. end
  10.  
  11. addon:RegisterEvent("ADDON_LOADED", function(loaded_addon)
  12.     if addon_name ~= loaded_addon then return end
  13.     self:KillEvent("ADDON_LOADED")
  14.     -- on initialize (saved variables loaded)
  15.  
  16.     if IsLoggedIn() then
  17.         self:PLAYER_LOGIN()
  18.     else
  19.         self:RegisterEvent("PAYER_LOGIN")
  20.     end
  21. end)
I'm not saying you should or should always use this method, as it probably contains errors and it has both disadvantages and advantages, if you have a lot of events to register for, then for readability, you could use this method.

Last edited by ravagernl : 12-30-14 at 12:36 PM. Reason: Oh Brain you... (bonk bonk bonk)
  Reply With Quote