diff options
Diffstat (limited to 'src/state/models')
-rw-r--r-- | src/state/models/link-metas-view.ts | 44 | ||||
-rw-r--r-- | src/state/models/root-store.ts | 2 |
2 files changed, 46 insertions, 0 deletions
diff --git a/src/state/models/link-metas-view.ts b/src/state/models/link-metas-view.ts new file mode 100644 index 000000000..0187f4260 --- /dev/null +++ b/src/state/models/link-metas-view.ts @@ -0,0 +1,44 @@ +import {makeAutoObservable} from 'mobx' +import {LRUMap} from 'lru_map' +import {RootStoreModel} from './root-store' +import {LinkMeta, getLinkMeta} from '../../lib/link-meta' + +type CacheValue = Promise<LinkMeta> | LinkMeta +export class LinkMetasViewModel { + 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(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/root-store.ts b/src/state/models/root-store.ts index b7a04800b..af79ccc1e 100644 --- a/src/state/models/root-store.ts +++ b/src/state/models/root-store.ts @@ -11,6 +11,7 @@ import {SessionModel} from './session' import {NavigationModel} from './navigation' import {ShellUiModel} from './shell-ui' import {ProfilesViewModel} from './profiles-view' +import {LinkMetasViewModel} from './link-metas-view' import {MeModel} from './me' import {OnboardModel} from './onboard' @@ -21,6 +22,7 @@ export class RootStoreModel { me = new MeModel(this) onboard = new OnboardModel() profiles = new ProfilesViewModel(this) + linkMetas = new LinkMetasViewModel(this) constructor(public api: SessionServiceClient) { makeAutoObservable(this, { |