From 2b491ddb10d87d1a536a8367663f3b9d958af953 Mon Sep 17 00:00:00 2001 From: Nolan Lawson Date: Sat, 23 Feb 2019 15:18:48 -0800 Subject: [PATCH] fix: fix pinned toots not in IDB (#1040) fixes #660 --- src/routes/_actions/pinnedStatuses.js | 8 ++++++- src/routes/_database/timelines/fetchStatus.js | 3 +++ src/routes/_utils/sync.js | 21 +++++-------------- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/src/routes/_actions/pinnedStatuses.js b/src/routes/_actions/pinnedStatuses.js index 2022faa5..02923858 100644 --- a/src/routes/_actions/pinnedStatuses.js +++ b/src/routes/_actions/pinnedStatuses.js @@ -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() diff --git a/src/routes/_database/timelines/fetchStatus.js b/src/routes/_database/timelines/fetchStatus.js index 8e6b760a..e5bf027b 100644 --- a/src/routes/_database/timelines/fetchStatus.js +++ b/src/routes/_database/timelines/fetchStatus.js @@ -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 }) diff --git a/src/routes/_utils/sync.js b/src/routes/_utils/sync.js index 51344c31..8cc3b190 100644 --- a/src/routes/_utils/sync.js +++ b/src/routes/_utils/sync.js @@ -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) - } -}