mirror of
https://github.com/littensy/charm.git
synced 2026-10-21 11:44:08 +00:00
feat: initial commit
This commit is contained in:
commit
18723c1377
50 changed files with 3238 additions and 0 deletions
31
src/utils/reference.luau
Normal file
31
src/utils/reference.luau
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
local refs = {}
|
||||
|
||||
--- Prevents the garbage collector from collecting a value
|
||||
local function hold(value: unknown)
|
||||
refs[value] = true
|
||||
end
|
||||
|
||||
--- Allows the garbage collector to collect a value
|
||||
local function release(value: unknown)
|
||||
refs[value] = nil
|
||||
end
|
||||
|
||||
--- Clears all held references
|
||||
local function clear()
|
||||
table.clear(refs)
|
||||
end
|
||||
|
||||
--- Returns a function that retrieves the value through a weak reference
|
||||
local function weak<T>(value: T): () -> T?
|
||||
local set = setmetatable({ value }, { __mode = "v" })
|
||||
return function()
|
||||
return set[1]
|
||||
end
|
||||
end
|
||||
|
||||
return {
|
||||
hold = hold,
|
||||
release = release,
|
||||
clear = clear,
|
||||
weak = weak,
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue