Skip to content

Instantly share code, notes, and snippets.

@perymimon
Created November 6, 2019 08:12
Show Gist options
  • Save perymimon/12eebc6b5e8be73ca5cb6c4aefa53a97 to your computer and use it in GitHub Desktop.
Save perymimon/12eebc6b5e8be73ca5cb6c4aefa53a97 to your computer and use it in GitHub Desktop.
dispenser too bring async data
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