2026-11-30 02:42:51 +08:00
|
|
|
local ____exports = __module.env
|
|
|
|
|
____exports.ObjectPool = __TS__DefineClass("ObjectPool", __module)
|
2026-11-29 03:35:49 +08:00
|
|
|
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
|
2026-11-30 02:42:51 +08:00
|
|
|
function __module.UpdateImports()
|
|
|
|
|
end
|