From fee954bf27d7f3e24e03518a5849e8f8931b7ba1 Mon Sep 17 00:00:00 2001 From: Nolan Lawson Date: Thu, 8 Feb 2018 17:51:48 -0800 Subject: [PATCH] clean up actions, implement cache-first sync strategy --- .../[accountId].js => _actions/accounts.js} | 6 ++-- .../add.js => _actions/addInstance.js} | 12 ++++---- .../instances.js} | 29 +++++++++---------- routes/_actions/lists.js | 20 +++++++++++++ routes/_actions/timeline.js | 3 -- routes/_components/Nav.html | 7 ----- routes/_store/observers.js | 6 ++-- routes/_utils/sync.js | 18 ++++++++++++ routes/accounts/[accountId].html | 4 +-- routes/community/_actions/community.js | 19 ------------ routes/community/index.html | 4 +-- routes/lists/[listId].html | 4 --- routes/settings/instances/[instanceName].html | 2 +- routes/settings/instances/add.html | 2 +- 14 files changed, 69 insertions(+), 67 deletions(-) rename routes/{accounts/_actions/[accountId].js => _actions/accounts.js} (90%) rename routes/{settings/instances/_actions/add.js => _actions/addInstance.js} (90%) rename routes/{settings/instances/_actions/[instanceName].js => _actions/instances.js} (71%) create mode 100644 routes/_actions/lists.js create mode 100644 routes/_utils/sync.js delete mode 100644 routes/community/_actions/community.js diff --git a/routes/accounts/_actions/[accountId].js b/routes/_actions/accounts.js similarity index 90% rename from routes/accounts/_actions/[accountId].js rename to routes/_actions/accounts.js index 054c44de..0414577c 100644 --- a/routes/accounts/_actions/[accountId].js +++ b/routes/_actions/accounts.js @@ -1,6 +1,6 @@ -import { getAccount, getRelationship } from '../../_api/user' -import { database } from '../../_database/database' -import { store } from '../../_store/store' +import { getAccount, getRelationship } from '../_api/user' +import { database } from '../_database/database' +import { store } from '../_store/store' async function updateAccount(accountId, instanceName, accessToken) { let localPromise = database.getAccount(instanceName, accountId) diff --git a/routes/settings/instances/_actions/add.js b/routes/_actions/addInstance.js similarity index 90% rename from routes/settings/instances/_actions/add.js rename to routes/_actions/addInstance.js index 064db686..6cf9d226 100644 --- a/routes/settings/instances/_actions/add.js +++ b/routes/_actions/addInstance.js @@ -1,10 +1,10 @@ -import { getAccessTokenFromAuthCode, registerApplication, generateAuthLink } from '../../../_api/oauth' -import { getInstanceInfo } from '../../../_api/instance' +import { getAccessTokenFromAuthCode, registerApplication, generateAuthLink } from '../_api/oauth' +import { getInstanceInfo } from '../_api/instance' import { goto } from 'sapper/runtime.js' -import { switchToTheme } from '../../../_utils/themeEngine' -import { database } from '../../../_database/database' -import { store } from '../../../_store/store' -import { updateVerifyCredentialsForInstance } from './[instanceName]' +import { switchToTheme } from '../_utils/themeEngine' +import { database } from '../_database/database' +import { store } from '../_store/store' +import { updateVerifyCredentialsForInstance } from './instances' const REDIRECT_URI = (typeof location !== 'undefined' ? location.origin : 'https://pinafore.social') + '/settings/instances/add' diff --git a/routes/settings/instances/_actions/[instanceName].js b/routes/_actions/instances.js similarity index 71% rename from routes/settings/instances/_actions/[instanceName].js rename to routes/_actions/instances.js index 8f8c6318..63dd0319 100644 --- a/routes/settings/instances/_actions/[instanceName].js +++ b/routes/_actions/instances.js @@ -1,10 +1,10 @@ -import { getVerifyCredentials } from '../../../_api/user' -import { store } from '../../../_store/store' -import { switchToTheme } from '../../../_utils/themeEngine' -import { toast } from '../../../_utils/toast' -import { database } from '../../../_database/database' +import { getVerifyCredentials } from '../_api/user' +import { store } from '../_store/store' +import { switchToTheme } from '../_utils/themeEngine' +import { toast } from '../_utils/toast' +import { database } from '../_database/database' import { goto } from 'sapper/runtime.js' -import pAny from 'p-any' +import { cacheFirstUpdateAfter } from '../_utils/sync' export function changeTheme(instanceName, newTheme) { let instanceThemes = store.get('instanceThemes') @@ -55,14 +55,11 @@ function setStoreVerifyCredentials(instanceName, thisVerifyCredentials) { export async function updateVerifyCredentialsForInstance(instanceName) { let loggedInInstances = store.get('loggedInInstances') - let instanceData = loggedInInstances[instanceName] - await pAny([ - database.getInstanceVerifyCredentials(instanceName).then(verifyCredentials => { - setStoreVerifyCredentials(instanceName, verifyCredentials) - }), - getVerifyCredentials(instanceName, instanceData.access_token).then(verifyCredentials => { - setStoreVerifyCredentials(instanceName, verifyCredentials) - return database.setInstanceVerifyCredentials(instanceName, verifyCredentials) - }) - ]) + let accessToken = loggedInInstances[instanceName].access_token + await cacheFirstUpdateAfter( + () => getVerifyCredentials(instanceName, accessToken), + () => database.getInstanceVerifyCredentials(instanceName), + verifyCredentials => database.setInstanceVerifyCredentials(instanceName, verifyCredentials), + verifyCredentials => setStoreVerifyCredentials(instanceName, verifyCredentials) + ) } \ No newline at end of file diff --git a/routes/_actions/lists.js b/routes/_actions/lists.js new file mode 100644 index 00000000..da3616d3 --- /dev/null +++ b/routes/_actions/lists.js @@ -0,0 +1,20 @@ +import { store } from '../_store/store' +import { database } from '../_database/database' +import { getLists } from '../_api/lists' +import { cacheFirstUpdateAfter } from '../_utils/sync' + +export async function updateLists() { + let instanceName = store.get('currentInstance') + let accessToken = store.get('accessToken') + + await cacheFirstUpdateAfter( + () => getLists(instanceName, accessToken), + () => database.getLists(instanceName), + lists => database.setLists(instanceName, lists), + lists => { + let instanceLists = store.get('instanceLists') + instanceLists[instanceName] = lists + store.set({instanceLists: instanceLists}) + } + ) +} \ No newline at end of file diff --git a/routes/_actions/timeline.js b/routes/_actions/timeline.js index 79534b4b..bd69db42 100644 --- a/routes/_actions/timeline.js +++ b/routes/_actions/timeline.js @@ -63,9 +63,6 @@ export function initializeTimeline() { export async function setupTimeline() { mark('setupTimeline') - let timelineName = store.get('currentTimeline') - let instanceName = store.get('currentInstance') - let accessToken = store.get('accessToken') if (!store.get('timelineItemIds').length) { await fetchTimelineItemsAndPossiblyFallBack() } diff --git a/routes/_components/Nav.html b/routes/_components/Nav.html index 655bb478..0f2655ba 100644 --- a/routes/_components/Nav.html +++ b/routes/_components/Nav.html @@ -64,15 +64,8 @@