Ive always been against using stuff such as open source modules. Ive always been a guy that wants to make my own modules from scratch. Soo today i decided i wanted to give a shot at creating my own Maid module. My only concern is if there is anything that could be improved or cause any sort of memory leak??
Should also note that this Maid was coded to only support RBXScriptConnections as you can see from the assert(). Nothing else.
Heres my Maid code currently (:
local Maid = {}
Maid.__index = Maid
function Maid.new()
return setmetatable({},Maid)
end
local function GetNumCons(Table) -- // Cons Short for Connections
local Num = 0
for _, v in pairs(Table) do
Num = Num + 1
end
return Num
end
function Maid:GiveTask(Func)
assert(typeof(Func) == "RBXScriptConnection","Argument isnt an RBXScriptConnection. Maid only accepts connections!")
local NumConnections = GetNumCons(self)
local Key = "Task_"..tostring(NumConnections)
self[Key] = Func
return Key -- // Key used when disconnecting/cleaning a task
end
function Maid:ClearTask(TaskKey)
if self[TaskKey] then
local Task = self[TaskKey]
Task:Disconnect()
Task = nil
else
warn("No task with the key " ..TaskKey.." has been created before")
return
end
end
function Maid:ClearAllTasks()
for i, v in pairs(self) do
v:Disconnect()
end
table.clear(self)
end
return Maid