pinafore/src/routes/_actions/pinnedStatuses.js

29 lines
1 KiB
JavaScript
Raw Normal View History

2018-02-11 18:35:25 +00:00
import { store } from '../_store/store'
import { cacheFirstUpdateAfter } from '../_utils/sync'
import { database } from '../_database/database'
import {
getPinnedStatuses
} from '../_api/pinnedStatuses'
2018-02-11 18:35:25 +00:00
2018-02-11 22:11:03 +00:00
export async function updatePinnedStatusesForAccount (accountId) {
2019-08-03 20:49:37 +00:00
const { currentInstance, accessToken } = store.get()
2018-02-11 18:35:25 +00:00
await cacheFirstUpdateAfter(
() => getPinnedStatuses(currentInstance, accessToken, accountId),
async () => {
2019-08-03 20:49:37 +00:00
const pinnedStatuses = await database.getPinnedStatuses(currentInstance, accountId)
if (!pinnedStatuses || !pinnedStatuses.every(Boolean)) {
throw new Error('missing pinned statuses in idb')
}
return pinnedStatuses
},
statuses => database.insertPinnedStatuses(currentInstance, accountId, statuses),
2018-02-11 18:35:25 +00:00
statuses => {
2019-08-03 20:49:37 +00:00
const { pinnedStatuses } = store.get()
pinnedStatuses[currentInstance] = pinnedStatuses[currentInstance] || {}
pinnedStatuses[currentInstance][accountId] = statuses
store.set({ pinnedStatuses: pinnedStatuses })
2018-02-11 18:35:25 +00:00
}
)
2018-02-11 22:11:03 +00:00
}