parent
8c37a7cc02
commit
2b491ddb10
|
@ -10,7 +10,13 @@ export async function updatePinnedStatusesForAccount (accountId) {
|
|||
|
||||
await cacheFirstUpdateAfter(
|
||||
() => getPinnedStatuses(currentInstance, accessToken, accountId),
|
||||
() => database.getPinnedStatuses(currentInstance, accountId),
|
||||
async () => {
|
||||
let 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),
|
||||
statuses => {
|
||||
let { pinnedStatuses } = store.get()
|
||||
|
|
|
@ -5,6 +5,9 @@ export function fetchStatus (statusesStore, accountsStore, id, callback) {
|
|||
statusesStore.get(id).onsuccess = e => {
|
||||
let status = e.target.result
|
||||
callback(status)
|
||||
if (!status) {
|
||||
return
|
||||
}
|
||||
fetchAccount(accountsStore, status[ACCOUNT_ID], account => {
|
||||
status.account = account
|
||||
})
|
||||
|
|
|
@ -5,8 +5,12 @@ export async function cacheFirstUpdateAfter (networkFetcher, dbFetcher, dbUpdate
|
|||
let dbResponse
|
||||
try {
|
||||
dbResponse = await dbFetcher()
|
||||
stateSetter(dbResponse)
|
||||
} catch (err) {
|
||||
console.error('ignored DB error', err)
|
||||
} finally {
|
||||
if (dbResponse) {
|
||||
stateSetter(dbResponse)
|
||||
}
|
||||
let fetchAndUpdatePromise = networkPromise.then(networkResponse => {
|
||||
/* no await */ dbUpdater(networkResponse)
|
||||
stateSetter(networkResponse)
|
||||
|
@ -16,18 +20,3 @@ export async function cacheFirstUpdateAfter (networkFetcher, dbFetcher, dbUpdate
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Make a change that we optimistically show to the user as successful, but which
|
||||
// actually depends on a network operation. In the unlikely event that the network
|
||||
// operation fails, revert the changes
|
||||
export async function optimisticUpdate (doImmediately, networkUpdater, onSuccess, onFailure) {
|
||||
let networkPromise = networkUpdater()
|
||||
doImmediately()
|
||||
try {
|
||||
let response = await networkPromise
|
||||
onSuccess(response)
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
onFailure(e)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue