Skip to content

Commit

Permalink
Patch: Update old samples and their sources.
Browse files Browse the repository at this point in the history
  • Loading branch information
robloxiandemo committed Apr 6, 2024
1 parent afcdb27 commit 2f9a8a2
Show file tree
Hide file tree
Showing 83 changed files with 1,365 additions and 557 deletions.
109 changes: 109 additions & 0 deletions samples/Luau/EnumList.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
--// Authored by @Sleitnick (https://github.com/sleitnick)
--// Fetched from (https://github.com/Sleitnick/RbxUtil/blob/main/modules/enum-list/init.lua)
--// Licensed under the MIT License (https://github.com/Sleitnick/RbxUtil/blob/main/LICENSE.md)

--!optimize 2
--!strict
--!native

-- EnumList
-- Stephen Leitnick
-- January 08, 2021

type EnumNames = { string }

--[=[
@interface EnumItem
.Name string
.Value number
.EnumType EnumList
@within EnumList
]=]
export type EnumItem = {
Name: string,
Value: number,
EnumType: any,
}

local LIST_KEY = newproxy()
local NAME_KEY = newproxy()

local function CreateEnumItem(name: string, value: number, enum: any): EnumItem
local enumItem = {
Name = name,
Value = value,
EnumType = enum,
}
table.freeze(enumItem)
return enumItem
end

--[=[
@class EnumList
Defines a new Enum.
]=]
local EnumList = {}
EnumList.__index = EnumList

--[=[
@param name string
@param enums {string}
@return EnumList
Constructs a new EnumList.
```lua
local directions = EnumList.new("Directions", {
"Up",
"Down",
"Left",
"Right",
})
local direction = directions.Up
```
]=]
function EnumList.new(name: string, enums: EnumNames)
assert(type(name) == "string", "Name string required")
assert(type(enums) == "table", "Enums table required")
local self = {}
self[LIST_KEY] = {}
self[NAME_KEY] = name
for i, enumName in ipairs(enums) do
assert(type(enumName) == "string", "Enum name must be a string")
local enumItem = CreateEnumItem(enumName, i, self)
self[enumName] = enumItem
table.insert(self[LIST_KEY], enumItem)
end
return table.freeze(setmetatable(self, EnumList))
end

--[=[
@param obj any
@return boolean
Returns `true` if `obj` belongs to the EnumList.
]=]
function EnumList:BelongsTo(obj: any): boolean
return type(obj) == "table" and obj.EnumType == self
end

--[=[
Returns an array of all enum items.
@return {EnumItem}
@since v2.0.0
]=]
function EnumList:GetEnumItems()
return self[LIST_KEY]
end

--[=[
Get the name of the enum.
@return string
@since v2.0.0
]=]
function EnumList:GetName()
return self[NAME_KEY]
end

export type EnumList = typeof(EnumList.new(...))

return EnumList
213 changes: 213 additions & 0 deletions samples/Luau/Maid.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
--// Authored by @Dig1t (https://github.com/dig1t)
--// Fetched from (https://github.com/dig1t/dlib/blob/main/src/Maid.luau)
--// Licensed under the MIT License (https://github.com/dig1t/dlib/blob/main/LICENSE)

--!optimize 2
--!strict
--!native

local Util = require(script.Parent.Util)

type MaidClass = {
__index: MaidClass,
new: () -> MaidType,
task: (self: MaidType, task: any, cleaner: () -> any) -> string,
removeTask: (self: MaidType, taskToRemove: MaidTask) -> nil,
clean: (self: MaidType) -> nil,
destroy: (any) -> nil,

MaidType: MaidType
}

type MaidInstance = {
_tasks: { [string]: MaidTask },
[any]: any
}

export type MaidType = typeof(setmetatable(
{} :: MaidInstance,
{} :: MaidClass
))

type MaidTask = {
Connected: boolean?,
Disconnect: () -> nil?,
Destroy: (any) -> nil?,
destroy: (any) -> nil?,
destructor: (task: any) -> nil?,
[any]: any
}? | () -> nil? | Instance;

--[=[
Task management class for cleaning up things for garbage collection.
@class Maid
]=]
local Maid: MaidClass = {} :: MaidClass
Maid.__index = Maid

--[=[
Creates a new Maid instance.
```lua
local maid = Maid.new()
```
@return Maid
]=]
function Maid.new(): MaidType
local self = setmetatable({}, Maid)

self._tasks = {}

return self
end

--[=[
Adds a task to the Maid instance.
```lua
local maid = Maid.new()
maid:task(function()
print("Hello world!")
end)
```
Multiple types of tasks can be added to the Maid instance.
```lua
local maid = Maid.new()
-- Functions
maid:task(function()
print("Hello world!")
end)
-- RBXScriptConnections
maid:task(workspace.ChildAdded:Connect(function()
print("Hello world!")
end))
-- Instances with "Destroy" methods
maid:task(Instance.new("Part"))
-- Packages with "Destroy", "destroy", or "destructor" methods
local instance = Class.new({
PackageVariable = "Hello world!",
Destroy = function()
-- destroy this package instance
end
})
maid:task(instance)
```
@param _task any -- The task to add.
@return string -- The task id.
]=]
function Maid:task(_task: MaidTask): string
local taskId = Util.randomString(14)

self._tasks[taskId] = _task

return taskId
end

--[=[
Removes a task from the Maid instance.
```lua
local maid = Maid.new()
local taskId = maid:task(function()
print("Hello world!")
end)
maid:removeTask(taskId)
```
@param taskToRemove any -- The task item to remove.
@return nil
]=]
function Maid:removeTask(taskToRemove: string | MaidTask): nil
-- Remove by task id
if typeof(taskToRemove) == "string" then
self._tasks[taskToRemove] = nil

return
end

-- Remove by task
for taskId, _task: MaidTask in pairs(self._tasks) do
if _task == taskToRemove then
self._tasks[taskId] = nil
end
end

return
end

--[=[
Cleans up all tasks in the Maid instance.
```lua
local maid: typeof(Maid.MaidType) = Maid.new()
maid:task(function()
print("Hello world!")
end)
maid:clean() -- Hello world!
```
@return nil
]=]
function Maid:clean(): nil
for taskId, _task: MaidTask in pairs(self._tasks) do
if typeof(_task) == "function" then
_task() -- Run cleaning _task
elseif typeof(_task) == "RBXScriptConnection" and _task.Connected then
_task:Disconnect()
elseif typeof(_task) == "Instance" or (_task and _task.Destroy) then
_task:Destroy()
elseif _task and _task.destroy then
_task:destroy()
elseif typeof(_task) == "table" then
if _task.destructor then
_task.destructor(_task.task)
end
end

self._tasks[taskId] = nil
end

return
end

--[=[
Destroys the Maid instance.
```lua
local maid = Maid.new()
maid:task(function()
print("Hello world!")
end)
maid:destroy()
maid:clean() -- method no longer exists
```
@return nil
]=]
function Maid:destroy(): nil
for key: any, _ in pairs(self) do
self[key] = nil
end

return nil
end

return Maid
Loading

0 comments on commit 2f9a8a2

Please sign in to comment.