Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local CleanService
- do
- --effectively private
- local keys = {}
- local instances = {}
- CleanService = {
- Add = function(self, obj)
- if keys[self] then error("Cannot modify locked instance.") end
- table.insert(self.Garbage, obj)
- end,
- Clean = function(self)
- if keys[self] then error("Cannot clean locked instance.") end
- for _, item in pairs(self.Garbage) do
- pcall(function() item:Remove() end)
- end
- self.Garbage = {}
- end,
- Contains = function(self, obj)
- if keys[self] then error("Cannot search locked instance.") end
- for idx, item in pairs(self.Garbage) do
- if item == obj then
- return true, idx
- end
- end
- return false
- end,
- Remove = function(self, rem)
- if rem ~= nil then
- if keys[self] then error("Cannot modify locked instance.") end
- for idx, item in pairs(self.Garbage) do
- if item == rem then
- table.remove(self.Garbage, idx)
- end
- end
- end
- if keys[self] then error("Cannot remove locked instance.") end
- for idx, item in pairs(CleanServices.Instances) do
- if item == self then
- table.remove(CleanServices.Instances, idx)
- end
- end
- for idx in pairs(self) do
- self[idx] = nil
- end
- end,
- TimedClean = function(self, time)
- if keys[self] then error("Cannot clean locked instance.") end
- Spawn(function()
- wait(time)
- self:Clean()
- end)
- end,
- Lock = function(self)
- if keys[self] then error("Cannot lock locked instance.") end
- keys[self] = {} --All table literals are unique
- return keys[self]
- end,
- Unlock = function(self, key)
- if key ~= keys[self] then error("Incorrect key.") end
- keys[self] = nil
- end,
- CleanAll = function(self)
- for _, inst in pairs(self.Instances) do
- pcall(function() inst:Clean() end)
- end
- end
- }
- CleanService.new = function()
- -- Use metatables, so that CleanService.Add(instance, ...) is the same as instance:Add(...)
- local instance = setmetatable({
- Garbage = {}
- }, {
- __index = CleanService
- });
- table.insert(instances, instance)
- return instance
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment