WoWInterface - View Single Post - [LUA] How to do this library works for more than 1 addon at the same time
View Single Post
12-15-16, 04:55 PM   #4
A Pyroguard Emberseer
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,085
Here's an example of data isolation by generator function. The use of metatables can be tricky to comprehend, but it's easier on the system than scanning each addon's unit table. It uses a trick in which __mode in a metatable can control whether or not the GarbageCollector can take keys and/or values out of a table when it runs. When it does take either from a table, it removes the entire entry.

This actually uses two metatables. MasterList uses it as described above. The other is to apply our API functions to the individual tables we return to each addon in our generator function.
Lua Code:
  1. --[[    Library Registration    ]]
  2. local LibTalentsTrack=LibStub:NewLibrary("LibTalentsTrack-1.0",1);
  3. if not LibTalentsTrack then return; end
  5. --[[    Internal Variables  ]]
  6. local MasterList=setmetatable({},{__mode="v"});--   Generate table with weak values to let GarbageCollection take them as necessary
  8. --[[    Tracker Object Metatable    ]]
  9. local TrackerMeta={__index={}};
  11. function TrackerMeta.__index:RegisterUnit(unit)
  12. --  self is our tracker object table given to addons
  13.     if UnitExists(unit) then
  14.         local guid=UnitGUID(unit);--    Save by GUID to guarantee correct tracking
  16.         local unitdata=MasterList[guid];
  17.         if not unitdata then--  Is new unit?
  18.             unitdata={Name=UnitName(unit)};--   Populate this with default data
  19.             MasterList[guid]=userdata;--    Register table with our master list
  20.         end
  21.         self[guid]=unitdata;--  Save unit in our own list
  22.     end
  23. end
  25. function TrackerMeta.__index:UnregisterUnit(unit)
  26. --  self is our tracker object table given to addons
  27.     if UnitExists(unit) then
  28.         self[UnitGUID(unit)]=nil;-- Erase our copy of the unit table (MasterList will lose its copy if this was the last reference to it next GarbageCollection cycle)
  29.     end
  30. end
  32. function TrackerMeta.__index:UnregisterAllUnits(unit)
  33. --  self is our tracker object table given to addons
  34.     table.wipe(self);-- Erase everything (metatable stays intact)
  35. end
  37. --  Generates new tracker object for addons to use
  38. function LibTalentsTrack:NewTracker()
  39.     return setmetatable({},TrackerMeta);
  40. end
  42. --[[    Tracker Stuff   ]]
  44. --[[
  45.     Do all your CLEU stuff and store all your data in the unit table at MasterList[GUID]
  46.     Be sure to check its existence every CLEU event as the GarbageCollector will randomly take it away after all other references to it are unregistered
  47. ]]

This allows addons using your lib to do this.
Lua Code:
  1. --  Grab a new tracker object from our lib
  2. local UnitTracker=LibStub:GetLibrary("LibTalentsTrack-1.0"):NewTracker();
  4. local EventFrame=CreateFrame("Frame");
  5. EventFrame:RegisterEvent("PLAYER_TARGET_CHANGED");
  6. EventFrame:SetScript("OnEvent",function(self,event,...)
  7.     UnitTracker:RegisterUnit("target");--   Register target for tracking
  8. end);
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 : 12-15-16 at 05:37 PM.
  Reply With Quote