Ohh, that code looks nice... Can I modify it to not use the
TrackerMeta object directly as data store? Can I use the
TrackerMeta to store only the addon name (and the functions) and use this addon name to access a local variables when I wants?
I can't test this now, but I am looking some like this (modified version of your code):
lua Code:
--[[ Library Registration ]]
local LibTalentsTrack=LibStub:NewLibrary("LibTalentsTrack-1.0",1);
if not LibTalentsTrack then return; end
--[[ Internal Variables ]]
local MasterList=setmetatable({},{__mode="v"});-- Generate table with weak values to let GarbageCollection take them as necessary
--[[ Tracker Object Metatable ]]
local TrackerMeta={__index={}};
local libUnitsToTrack = {} --accesed libUnitsToTrack[addonName][guid]
local libUnitsToDoSomething2 = {} --accesed libUnitsToDoSomething2[addonName][guid]
local libUnitsToDoSomething3 = {} --accesed libUnitsToDoSomething3[addonName][guid]
function TrackerMeta.__index:RegisterUnit(unit)
-- self is our tracker object table given to addons
if UnitExists(unit) then
local guid=UnitGUID(unit);-- Save by GUID to guarantee correct tracking
local addonName = self["addonName"]
libUnitsToTrack[addonName][guid] = {0, 0, 0} --my default data
end
end
function TrackerMeta.__index:UnregisterUnit(unit)
-- self is our tracker object table given to addons
if UnitExists(unit) then
local addonName = self["addonName"]
libUnitsToTrack[addonName][UnitGUID(unit)]=nil;-- Erase data
end
end
function TrackerMeta.__index:UnregisterAllUnits(unit)
-- self is our tracker object table given to addons
local addonName = self["addonName"]
table.wipe(libUnitsToTrack[addonName]);-- Erase everything
end
-- Generates new tracker object for addons to use (if exists, return the current tracker for that addon)
function LibTalentsTrack:NewTracker(addonName)
if addonName == nil then return end
local existTracker=MasterList[addonName];
if not existTracker then
local tracker = setmetatable({},TrackerMeta);
tracker["addonName"] = addonName
MasterList[addonName]=tracker["addonName"]
return tracker
end
return existTracker
end
function LibTalentsTrack:DeleteTracker(tracker)
if tracker == nil then return end
local addonName = tracker["addonName"]
table.wipe(tracker)
tracker = nil
MasterList[addonName]=nil
end
--Do stuff without problems with MasterList and GarbageCollector
If this is possible, it's easy for me programming the rest of library and I can use infinity variables to store info (with your code my data it's limited to the TrackerMeta object). I could manage complicated think like
MasterList only on
NewTracker and
DeleteTracker functions and forget about this in the rest of the code. On every function that I needs differentiation I only should use
local addonName = self["addonName"] and no more complications by metatables and GarbageCollector. It's this possible?
Forgive me if I pretend do some in wrong way, but the use of metatables and metafunctions are difficult to undestand for me :S
And one more question... what it's the purpose for the
MasterList? I know it's for GarbageCollection, but, I don't know why it's needed...
Seriously, thank you very much, your help is helping me a lot!