Untitled

Anonymous
plain_text
02/11/2026 1:59 PM
3.1 KB
11
Indexable
-- === Macro / sugar helpers ===

-- Short aliases (Get / Free)
function ObjectPool:Get()
    -- alias for :Acquire()
    return (self :: any):Acquire()
end

function ObjectPool:Free(obj: any)
    -- alias for :Release(obj)
    (self :: any):Release(obj)
end

-- High-level spawn helper
-- usage:
-- pool:Spawn(function(obj)
--     -- configure object
-- end, 2) -- optional auto-release delay
function ObjectPool:Spawn(configure: (any) -> (), autoReleaseDelay: number?)
    assert(type(configure) == "function", "ObjectPool:Spawn requires a configure function")

    local obj = (self :: any):Acquire()
    configure(obj)

    if autoReleaseDelay and autoReleaseDelay > 0 then
        task.delay(autoReleaseDelay, function()
            local internal = (self :: any)
            -- safety checks
            if not internal._destroyed and internal._inUse and internal._inUse[obj] then
                (self :: any):Release(obj)
            end
        end)
    end

    return obj
end

-- Instance template macro
-- Tek bir template Instance üzerinden pool kurmak için:
-- local pool = ObjectPool.CreateInstancePool(template, { parent = workspace.Folder, minSize = 50, ... })
function ObjectPool.CreateInstancePool(template: Instance, config: {
    parent: Instance?,
    reset: ((Instance) -> ())?,
    destroy: ((Instance) -> ())?,
    minSize: number?,
    maxSize: number?,
    autoExpand: boolean?,
    growthStep: number?,
}?): ObjectPool<Instance>
    assert(typeof(template) == "Instance", "CreateInstancePool: 'template' must be an Instance")

    config = config or {}
    local parent = config.parent or template.Parent

    -- wrap user reset so parent her zaman doğru olsun
    local userReset = config.reset
    local function wrappedReset(obj: Instance)
        obj.Parent = parent
        if userReset then
            userReset(obj)
        end
    end

    return ObjectPool.new({
        factory = function()
            local clone = template:Clone()
            clone.Parent = parent
            return clone
        end,
        reset = wrappedReset,
        destroy = config.destroy,
        minSize = config.minSize,
        maxSize = config.maxSize,
        autoExpand = config.autoExpand,
        growthStep = config.growthStep,
    })
end
Editor is loading...
Leave a Comment