1
0
mirror of https://git.sr.ht/~cadence/bibliogram synced 2024-09-28 14:57:30 +00:00
bibliogram/src/lib/structures/Timeline.js

46 lines
1.1 KiB
JavaScript
Raw Normal View History

2020-01-12 12:50:21 +00:00
const constants = require("../constants")
const TimelineImage = require("./TimelineImage")
const collectors = require("../collectors")
require("../testimports")(constants, TimelineImage)
function transformEdges(edges) {
return edges.map(e => new TimelineImage(e.node))
}
class Timeline {
/**
* @param {import("./User")} user
*/
constructor(user) {
this.user = user
this.pages = []
this.addPage(this.user.data.edge_owner_to_timeline_media)
this.page_info = this.user.data.edge_owner_to_timeline_media.page_info
}
hasNextPage() {
return this.page_info.has_next_page
}
fetchNextPage() {
if (!this.hasNextPage()) return constants.symbols.NO_MORE_PAGES
return collectors.fetchTimelinePage(this.user.data.id, this.page_info.end_cursor).then(page => {
this.addPage(page)
return this.pages.slice(-1)[0]
})
}
async fetchUpToPage(index) {
while (this.pages[index] === undefined && this.hasNextPage()) {
await this.fetchNextPage()
}
}
addPage(page) {
this.pages.push(transformEdges(page.edges))
this.page_info = page.page_info
}
}
module.exports = Timeline