Eric_W

Better CleanService

Oct 23rd, 2011
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.08 KB | None | 0 0
  1. local CleanService
  2. do
  3.     --effectively private
  4.     local keys = {}
  5.     local instances = {}
  6.    
  7.     CleanService = {
  8.         Add = function(self, obj)
  9.             if keys[self] then error("Cannot modify locked instance.") end
  10.             table.insert(self.Garbage, obj)
  11.         end,
  12.         Clean = function(self)
  13.             if keys[self] then error("Cannot clean locked instance.") end
  14.             for _, item in pairs(self.Garbage) do
  15.                 pcall(function() item:Remove() end)
  16.             end
  17.             self.Garbage = {}
  18.         end,
  19.         Contains = function(self, obj)
  20.             if keys[self] then error("Cannot search locked instance.") end
  21.             for idx, item in pairs(self.Garbage) do
  22.                 if item == obj then
  23.                     return true, idx
  24.                 end
  25.             end
  26.             return false
  27.         end,
  28.         Remove = function(self, rem)
  29.             if rem ~= nil then
  30.                 if keys[self] then error("Cannot modify locked instance.") end
  31.                 for idx, item in pairs(self.Garbage) do
  32.                     if item == rem then
  33.                         table.remove(self.Garbage, idx)
  34.                     end
  35.                 end
  36.             end
  37.             if keys[self] then error("Cannot remove locked instance.") end
  38.             for idx, item in pairs(CleanServices.Instances) do
  39.                 if item == self then
  40.                     table.remove(CleanServices.Instances, idx)
  41.                 end
  42.             end
  43.             for idx in pairs(self) do
  44.                 self[idx] = nil
  45.             end
  46.         end,
  47.         TimedClean = function(self, time)
  48.             if keys[self] then error("Cannot clean locked instance.") end
  49.             Spawn(function()
  50.                 wait(time)
  51.                 self:Clean()
  52.             end)
  53.         end,
  54.         Lock = function(self)
  55.             if keys[self] then error("Cannot lock locked instance.") end
  56.             keys[self] = {} --All table literals are unique
  57.             return keys[self]
  58.         end,
  59.         Unlock = function(self, key)
  60.             if key ~= keys[self] then error("Incorrect key.") end
  61.             keys[self] = nil
  62.         end,
  63.         CleanAll = function(self)
  64.             for _, inst in pairs(self.Instances) do
  65.                 pcall(function() inst:Clean() end)
  66.             end
  67.         end
  68.     }
  69.     CleanService.new = function()
  70.         -- Use metatables, so that CleanService.Add(instance, ...) is the same as instance:Add(...)
  71.         local instance = setmetatable({
  72.             Garbage = {}
  73.         }, {
  74.             __index = CleanService
  75.         });
  76.         table.insert(instances, instance)
  77.        
  78.         return instance
  79.     end
  80. end
  81.  
  82.  
Advertisement
Add Comment
Please, Sign In to add comment