Hi, not sure this is the correct place to post, but here goes.
Im wondering if its worth it to make some libraries parametrisized
To explain what i mean by that i've made a small description and some psodo code.
Description:
Take a library that can make GuiWindows.
Lets say the Library would like to supply a way for an addon to close or open all GuiWindows made by that addon.
This can be done in several ways.
Creation and open and close should have an addon parameter, identifying what addon is working with the library.
Use a Factory of Factory scheme with closures
1) Adding an addon parameter
local Lib,minor=LibStub("GuiWindow-1.0",0)
minor=minor or 0
if not lib then return end
local addonSpace={}
setmetatable(addonSpaces,{__mode="v"})
function Lib:Create(addon,x,y,w,h)
if not addonSpace[addon] then addonSpace[addon]={} end
local window
-- TODO: code to create actual window
tinsert(addonSpace[addon],window)
end
function Lib:Hide(addon)
-- code to hide all windows made by addon
for window in ipairs(addonSpace[addon] do
window:Hide()
end
end
function Lib:Show(addon)
-- code to show all windows made by addon
for window in ipairs(addonSpace[addon] do
window:Show()
end
end
Would be used this way
addonName,addonTable=...
local myAddon=LibStub("AceAddon-3.0"):NewAddon(addonName)
local Lib=LibStub:GetLibrary("GuiWindows-1.0",true)
-- creating windows
local aWin1=Lib:Create([B]myAddon[/B],200,200,100,100)
local aWin2=Lib:Create([B]myAddon[/B],200,300,100,100)
local aWin3=Lib:Create([B]myAddon[/B],200,400,100,100)
-- hiding all myAddon windows
Lib:Hide([B]myAddon[/B])
-- showing all myAddon windows
Lib:Show([B]myAddon[/B])
In the library
local Lib,minor=LibStub("GuiWindow-1.0",0)
minor=minor or 0
if not lib then return end
local addonSpace={}
local closures={}
setmetatable(addonSpaces,{__mode="v"})
local function Create(addon,x,y,w,h)
if not addonSpace[addon] then addonSpace[addon]={} end
local window
-- TODO: code to create actual window
tinsert(addonSpace[addon],window)
end
local function Hide(addon)
-- code to hide all windows made by addon
for window in ipairs(addonSpace[addon] do
window:Hide()
end
end
local function Show(addon)
-- code to show all windows made by addon
for window in ipairs(addonSpace[addon] do
window:Show()
end
end
-- Creating table with closures
function Lib:Parametrisize(addon)
if closures[addon] then return closures[addon] end
local closure={
Create=function(x,y,w,h) return Create(addon,x,y,w,h) end,
Hide=function() return Hide(addon) end,
Show=function() return Show(addon) end
}
closures[addon]=closure
return closure
end
Addon using the library
addonName,addonTable=...
local myAddon=LibStub("AceAddon-3.0"):NewAddon(addonName)
local Lib=LibStub:GetLibrary("GuiWindows-1.0",true):Parametrisize(myAddon)
-- Use Lib
-- creating windows
local aWin1=Lib:Create(200,200,100,100)
local aWin2=Lib:Create(200,300,100,100)
local aWin3=Lib:Create(200,400,100,100)
-- hiding all myAddon windows
Lib:Hide()
-- showing all myAddon windows
Lib:Show()
Discussion
Is it worth it?
Note that Parametrisized is just a simple example
It could just as well have been made with multiple arguments
And if it fit the library it could implement more variants of Parametrisized.
An example could be a library that implemented several similar libraries.
Such a library could then supply several functions like Parametrisized where each would supply a different variant of the library.
The overhead of doing it this way is that we get a few extra closure tables (or one for each parametrisized like functions + if per addon is used, like in the example, one for each addon)
Note that it is using closures so the calling overhead is minimal
The benefit is robustness, easier use and that we can supply multiple variants of a library in less code.
Would love to hear what this great coding community thinks, so do post if you have some relevant observations.
If it makes sense to include it, by all means do so.
If you didn't, I would have to create a table in my addon containing pointers to every window owned by my addon and code a function to iterate over it and close every window in the table.
Sometimes it makes sense for library X to have it. Sometimes it doesn't. Many libraries already use your proposed idea. For example AceEvent-3.0 internally keeps track of which addon registered which events (since its a mixin), and when you call :Disable() on that addon, it unregisters all events that addon and only that addon made automatically.
Some libs do this already with self... we usually call it a mixin. You might want to check out stuff like CallbackHandler, you're essentially just renaming and complicating it.
Im wondering if its worth it to make some libraries parametrisized
To explain what i mean by that i've made a small description and some psodo code.
Description:
Take a library that can make GuiWindows.
Lets say the Library would like to supply a way for an addon to close or open all GuiWindows made by that addon.
This can be done in several ways.
1) Adding an addon parameter
Would be used this way
In the library
Addon using the library
Discussion
Is it worth it?
Note that Parametrisized is just a simple example
It could just as well have been made with multiple arguments
And if it fit the library it could implement more variants of Parametrisized.
An example could be a library that implemented several similar libraries.
Such a library could then supply several functions like Parametrisized where each would supply a different variant of the library.
The overhead of doing it this way is that we get a few extra closure tables (or one for each parametrisized like functions + if per addon is used, like in the example, one for each addon)
Note that it is using closures so the calling overhead is minimal
The benefit is robustness, easier use and that we can supply multiple variants of a library in less code.
Would love to hear what this great coding community thinks, so do post if you have some relevant observations.
If you didn't, I would have to create a table in my addon containing pointers to every window owned by my addon and code a function to iterate over it and close every window in the table.
Sometimes it makes sense for library X to have it. Sometimes it doesn't. Many libraries already use your proposed idea. For example AceEvent-3.0 internally keeps track of which addon registered which events (since its a mixin), and when you call :Disable() on that addon, it unregisters all events that addon and only that addon made automatically.
The other half of the idea "Similar libraries in one" can be made by having multiple "constructors" in the library.. so i've been way off track.
Thanks for the input.