Skip to content

Instantly share code, notes, and snippets.

@howmanysmall
Created July 24, 2022 18:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save howmanysmall/8c7c47a8bc2e5446b6657890ddf26653 to your computer and use it in GitHub Desktop.
Save howmanysmall/8c7c47a8bc2e5446b6657890ddf26653 to your computer and use it in GitHub Desktop.
local BetterPages = {}
function BetterPages.new(Pages: Pages)
return setmetatable({Pages = Pages}, BetterPages)
end
function BetterPages.FromPcall(Success: boolean, ValueOrError: Pages | string)
if Success then
return true, setmetatable({Pages = ValueOrError}, BetterPages)
else
return false, ValueOrError
end
end
function BetterPages:__index(Index)
local Value = BetterPages[Index]
if Value then
return Value
end
Value = self.Pages[Index]
if type(Value) == "function" then
return function(_, ...)
return Value(self.Pages, ...)
end
else
return Value
end
end
local function AdvanceToNextPageAsync(Pages: Pages)
Pages:AdvanceToNextPageAsync()
end
function BetterPages:__iter()
return coroutine.wrap(function()
local PageNumber = 1
while true do
for Index, Value in self:GetCurrentPage() do
coroutine.yield(Value, PageNumber, Index)
end
if self.IsFinished then
break
end
local Success, Error = pcall(AdvanceToNextPageAsync, self)
if not Success then
warn(string.format("Error calling AdvanceToNextPageAsync:\n\tError: %s", tostring(Error)))
break
end
PageNumber += 1
end
end)
end
function BetterPages:__tostring()
return self.ClassName
end
table.freeze(BetterPages)
return BetterPages
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment