about summary refs log tree commit diff
path: root/src/state/models/cache/posts.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/state/models/cache/posts.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/state/models/cache/posts.ts')
-rw-r--r--src/state/models/cache/posts.ts31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/state/models/cache/posts.ts b/src/state/models/cache/posts.ts
new file mode 100644
index 000000000..48621226a
--- /dev/null
+++ b/src/state/models/cache/posts.ts
@@ -0,0 +1,31 @@
+import {LRUMap} from 'lru_map'
+import {RootStoreModel} from '../root-store'
+import {AppBskyFeedDefs} from '@atproto/api'
+
+type PostView = AppBskyFeedDefs.PostView
+
+export class PostsCache {
+  cache: LRUMap<string, PostView> = new LRUMap(500)
+
+  constructor(public rootStore: RootStoreModel) {}
+
+  set(uri: string, postView: PostView) {
+    this.cache.set(uri, postView)
+    if (postView.author.handle) {
+      this.rootStore.handleResolutions.cache.set(
+        postView.author.handle,
+        postView.author.did,
+      )
+    }
+  }
+
+  fromFeedItem(feedItem: AppBskyFeedDefs.FeedViewPost) {
+    this.set(feedItem.post.uri, feedItem.post)
+    if (
+      feedItem.reply?.parent &&
+      AppBskyFeedDefs.isPostView(feedItem.reply?.parent)
+    ) {
+      this.set(feedItem.reply.parent.uri, feedItem.reply.parent)
+    }
+  }
+}