diff --git a/routes/_components/Timeline.html b/routes/_components/Timeline.html index 9613fa4..7f111b5 100644 --- a/routes/_components/Timeline.html +++ b/routes/_components/Timeline.html @@ -108,13 +108,13 @@ if (process.env.NODE_ENV !== 'production') { console.log('addStatuses()') } - let instanceName = this.store.get('instanceName') + let instanceName = this.store.get('currentInstance') let timeline = this.get('timeline') - /* no await */ database.insertStatuses(instanceName, timeline, newStatuses) let statusIds = this.get('statusIds') if (!statusIds) { return } + /* no await */ database.insertStatuses(instanceName, timeline, newStatuses) let newStatusIds = newStatuses.map(status => status.id) let merged = mergeStatuses(statusIds, newStatusIds) this.set({ statusIds: merged }) diff --git a/routes/_utils/database/databaseCore.js b/routes/_utils/database/databaseCore.js index 7a07bc2..a1ddb8d 100644 --- a/routes/_utils/database/databaseCore.js +++ b/routes/_utils/database/databaseCore.js @@ -16,12 +16,20 @@ import { import QuickLRU from 'quick-lru' const statusesCache = new QuickLRU({maxSize: 100}) +const accountsCache = new QuickLRU({maxSize: 50}) if (process.browser && process.env.NODE_ENV !== 'production') { window.cacheStats = { - cache: statusesCache, - cacheHits: 0, - cacheMisses: 0 + statuses: { + cache: statusesCache, + hits: 0, + misses: 0 + }, + accounts: { + cache: accountsCache, + hits: 0, + misses: 0 + } } } @@ -51,6 +59,10 @@ export async function getTimeline(instanceName, timeline, maxId = null, limit = export async function insertStatuses(instanceName, timeline, statuses) { for (let status of statuses) { statusesCache.set(status.id, status) + accountsCache.set(status.account.id, status.account) + if (status.reblog) { + accountsCache.set(status.reblog.account.id, status.reblog.account) + } } const db = await getDatabase(instanceName, timeline) await dbPromise(db, [TIMELINE_STORE, STATUSES_STORE, ACCOUNTS_STORE], 'readwrite', (stores) => { @@ -90,12 +102,22 @@ export async function setInstanceVerifyCredentials(instanceName, verifyCredentia } export async function getAccount(instanceName, accountId) { + if (accountsCache.has(accountId)) { + if (process.browser && process.env.NODE_ENV !== 'production') { + window.cacheStats.accounts.hits++ + } + return accountsCache.get(accountId) + } const db = await getDatabase(instanceName) - return await dbPromise(db, ACCOUNTS_STORE, 'readonly', (store, callback) => { + let result = await dbPromise(db, ACCOUNTS_STORE, 'readonly', (store, callback) => { store.get(accountId).onsuccess = (e) => { callback(e.target.result && e.target.result) } }) + if (process.browser && process.env.NODE_ENV !== 'production') { + window.cacheStats.accounts.misses++ + } + return result } export async function clearDatabaseForInstance(instanceName) { @@ -105,7 +127,7 @@ export async function clearDatabaseForInstance(instanceName) { export async function getStatus(instanceName, statusId) { if (statusesCache.has(statusId)) { if (process.browser && process.env.NODE_ENV !== 'production') { - window.cacheStats.cacheHits++ + window.cacheStats.statuses.hits++ } return statusesCache.get(statusId) } @@ -117,7 +139,7 @@ export async function getStatus(instanceName, statusId) { }) statusesCache.set(statusId, result) if (process.browser && process.env.NODE_ENV !== 'production') { - window.cacheStats.cacheMisses++ + window.cacheStats.statuses.misses++ } return result } \ No newline at end of file diff --git a/routes/_utils/database/databaseLifecycle.js b/routes/_utils/database/databaseLifecycle.js index 9af324a..81f73af 100644 --- a/routes/_utils/database/databaseLifecycle.js +++ b/routes/_utils/database/databaseLifecycle.js @@ -9,6 +9,10 @@ import { } from './constants' export function getDatabase(instanceName) { + if (!instanceName && process.env.NODE_ENV !== 'production') { + console.trace() + throw new Error('instanceName is undefined in getDatabase()') + } if (databaseCache[instanceName]) { return Promise.resolve(databaseCache[instanceName]) }