fix: fix pinned toots not in IDB (#1040)

fixes #660
This commit is contained in:
Nolan Lawson 2019-02-23 15:18:48 -08:00 committed by GitHub
parent 8c37a7cc02
commit 2b491ddb10
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 17 deletions

View File

@ -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()

View File

@ -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
})

View File

@ -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)
}
}