-async function refreshActorIfNeeded <T extends MActorFull | MActorAccountChannelId> (
- actorArg: T,
- fetchedType: ActorFetchByUrlType
-): Promise<{ actor: T | MActorFull, refreshed: boolean }> {
- if (!actorArg.isOutdated()) return { actor: actorArg, refreshed: false }
+type RefreshResult <T> = Promise<{ actor: T | MActorFull, refreshed: boolean }>
+
+type RefreshOptions <T> = {
+ actor: T
+ fetchedType: ActorLoadByUrlType
+}
+
+const promiseCache = new PromiseCache(doRefresh, (options: RefreshOptions<MActorFull | MActorAccountChannelId>) => options.actor.url)
+
+function refreshActorIfNeeded <T extends MActorFull | MActorAccountChannelId> (options: RefreshOptions<T>): RefreshResult <T> {
+ const actorArg = options.actor
+ if (!actorArg.isOutdated()) return Promise.resolve({ actor: actorArg, refreshed: false })
+
+ return promiseCache.run(options)
+}
+
+export {
+ refreshActorIfNeeded
+}
+
+// ---------------------------------------------------------------------------
+
+async function doRefresh <T extends MActorFull | MActorAccountChannelId> (options: RefreshOptions<T>): RefreshResult <MActorFull> {
+ const { actor: actorArg, fetchedType } = options