Created
November 6, 2019 08:12
-
-
Save perymimon/12eebc6b5e8be73ca5cb6c4aefa53a97 to your computer and use it in GitHub Desktop.
dispenser too bring async data
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
function Dispenser(filter, sort, requestSize = 25, pageSize) { | |
let pages = 0; | |
let rowCount = 0; | |
let contentsPool = []; | |
let items = []; | |
Dispenser.rowCounts = _ => rowCount; | |
Dispenser.getItem = id => items.find(item => item.dbId == id); | |
return async function dispenses() { | |
while (pageSize > contentsPool.length) { | |
var result = await api.getAll(filter, sort, page++, requestSize); | |
rowCount = result.data.rowCount; | |
const newRows = result.data.rows; | |
contentsPool.splice(Infinity, 0, ...newRows); | |
items.splice(Infinity, 0, ...newRows); | |
if (page * requestSize >= rowCount) break; | |
} | |
return contentsPool.splice(0, requestSize); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment