2018-01-28 21:09:39 +00:00
|
|
|
import { store } from '../_store/store'
|
2018-02-08 16:22:14 +00:00
|
|
|
import { database } from '../_database/database'
|
|
|
|
import { getTimeline } from '../_api/timelines'
|
2018-01-28 01:34:08 +00:00
|
|
|
import { toast } from '../_utils/toast'
|
|
|
|
import { mark, stop } from '../_utils/marks'
|
2018-01-28 23:02:02 +00:00
|
|
|
import { mergeArrays } from '../_utils/arrays'
|
2018-01-28 01:34:08 +00:00
|
|
|
|
|
|
|
const FETCH_LIMIT = 20
|
|
|
|
|
2018-02-09 06:29:29 +00:00
|
|
|
async function fetchTimelineItems (instanceName, accessToken, timelineName, lastTimelineItemId, online) {
|
2018-02-04 02:06:02 +00:00
|
|
|
mark('fetchTimelineItems')
|
|
|
|
let items
|
2018-01-28 01:34:08 +00:00
|
|
|
if (!online) {
|
2018-02-04 02:06:02 +00:00
|
|
|
items = await database.getTimeline(instanceName, timelineName, lastTimelineItemId, FETCH_LIMIT)
|
2018-01-28 01:34:08 +00:00
|
|
|
} else {
|
|
|
|
try {
|
2018-02-04 02:06:02 +00:00
|
|
|
items = await getTimeline(instanceName, accessToken, timelineName, lastTimelineItemId, FETCH_LIMIT)
|
|
|
|
/* no await */ database.insertTimelineItems(instanceName, timelineName, items)
|
2018-01-28 01:34:08 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e)
|
|
|
|
toast.say('Internet request failed. Showing offline content.')
|
2018-02-04 02:06:02 +00:00
|
|
|
items = await database.getTimeline(instanceName, timelineName, lastTimelineItemId, FETCH_LIMIT)
|
2018-01-28 01:34:08 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-04 02:06:02 +00:00
|
|
|
stop('fetchTimelineItems')
|
|
|
|
return items
|
2018-01-28 01:34:08 +00:00
|
|
|
}
|
|
|
|
|
2018-02-09 06:29:29 +00:00
|
|
|
async function addTimelineItems (instanceName, timelineName, newItems) {
|
2018-02-04 02:06:02 +00:00
|
|
|
console.log('addTimelineItems, length:', newItems.length)
|
|
|
|
mark('addTimelineItems')
|
|
|
|
let newIds = newItems.map(item => item.id)
|
2018-02-11 21:46:57 +00:00
|
|
|
addTimelineItemIds(instanceName, timelineName, newIds)
|
|
|
|
stop('addTimelineItems')
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function addTimelineItemIds (instanceName, timelineName, newIds) {
|
2018-02-04 02:06:02 +00:00
|
|
|
let oldIds = store.getForTimeline(instanceName, timelineName, 'timelineItemIds') || []
|
|
|
|
let merged = mergeArrays(oldIds, newIds)
|
|
|
|
store.setForTimeline(instanceName, timelineName, { timelineItemIds: merged })
|
2018-01-28 01:34:08 +00:00
|
|
|
}
|
|
|
|
|
2018-02-09 06:29:29 +00:00
|
|
|
async function fetchTimelineItemsAndPossiblyFallBack () {
|
2018-02-04 02:06:02 +00:00
|
|
|
mark('fetchTimelineItemsAndPossiblyFallBack')
|
2018-01-28 01:34:08 +00:00
|
|
|
let timelineName = store.get('currentTimeline')
|
|
|
|
let instanceName = store.get('currentInstance')
|
|
|
|
let accessToken = store.get('accessToken')
|
2018-02-04 02:06:02 +00:00
|
|
|
let lastTimelineItemId = store.get('lastTimelineItemId')
|
2018-01-28 01:34:08 +00:00
|
|
|
let online = store.get('online')
|
|
|
|
|
2018-02-04 02:06:02 +00:00
|
|
|
let newItems = await fetchTimelineItems(instanceName, accessToken, timelineName, lastTimelineItemId, online)
|
|
|
|
addTimelineItems(instanceName, timelineName, newItems)
|
|
|
|
stop('fetchTimelineItemsAndPossiblyFallBack')
|
2018-01-28 01:34:08 +00:00
|
|
|
}
|
|
|
|
|
2018-02-09 06:29:29 +00:00
|
|
|
export function initializeTimeline () {
|
2018-01-28 01:34:08 +00:00
|
|
|
mark('initializeTimeline')
|
|
|
|
let instanceName = store.get('currentInstance')
|
|
|
|
let timeline = store.get('currentTimeline')
|
|
|
|
requestAnimationFrame(() => {
|
|
|
|
requestAnimationFrame(() => {
|
2018-02-12 00:10:39 +00:00
|
|
|
console.log('initialized')
|
2018-01-28 01:34:08 +00:00
|
|
|
store.setForTimeline(instanceName, timeline, {initialized: true})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
stop('initializeTimeline')
|
|
|
|
}
|
|
|
|
|
2018-02-09 06:29:29 +00:00
|
|
|
export async function setupTimeline () {
|
2018-02-04 02:06:02 +00:00
|
|
|
mark('setupTimeline')
|
2018-02-09 02:54:48 +00:00
|
|
|
if (!store.get('timelineItemIds')) {
|
2018-02-04 02:06:02 +00:00
|
|
|
await fetchTimelineItemsAndPossiblyFallBack()
|
2018-01-28 01:34:08 +00:00
|
|
|
}
|
2018-02-04 02:06:02 +00:00
|
|
|
stop('setupTimeline')
|
2018-01-28 01:34:08 +00:00
|
|
|
}
|
|
|
|
|
2018-02-09 06:29:29 +00:00
|
|
|
export async function fetchTimelineItemsOnScrollToBottom () {
|
2018-01-28 01:34:08 +00:00
|
|
|
let timelineName = store.get('currentTimeline')
|
|
|
|
let instanceName = store.get('currentInstance')
|
|
|
|
store.setForTimeline(instanceName, timelineName, { runningUpdate: true })
|
2018-02-04 02:06:02 +00:00
|
|
|
await fetchTimelineItemsAndPossiblyFallBack()
|
2018-01-28 01:34:08 +00:00
|
|
|
store.setForTimeline(instanceName, timelineName, { runningUpdate: false })
|
2018-02-09 06:29:29 +00:00
|
|
|
}
|
2018-02-12 03:15:21 +00:00
|
|
|
|
|
|
|
export async function showMoreItemsForCurrentTimeline() {
|
2018-02-12 06:40:56 +00:00
|
|
|
mark('showMoreItemsForCurrentTimeline')
|
2018-02-12 03:15:21 +00:00
|
|
|
let instanceName = store.get('currentInstance')
|
|
|
|
let timelineName = store.get('currentTimeline')
|
|
|
|
let itemIdsToAdd = store.get('itemIdsToAdd')
|
|
|
|
addTimelineItemIds(instanceName, timelineName, itemIdsToAdd)
|
|
|
|
store.setForTimeline(instanceName, timelineName, {
|
|
|
|
itemIdsToAdd: [],
|
|
|
|
shouldShowHeader: false,
|
|
|
|
showHeader: false
|
|
|
|
})
|
2018-02-12 06:40:56 +00:00
|
|
|
stop('showMoreItemsForCurrentTimeline')
|
2018-02-12 03:15:21 +00:00
|
|
|
}
|