Skip to main content

TaskQueue

A queue that flushes all objects at the end of the current execution step. This works by scheduling all tasks with task.defer.

A possible use-case is to batch all requests being sent through a RemoteEvent to help prevent calling it too many times on the same frame.

local bulletQueue = TaskQueue.new(function(bullets)
	bulletRemoteEvent:FireAllClients(bullets)
end)

-- Add 3 bullets. Because they're all added on the same
-- execution step, they will all be grouped together on
-- the next queue flush, which the above function will
-- handle.
bulletQueue:Add(someBullet)
bulletQueue:Add(someBullet)
bulletQueue:Add(someBullet)

Functions

new

TaskQueue.new(onFlush({T}) → ()) → TaskQueue<T>

Constructs a new TaskQueue.

Add

TaskQueue:Add(objectT) → ()

Add an object to the queue.

Clear

TaskQueue:Clear() → ()

Clears the TaskQueue. This will clear any tasks that were scheduled to be flushed on the current execution frame.

queue:Add(something1)
queue:Add(something2)
queue:Clear()

Destroy

TaskQueue:Destroy() → ()

Destroys the TaskQueue. Just an alias for Clear().

Show raw api
{
    "functions": [
        {
            "name": "new",
            "desc": "Constructs a new TaskQueue.",
            "params": [
                {
                    "name": "onFlush",
                    "desc": "",
                    "lua_type": "({T}) -> ()"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "TaskQueue<T>"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 37,
                "path": "modules/task-queue/init.luau"
            }
        },
        {
            "name": "Add",
            "desc": "Add an object to the queue.",
            "params": [
                {
                    "name": "object",
                    "desc": "",
                    "lua_type": "T"
                }
            ],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 52,
                "path": "modules/task-queue/init.luau"
            }
        },
        {
            "name": "Clear",
            "desc": "Clears the TaskQueue. This will clear any tasks\nthat were scheduled to be flushed on the current\nexecution frame.\n\n```lua\nqueue:Add(something1)\nqueue:Add(something2)\nqueue:Clear()\n```",
            "params": [],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 77,
                "path": "modules/task-queue/init.luau"
            }
        },
        {
            "name": "Destroy",
            "desc": "Destroys the TaskQueue. Just an alias for `Clear()`.",
            "params": [],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 93,
                "path": "modules/task-queue/init.luau"
            }
        }
    ],
    "properties": [],
    "types": [],
    "name": "TaskQueue",
    "desc": "A queue that flushes all objects at the end of the current\nexecution step. This works by scheduling all tasks with\n`task.defer`.\n\nA possible use-case is to batch all requests being sent through\na RemoteEvent to help prevent calling it too many times on\nthe same frame.\n\n```lua\nlocal bulletQueue = TaskQueue.new(function(bullets)\n\tbulletRemoteEvent:FireAllClients(bullets)\nend)\n\n-- Add 3 bullets. Because they're all added on the same\n-- execution step, they will all be grouped together on\n-- the next queue flush, which the above function will\n-- handle.\nbulletQueue:Add(someBullet)\nbulletQueue:Add(someBullet)\nbulletQueue:Add(someBullet)\n```",
    "source": {
        "line": 29,
        "path": "modules/task-queue/init.luau"
    }
}