mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-19 16:06:02 +01:00
refactor(class): improve types
This commit is contained in:
@@ -21,17 +21,21 @@ local function assertType(id, var, expected)
|
|||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---@alias OxClassConstructor<T> fun(self: T, ...: unknown): nil
|
||||||
|
|
||||||
---@class OxClass
|
---@class OxClass
|
||||||
|
---@field private __index table
|
||||||
---@field protected __name string
|
---@field protected __name string
|
||||||
---@field protected private table
|
---@field protected private? { [string]: unknown }
|
||||||
---@field protected super function
|
---@field protected super? OxClassConstructor
|
||||||
---@field protected constructor function
|
---@field protected constructor? OxClassConstructor
|
||||||
local mixins = {}
|
local mixins = {}
|
||||||
local constructors = {}
|
local constructors = {}
|
||||||
local getinfo = debug.getinfo
|
local getinfo = debug.getinfo
|
||||||
|
|
||||||
---Somewhat hacky way to remove the constructor from the class.__index.
|
---Somewhat hacky way to remove the constructor from the class.__index.
|
||||||
---Maybe add static fields in the future?
|
---Maybe add static fields in the future?
|
||||||
|
---@param class OxClass
|
||||||
local function getConstructor(class)
|
local function getConstructor(class)
|
||||||
local constructor = constructors[class] or class.constructor
|
local constructor = constructors[class] or class.constructor
|
||||||
|
|
||||||
@@ -47,10 +51,9 @@ local function void() return '' end
|
|||||||
|
|
||||||
---Creates a new instance of the given class.
|
---Creates a new instance of the given class.
|
||||||
---@generic T
|
---@generic T
|
||||||
---@param class T
|
---@param class T | OxClass
|
||||||
---@return T
|
---@return T
|
||||||
function mixins.new(class, ...)
|
function mixins.new(class, ...)
|
||||||
---@cast class +OxClass
|
|
||||||
local constructor = getConstructor(class)
|
local constructor = getConstructor(class)
|
||||||
local obj = {
|
local obj = {
|
||||||
private = {}
|
private = {}
|
||||||
@@ -81,6 +84,7 @@ This behaviour is deprecated and will not be supported in the future.]])
|
|||||||
end
|
end
|
||||||
|
|
||||||
constructor(obj, ...)
|
constructor(obj, ...)
|
||||||
|
---@diagnostic disable-next-line: undefined-field
|
||||||
elseif class.init then
|
elseif class.init then
|
||||||
lib.print.warn(([[Calling %s:init() is deprecated and will not be supported in the future.
|
lib.print.warn(([[Calling %s:init() is deprecated and will not be supported in the future.
|
||||||
Use %s:constructor(...args) and assign properties in the constructor.]])
|
Use %s:constructor(...args) and assign properties in the constructor.]])
|
||||||
@@ -119,22 +123,19 @@ Use %s:constructor(...args) and assign properties in the constructor.]])
|
|||||||
obj.private = nil
|
obj.private = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
---@cast class -OxClass
|
|
||||||
return obj
|
return obj
|
||||||
end
|
end
|
||||||
|
|
||||||
---Checks if an object is an instance of the given class.
|
---Checks if an object is an instance of the given class.
|
||||||
---@param obj table
|
|
||||||
---@param class OxClass
|
---@param class OxClass
|
||||||
function mixins.isClass(obj, class)
|
function mixins:isClass(class)
|
||||||
return getmetatable(obj) == class
|
return getmetatable(self) == class
|
||||||
end
|
end
|
||||||
|
|
||||||
---Checks if an object is an instance or derivative of the given class.
|
---Checks if an object is an instance or derivative of the given class.
|
||||||
---@param obj table
|
|
||||||
---@param class OxClass
|
---@param class OxClass
|
||||||
function mixins.instanceOf(obj, class)
|
function mixins:instanceOf(class)
|
||||||
local mt = getmetatable(obj)
|
local mt = getmetatable(self)
|
||||||
|
|
||||||
while mt do
|
while mt do
|
||||||
if mt == class then return true end
|
if mt == class then return true end
|
||||||
@@ -155,7 +156,6 @@ function lib.class(name, super)
|
|||||||
local class = table.clone(mixins)
|
local class = table.clone(mixins)
|
||||||
|
|
||||||
class.__name = name
|
class.__name = name
|
||||||
---@diagnostic disable-next-line: inject-field
|
|
||||||
class.__index = class
|
class.__index = class
|
||||||
|
|
||||||
if super then
|
if super then
|
||||||
|
|||||||
Reference in New Issue
Block a user