From 8c67a59a85a3f9fe750fdffd44f5f7c4db1235b5 Mon Sep 17 00:00:00 2001 From: Stephen Leitnick Date: Tue, 17 Oct 2023 12:00:10 -0400 Subject: [PATCH] TaskQueue types --- modules/task-queue/index.d.ts | 27 +++++++++++++++++++++++++++ modules/task-queue/init.lua | 28 +++++++++++++++------------- modules/task-queue/wally.toml | 2 +- 3 files changed, 43 insertions(+), 14 deletions(-) create mode 100644 modules/task-queue/index.d.ts diff --git a/modules/task-queue/index.d.ts b/modules/task-queue/index.d.ts new file mode 100644 index 00000000..523b70c0 --- /dev/null +++ b/modules/task-queue/index.d.ts @@ -0,0 +1,27 @@ +declare namespace TaskQueue { + interface Constructor { + new (onFlush: (items: T[]) => void): TaskQueue; + } +} + +interface TaskQueue { + /** + * Add an item to the queue. + */ + Add(item: T): void; + + /** + * Clears items in the queue (except for items currently being flushed, + * which would only occur if the flushing function yielded). + */ + Clear(): void; + + /** + * Alias for `Clear()`. + */ + Destroy(): void; +} + +declare const TaskQueue: TaskQueue.Constructor; + +export = TaskQueue; diff --git a/modules/task-queue/init.lua b/modules/task-queue/init.lua index 9b1d4f75..bc6953bd 100644 --- a/modules/task-queue/init.lua +++ b/modules/task-queue/init.lua @@ -1,5 +1,3 @@ ---!strict - -- TaskQueue -- Stephen Leitnick -- November 20, 2021 @@ -32,16 +30,18 @@ local TaskQueue = {} TaskQueue.__index = TaskQueue --[=[ - @param onFlush ({T}) -> nil + @param onFlush ({T}) -> () @return TaskQueue Constructs a new TaskQueue. ]=] -function TaskQueue.new(onFlush: ({ T }) -> nil) +function TaskQueue.new(onFlush: ({ T }) -> ()) local self = setmetatable({}, TaskQueue) + self._queue = {} self._flushing = false - self._flushingScheduled = false + self._scheduled = nil self._onFlush = onFlush + return self end @@ -51,17 +51,14 @@ end ]=] function TaskQueue:Add(object: T) table.insert(self._queue, object) - if not self._flushingScheduled then - self._flushingScheduled = true - task.defer(function() - if not self._flushingScheduled then - return - end + + if self._scheduled == nil then + self._scheduled = task.defer(function() self._flushing = true self._onFlush(self._queue) table.clear(self._queue) self._flushing = false - self._flushingScheduled = false + self._scheduled = nil end) end end @@ -81,8 +78,13 @@ function TaskQueue:Clear() if self._flushing then return end + + if self._scheduled ~= nil then + task.cancel(self._scheduled) + self._scheduled = nil + end + table.clear(self._queue) - self._flushingScheduled = false end --[=[ diff --git a/modules/task-queue/wally.toml b/modules/task-queue/wally.toml index 7de654b6..01c1a66a 100644 --- a/modules/task-queue/wally.toml +++ b/modules/task-queue/wally.toml @@ -1,7 +1,7 @@ [package] name = "sleitnick/task-queue" description = "Batches tasks that occur on the same execution step" -version = "0.1.1" +version = "1.0.0" license = "MIT" authors = ["Stephen Leitnick"] registry = "https://github.com/UpliftGames/wally-index"