test: add comprehensive ts2lua-unreal TS/Lua 5.4 test suites and upgrade to v0.2.2
This commit is contained in:
1 parent
ee2e62bb34
commit
3dd61c32a9
75 files changed
+9929
-10
No files matched your search
@@ -0,0 +1,61 @@
|
||||
local ____lualib = require("lualib_bundle")
|
||||
local __TS__Class = ____lualib.__TS__Class
|
||||
local ____exports = {}
|
||||
____exports.ObjectPool = __TS__Class()
|
||||
local ObjectPool = ____exports.ObjectPool
|
||||
ObjectPool.name = "ObjectPool"
|
||||
function ObjectPool.prototype.____constructor(self, options)
|
||||
self.idle = {}
|
||||
self.activeCount = 0
|
||||
self.totalCreated = 0
|
||||
self.factory = options.factory
|
||||
self.onAcquire = options.onAcquire
|
||||
self.onRelease = options.onRelease
|
||||
self.maxCapacity = options.maxCapacity or 64
|
||||
end
|
||||
function ObjectPool.prototype.Prewarm(self, count)
|
||||
local target = math.min(count, self.maxCapacity)
|
||||
while #self.idle < target do
|
||||
self.totalCreated = self.totalCreated + 1
|
||||
self.idle[#self.idle + 1] = self.factory()
|
||||
end
|
||||
end
|
||||
function ObjectPool.prototype.Acquire(self)
|
||||
local obj
|
||||
if #self.idle > 0 then
|
||||
obj = table.remove(self.idle)
|
||||
else
|
||||
self.totalCreated = self.totalCreated + 1
|
||||
obj = self.factory()
|
||||
end
|
||||
self.activeCount = self.activeCount + 1
|
||||
if self.onAcquire then
|
||||
self.onAcquire(obj)
|
||||
end
|
||||
return obj
|
||||
end
|
||||
function ObjectPool.prototype.Release(self, obj)
|
||||
if self.activeCount > 0 then
|
||||
self.activeCount = self.activeCount - 1
|
||||
end
|
||||
if self.onRelease then
|
||||
self.onRelease(obj)
|
||||
end
|
||||
if #self.idle < self.maxCapacity then
|
||||
self.idle[#self.idle + 1] = obj
|
||||
end
|
||||
end
|
||||
function ObjectPool.prototype.Clear(self)
|
||||
self.idle = {}
|
||||
self.activeCount = 0
|
||||
end
|
||||
function ObjectPool.prototype.GetIdleCount(self)
|
||||
return #self.idle
|
||||
end
|
||||
function ObjectPool.prototype.GetActiveCount(self)
|
||||
return self.activeCount
|
||||
end
|
||||
function ObjectPool.prototype.GetTotalCreated(self)
|
||||
return self.totalCreated
|
||||
end
|
||||
return ____exports
|
||||
Reference in new issue
Block a user