From a63f97aef2ea70ff9f1b81d40073f36ae8e88278 Mon Sep 17 00:00:00 2001 From: Paul Frazee Date: Thu, 3 Aug 2023 09:44:43 -0700 Subject: Use a post and handle-resolution cache to enable quick postthread loading (#1097) * Use a post and handle-resolution cache to enable quick postthread loading * Fix positioning of thread when loaded from cache and give more visual cues * Include parent posts in cache * Include notifications in cache --- src/lib/api/index.ts | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) (limited to 'src/lib/api/index.ts') diff --git a/src/lib/api/index.ts b/src/lib/api/index.ts index 458ef7baa..381d78435 100644 --- a/src/lib/api/index.ts +++ b/src/lib/api/index.ts @@ -29,10 +29,24 @@ export async function resolveName(store: RootStoreModel, didOrHandle: string) { if (didOrHandle.startsWith('did:')) { return didOrHandle } - const res = await store.agent.resolveHandle({ - handle: didOrHandle, - }) - return res.data.did + + // we run the resolution always to ensure freshness + const promise = store.agent + .resolveHandle({ + handle: didOrHandle, + }) + .then(res => { + store.handleResolutions.cache.set(didOrHandle, res.data.did) + return res.data.did + }) + + // but we can return immediately if it's cached + const cached = store.handleResolutions.cache.get(didOrHandle) + if (cached) { + return cached + } + + return promise } export async function uploadBlob( -- cgit 1.4.1