diff options
author | Paul Frazee <pfrazee@gmail.com> | 2022-11-14 16:56:59 -0600 |
---|---|---|
committer | Paul Frazee <pfrazee@gmail.com> | 2022-11-14 16:56:59 -0600 |
commit | 6e93301542ce9593f614ab89883e3c87e38c5769 (patch) | |
tree | 7e93e496d97d60087a44d7da523d45b30b33743d /src | |
parent | 723dee5704b4e5c9f51d4311752eced07e7f135f (diff) | |
download | voidsky-6e93301542ce9593f614ab89883e3c87e38c5769.tar.zst |
Add profile info caching
Diffstat (limited to 'src')
-rw-r--r-- | src/state/models/profiles-view.ts | 44 | ||||
-rw-r--r-- | src/state/models/root-store.ts | 2 | ||||
-rw-r--r-- | src/view/com/util/UserInfoText.tsx | 3 |
3 files changed, 47 insertions, 2 deletions
diff --git a/src/state/models/profiles-view.ts b/src/state/models/profiles-view.ts new file mode 100644 index 000000000..4dc5a3999 --- /dev/null +++ b/src/state/models/profiles-view.ts @@ -0,0 +1,44 @@ +import {makeAutoObservable} from 'mobx' +import {LRUMap} from 'lru_map' +import {RootStoreModel} from './root-store' +import * as GetProfile from '../../third-party/api/src/client/types/app/bsky/actor/getProfile' + +type CacheValue = Promise<GetProfile.Response> | GetProfile.Response +export class ProfilesViewModel { + 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.api.app.bsky.actor.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 + } + } +} diff --git a/src/state/models/root-store.ts b/src/state/models/root-store.ts index e2a505768..b7a04800b 100644 --- a/src/state/models/root-store.ts +++ b/src/state/models/root-store.ts @@ -10,6 +10,7 @@ import {isObj, hasProp} from '../lib/type-guards' import {SessionModel} from './session' import {NavigationModel} from './navigation' import {ShellUiModel} from './shell-ui' +import {ProfilesViewModel} from './profiles-view' import {MeModel} from './me' import {OnboardModel} from './onboard' @@ -19,6 +20,7 @@ export class RootStoreModel { shell = new ShellUiModel() me = new MeModel(this) onboard = new OnboardModel() + profiles = new ProfilesViewModel(this) constructor(public api: SessionServiceClient) { makeAutoObservable(this, { diff --git a/src/view/com/util/UserInfoText.tsx b/src/view/com/util/UserInfoText.tsx index 755e6ef4f..f4dbd1fa4 100644 --- a/src/view/com/util/UserInfoText.tsx +++ b/src/view/com/util/UserInfoText.tsx @@ -33,8 +33,7 @@ export function UserInfoText({ useEffect(() => { let aborted = false - // TODO use caching to reduce loads - store.api.app.bsky.actor.getProfile({actor: did}).then( + store.profiles.getProfile(did).then( v => { if (aborted) return setProfile(v.data) |