Created
May 10, 2023 01:12
-
-
Save MrAsynchronous/5630bbb96c7e67449249903cd36123bf to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| --[=[ | |
| @class DataPages | |
| ]=] | |
| local Import = require(script.Parent.loader).load(script) | |
| local BaseObject = Import("BaseObject") | |
| local Types = Import("Types") | |
| local DataPages = setmetatable({}, BaseObject) | |
| DataPages.ClassName = "DataPages" | |
| DataPages.__index = DataPages | |
| export type PagedData = { | |
| Cursor: number, | |
| Data: any | |
| } | |
| function DataPages.new() | |
| local self = setmetatable(BaseObject.new(), DataPages) | |
| self._currentPageNumber = 0 | |
| self._cursor = nil :: number | |
| self._cursors = {} :: {number} | |
| self._data = {} :: {any} | |
| return self | |
| end | |
| --[=[ | |
| Returns the current page of data | |
| ]=] | |
| function DataPages:GetCurrentPage() | |
| return self._data[self._cursors[self._currentPageNumber]] | |
| end | |
| --[=[ | |
| Calls the callback to fetch the next page of data | |
| ]=] | |
| function DataPages:GetNextPage() | |
| return self._newPageCallback(self._cursor):andThen(function(pagedData: PagedData) | |
| self._data[pagedData.Cursor] = pagedData.Data | |
| self._currentPageNumber += 1 | |
| table.insert(self._cursors, pagedData.Cursor) | |
| return pagedData.Data | |
| end) | |
| end | |
| --[=[ | |
| Returns a specific page in the data pages. | |
| ]=] | |
| function DataPages:GetPage(pageNumber: number) | |
| return self._data[self._cursors[pageNumber]] | |
| end | |
| --[=[ | |
| Set's the new page callback, and calls it to populate page with first page. | |
| ]=] | |
| function DataPages:SetNewPageCallback(callback: (number) -> Types.Promise <PagedData>) | |
| self._newPageCallback = callback | |
| end | |
| return DataPages |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment