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/state/models/cache/posts.ts | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/state/models/cache/posts.ts (limited to 'src/state/models/cache/posts.ts') 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 = 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) + } + } +} -- cgit 1.4.1