Created
July 24, 2022 18:33
-
-
Save howmanysmall/8c7c47a8bc2e5446b6657890ddf26653 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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