[LUA] How to do this library works for more than 1 addon at the same time
Hi, I create a new library with the purpose to track some units to check what talents are they using (enemy units), but I have one problem to separate the dates from the diferents addons that use it.
When I started to create it, I believed all addons that use it would retrieve a different "copy" of my addon library, but no, using LibStub only one Library object it's created and used on all addons (this make sense to save resources, but I had not noticed...) My code looks similar to this (example code to show concept): Lua Code:
The problem it's all addons have the same object, so, for example, if addon #1 register some units, and addon #2 register other units, and addon #2 then calls UnregisterAllUnits, this action deletes all the units (including units registered by addon #1 and #2). My intention is if the addon #2 calls UnregisterAllUnits only addon #2 unit's should be removed. Ok, how can I deal with this? For the data variable "libUnitsToTrack" I know that I can do a structure like this: Lua Code:
or like this: Lua Code:
instead of the actual: Lua Code:
But the problem it's how I can easy manage this since the addon part, I don't like add one more argument (the addon name) for all "RegisterUnit" and "UnregisterUnit" functions. I really have many functions to register and unregister units (allowing the addon that use this library register units by unitId, unitName, unitGUID, etc...). I prefer a solution more simplest and more transparent for the user. I like some like this but I don't know how I can do this: Lua Code:
Some way to do this? I don't know if I have explained well :S Thanks for the help! :) TL;DR: I am doing a library. I am looking for some way to use a global library object in diferents addons and I wants the library functions know the addon that which were called. |
There are two ways you can go at this. The first is requiring the addon to provide its name to every function requiring the differentiation. Another is to change your API so every addon has to call a generator function that returns a control object which is used by the library to isolate data for each addon.
|
How I can do the second way? Can you provide me some simple example?
Ty! |
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:
This allows addons using your lib to do this. Lua Code:
|
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:
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! |
TrackerMeta only stores your API functions for the tracker objects. It doesn't store any data at all. When an addon wants to interact with your library, it calls the generator function, :NewTracker(). This returns an empty table used to store the unit data and TrackerMeta attaches its functions to the new table so they can be run on it.
MasterList allows central access and synchronization of all unit data. When an addon registers a unit, if it sees the unit already tracked, it grabs a pointer to the existing unit table. If there isn't one, it's created and stored both in the addon's own table and MasterList. This method also allows the library to update info directly to MasterList and the changes propagate to the individual addon tables as well. A beneficial side effect of this method is it's entirely self-cleaning. If a tracker object gets released to the GarbageCollector, it starts a cascading effect where it acts as if the addon unregistered all of its units. |
All times are GMT -6. The time now is 01:09 AM. |
vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI