about summary refs log tree commit diff
path: root/src/lib/api/index.ts
diff options
context:
space:
mode:
authorPaul Frazee <pfrazee@gmail.com>2023-08-03 09:44:43 -0700
committerGitHub <noreply@github.com>2023-08-03 09:44:43 -0700
commita63f97aef2ea70ff9f1b81d40073f36ae8e88278 (patch)
treebf93006d20d099b3dc80861e68a4bd1f53d2a097 /src/lib/api/index.ts
parent7256169506332c7935ced4e0babcbd07159b9402 (diff)
downloadvoidsky-a63f97aef2ea70ff9f1b81d40073f36ae8e88278.tar.zst
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
Diffstat (limited to 'src/lib/api/index.ts')
-rw-r--r--src/lib/api/index.ts22
1 files changed, 18 insertions, 4 deletions
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(