What can i add/improve in my custom Maid module

Soo question is in title, here is code:

local Vaccumulator = {}
Vaccumulator.__index = Vaccumulator

function Vaccumulator.new(IsOneTimeUse: boolean): {}
	local self = setmetatable({}, Vaccumulator)
	
	self.List = {}
	self.IsOneTimeUse = IsOneTimeUse
	self.HighestIndex = 0
	
	return self
end

----[[PRIVATE]]----
local function ClearTable(Table: {})
	setmetatable(Table, nil)
	for _, object in Table do
		if typeof(object) == "table" then ClearTable(object); Table[_] = nil; continue end
		if typeof(object) == "thread" then task.cancel(object); continue end
		if typeof(object) == "RBXScriptConnection" then object:Disconnect(); continue end
		if typeof(object) == "Instance" then object:Destroy(); continue end
		
		Table[_] = nil
	end
end

----[[PUBLIC]]----

-- add new class to a CleanUp list
function Vaccumulator:Add(Class: {}, mode: string): number
	table.insert(self.List, Class)
	self.HighestIndex += 1
	
	return self.HighestIndex
end
-- remove specific class from CleanUp list
function Vaccumulator:Remove(index: number)
	table.clear(self.List[index])
	table.remove(self.List, index)
	self.HighestIndex -= 1
end
-- clear specific class from CleanUp list
function Vaccumulator:ClearClass(index: number, Mode: (any) -> (), ...)
	local ClassToClear = self.List[index]	
	if Mode then 
		Mode(ClassToClear, ...) --/ custom clean up function
	else
		ClearTable(ClassToClear)
	end
	
	self:Remove(index)
end
-- clear all classes from CleanUp list
function Vaccumulator:ClearAll()
	for _ = 1, self.HighestIndex do
		self:ClearClass(self.HighestIndex)
	end
end
-- destroy vaccumulator
function Vaccumulator:Destroy(CleanupBeforeDestroying: boolean)
	if CleanupBeforeDestroying then self:ClearAll() end
	
	self.List = nil
	self.IsOneTimeUse = nil
	self.HighestIndex = nil
	
	setmetatable(self, nil)
	table.freeze(self)
end
return Vaccumulator

Note: I wanted to make something myself that will help me, i really don’t like using public modules as i fell they can’t do what i want

1 Like

I noticed your class metatable lacks Type-checking. You can fix it this way:

type Prop = { -- The properties of your class
	List: {[number]: any},
	IsOneTimeUse: boolean,
	HighestIndex: number,	
}

local Vaccumulator = {}
Vaccumulator.__index = Vaccumulator

-- Class type: combines Prop and Vaccumulator together like your metatable
type Vaccumulator = typeof(setmetatable({} :: Prop, {} :: typeof(Vaccumulator)))
function Vaccumulator.new(IsOneTimeUse: boolean): Vaccumulator
	local self = setmetatable({
		List = {},
		IsOneTimeUse = IsOneTimeUse,
		HighestIndex = 0,
		
	}, Vaccumulator)
	return self
end

function Vaccumulator.Destroy(self: Vaccumulator, CleanupBeforeDestroying: boolean)
	if CleanupBeforeDestroying then self:ClearAll() end
	
	setmetatable(self :: any, nil)
	table.clear(self :: never)
	table.freeze(self)
end

return Vaccumulator

Prop is the class unique table values.
typeof(Vaccumulator) mimics your module dictionary.
typeof(setmetatable) mimics your class meta table.

:: assumes a set value is the specified type.
luau type-checking fails to recognize a class type as a table or metatable, which creates false flags in the script, these do nothing and can be silenced with :: any.

You can still call the functions with a semi-colon, which will skip the self parameter like usual.

You probably also do:

type Vac = typeof(Vaccumulator.new(true)) -- value shouldn't matter

as the class type, which would skip the prop type entirely, but i haven’t tested it much.

A step above would be implementing an **Impl type ** that mimics your module dict, which essentially implements your functions type checking outside the functions . This is better explained in the Account section in luau type-checking. (Link further down below)

Would heavily advice you to read everything inside luau type-checking because it is an incredibly useful resource:

https://luau.org/typecheck

There’s also some really useful special comments that may or may not help your current script much. I assume you know most of them from looking at your read time so i will just quickly list my favorites just in case.

–!strict
–!native
–!optimize 2
–!optimize 1 (not much documentation on this one so be careful)

1 Like

Soo i should add custom type for my class right?

EDIT: Can i create module that export types soo i can store them in one place?

yes, the link i sent will explain it 100 times better than i did

1 Like

Everything else is OK? or i should use ClearTable function in a module, because entire class is only to make removing many objects at once easier

You can export with export type

With that you can store all of them in one modulescript and have your other modulescripts require them, very useful for avoiding cyclic module dependencies

1 Like

I have not really looked through your code logic much. And i do not have sufficient experience in garbage collection to give you a completely trustworthy answer regarding that.

1 Like

anyways thx for your help, at least code will look better

1 Like