diff options
Diffstat (limited to 'src/state/models/cache')
-rw-r--r-- | src/state/models/cache/handle-resolutions.ts | 5 | ||||
-rw-r--r-- | src/state/models/cache/image-sizes.ts | 38 | ||||
-rw-r--r-- | src/state/models/cache/link-metas.ts | 44 | ||||
-rw-r--r-- | src/state/models/cache/my-follows.ts | 137 | ||||
-rw-r--r-- | src/state/models/cache/posts.ts | 70 | ||||
-rw-r--r-- | src/state/models/cache/profiles-view.ts | 50 |
6 files changed, 0 insertions, 344 deletions
diff --git a/src/state/models/cache/handle-resolutions.ts b/src/state/models/cache/handle-resolutions.ts deleted file mode 100644 index 2e2b69661..000000000 --- a/src/state/models/cache/handle-resolutions.ts +++ /dev/null @@ -1,5 +0,0 @@ -import {LRUMap} from 'lru_map' - -export class HandleResolutionsCache { - cache: LRUMap<string, string> = new LRUMap(500) -} diff --git a/src/state/models/cache/image-sizes.ts b/src/state/models/cache/image-sizes.ts deleted file mode 100644 index c30a68f4d..000000000 --- a/src/state/models/cache/image-sizes.ts +++ /dev/null @@ -1,38 +0,0 @@ -import {Image} from 'react-native' -import type {Dimensions} from 'lib/media/types' - -export class ImageSizesCache { - sizes: Map<string, Dimensions> = new Map() - activeRequests: Map<string, Promise<Dimensions>> = new Map() - - constructor() {} - - get(uri: string): Dimensions | undefined { - return this.sizes.get(uri) - } - - async fetch(uri: string): Promise<Dimensions> { - const Dimensions = this.sizes.get(uri) - if (Dimensions) { - return Dimensions - } - - const prom = - this.activeRequests.get(uri) || - new Promise<Dimensions>(resolve => { - Image.getSize( - uri, - (width: number, height: number) => resolve({width, height}), - (err: any) => { - console.error('Failed to fetch image dimensions for', uri, err) - resolve({width: 0, height: 0}) - }, - ) - }) - this.activeRequests.set(uri, prom) - const res = await prom - this.activeRequests.delete(uri) - this.sizes.set(uri, res) - return res - } -} diff --git a/src/state/models/cache/link-metas.ts b/src/state/models/cache/link-metas.ts deleted file mode 100644 index 607968c80..000000000 --- a/src/state/models/cache/link-metas.ts +++ /dev/null @@ -1,44 +0,0 @@ -import {makeAutoObservable} from 'mobx' -import {LRUMap} from 'lru_map' -import {RootStoreModel} from '../root-store' -import {LinkMeta, getLinkMeta} from 'lib/link-meta/link-meta' - -type CacheValue = Promise<LinkMeta> | LinkMeta -export class LinkMetasCache { - cache: LRUMap<string, CacheValue> = new LRUMap(100) - - constructor(public rootStore: RootStoreModel) { - makeAutoObservable( - this, - { - rootStore: false, - cache: false, - }, - {autoBind: true}, - ) - } - - // public api - // = - - async getLinkMeta(url: string) { - const cached = this.cache.get(url) - if (cached) { - try { - return await cached - } catch (e) { - // ignore, we'll try again - } - } - try { - const promise = getLinkMeta(this.rootStore, url) - this.cache.set(url, promise) - const res = await promise - this.cache.set(url, res) - return res - } catch (e) { - this.cache.delete(url) - throw e - } - } -} diff --git a/src/state/models/cache/my-follows.ts b/src/state/models/cache/my-follows.ts deleted file mode 100644 index e1e8af509..000000000 --- a/src/state/models/cache/my-follows.ts +++ /dev/null @@ -1,137 +0,0 @@ -import {makeAutoObservable} from 'mobx' -import { - AppBskyActorDefs, - AppBskyGraphGetFollows as GetFollows, - moderateProfile, -} from '@atproto/api' -import {RootStoreModel} from '../root-store' -import {bundleAsync} from 'lib/async/bundle' - -const MAX_SYNC_PAGES = 10 -const SYNC_TTL = 60e3 * 10 // 10 minutes - -type Profile = AppBskyActorDefs.ProfileViewBasic | AppBskyActorDefs.ProfileView - -export enum FollowState { - Following, - NotFollowing, - Unknown, -} - -export interface FollowInfo { - did: string - followRecordUri: string | undefined - handle: string - displayName: string | undefined - avatar: string | undefined -} - -/** - * This model is used to maintain a synced local cache of the user's - * follows. It should be periodically refreshed and updated any time - * the user makes a change to their follows. - */ -export class MyFollowsCache { - // data - byDid: Record<string, FollowInfo> = {} - lastSync = 0 - - constructor(public rootStore: RootStoreModel) { - makeAutoObservable( - this, - { - rootStore: false, - }, - {autoBind: true}, - ) - } - - // public api - // = - - clear() { - this.byDid = {} - } - - /** - * Syncs a subset of the user's follows - * for performance reasons, caps out at 1000 follows - */ - syncIfNeeded = bundleAsync(async () => { - if (this.lastSync > Date.now() - SYNC_TTL) { - return - } - - let cursor - for (let i = 0; i < MAX_SYNC_PAGES; i++) { - const res: GetFollows.Response = await this.rootStore.agent.getFollows({ - actor: this.rootStore.me.did, - cursor, - limit: 100, - }) - res.data.follows = res.data.follows.filter( - profile => - !moderateProfile(profile, this.rootStore.preferences.moderationOpts) - .account.filter, - ) - this.hydrateMany(res.data.follows) - if (!res.data.cursor) { - break - } - cursor = res.data.cursor - } - - this.lastSync = Date.now() - }) - - getFollowState(did: string): FollowState { - if (typeof this.byDid[did] === 'undefined') { - return FollowState.Unknown - } - if (typeof this.byDid[did].followRecordUri === 'string') { - return FollowState.Following - } - return FollowState.NotFollowing - } - - async fetchFollowState(did: string): Promise<FollowState> { - // TODO: can we get a more efficient method for this? getProfile fetches more data than we need -prf - const res = await this.rootStore.agent.getProfile({actor: did}) - this.hydrate(did, res.data) - return this.getFollowState(did) - } - - getFollowUri(did: string): string { - const v = this.byDid[did] - if (v && typeof v.followRecordUri === 'string') { - return v.followRecordUri - } - throw new Error('Not a followed user') - } - - addFollow(did: string, info: FollowInfo) { - this.byDid[did] = info - } - - removeFollow(did: string) { - if (this.byDid[did]) { - this.byDid[did].followRecordUri = undefined - } - } - - hydrate(did: string, profile: Profile) { - this.byDid[did] = { - did, - followRecordUri: profile.viewer?.following, - handle: profile.handle, - displayName: profile.displayName, - avatar: profile.avatar, - } - } - - hydrateMany(profiles: Profile[]) { - for (const profile of profiles) { - this.hydrate(profile.did, profile) - } - } -} diff --git a/src/state/models/cache/posts.ts b/src/state/models/cache/posts.ts deleted file mode 100644 index d3632f436..000000000 --- a/src/state/models/cache/posts.ts +++ /dev/null @@ -1,70 +0,0 @@ -import {LRUMap} from 'lru_map' -import {RootStoreModel} from '../root-store' -import { - AppBskyFeedDefs, - AppBskyEmbedRecord, - AppBskyEmbedRecordWithMedia, - AppBskyFeedPost, -} 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) - } - const embed = feedItem.post.embed - if ( - AppBskyEmbedRecord.isView(embed) && - AppBskyEmbedRecord.isViewRecord(embed.record) && - AppBskyFeedPost.isRecord(embed.record.value) && - AppBskyFeedPost.validateRecord(embed.record.value).success - ) { - this.set(embed.record.uri, embedViewToPostView(embed.record)) - } - if ( - AppBskyEmbedRecordWithMedia.isView(embed) && - AppBskyEmbedRecord.isViewRecord(embed.record?.record) && - AppBskyFeedPost.isRecord(embed.record.record.value) && - AppBskyFeedPost.validateRecord(embed.record.record.value).success - ) { - this.set( - embed.record.record.uri, - embedViewToPostView(embed.record.record), - ) - } - } -} - -function embedViewToPostView( - embedView: AppBskyEmbedRecord.ViewRecord, -): PostView { - return { - $type: 'app.bsky.feed.post#view', - uri: embedView.uri, - cid: embedView.cid, - author: embedView.author, - record: embedView.value, - indexedAt: embedView.indexedAt, - labels: embedView.labels, - } -} diff --git a/src/state/models/cache/profiles-view.ts b/src/state/models/cache/profiles-view.ts deleted file mode 100644 index e5a9be587..000000000 --- a/src/state/models/cache/profiles-view.ts +++ /dev/null @@ -1,50 +0,0 @@ -import {makeAutoObservable} from 'mobx' -import {LRUMap} from 'lru_map' -import {RootStoreModel} from '../root-store' -import {AppBskyActorGetProfile as GetProfile} from '@atproto/api' - -type CacheValue = Promise<GetProfile.Response> | GetProfile.Response -export class ProfilesCache { - cache: LRUMap<string, CacheValue> = new LRUMap(100) - - constructor(public rootStore: RootStoreModel) { - makeAutoObservable( - this, - { - rootStore: false, - cache: false, - }, - {autoBind: true}, - ) - } - - // public api - // = - - async getProfile(did: string) { - const cached = this.cache.get(did) - if (cached) { - try { - return await cached - } catch (e) { - // ignore, we'll try again - } - } - try { - const promise = this.rootStore.agent.getProfile({ - actor: did, - }) - this.cache.set(did, promise) - const res = await promise - this.cache.set(did, res) - return res - } catch (e) { - this.cache.delete(did) - throw e - } - } - - overwrite(did: string, res: GetProfile.Response) { - this.cache.set(did, res) - } -} |