Skip to content

Commit 556b912

Browse files
committed
refactor: extract runtime account manager cache
1 parent fa02a1e commit 556b912

2 files changed

Lines changed: 63 additions & 18 deletions

File tree

index.ts

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ import { applyFastSessionDefaults } from "./lib/request/request-transformer.js";
160160
import { isEmptyResponse } from "./lib/request/response-handler.js";
161161
import { withStreamingFailover } from "./lib/request/stream-failover.js";
162162
import { addJitter } from "./lib/rotation.js";
163+
import {
164+
invalidateRuntimeAccountManagerCache,
165+
reloadRuntimeAccountManager,
166+
} from "./lib/runtime/account-manager-cache.js";
163167
import { persistAccountPool } from "./lib/runtime/account-pool.js";
164168
import { applyAccountStorageScope } from "./lib/runtime/account-scope.js";
165169
import { handleAccountSelectEvent } from "./lib/runtime/account-select-event.js";
@@ -413,28 +417,33 @@ export const OpenAIOAuthPlugin: Plugin = async ({ client }: PluginInput) => {
413417
): string => getRuntimeStatusMarker(ui, status);
414418

415419
const invalidateAccountManagerCache = (): void => {
416-
cachedAccountManager = null;
417-
accountManagerPromise = null;
420+
invalidateRuntimeAccountManagerCache({
421+
setCachedAccountManager: (value) => {
422+
cachedAccountManager = value as AccountManager | null;
423+
},
424+
setAccountManagerPromise: (value) => {
425+
accountManagerPromise = value as Promise<AccountManager> | null;
426+
},
427+
});
418428
};
419429

420430
const reloadAccountManagerFromDisk = async (
421431
authFallback?: OAuthAuthDetails,
422-
): Promise<AccountManager> => {
423-
if (accountReloadInFlight) {
424-
return accountReloadInFlight;
425-
}
426-
accountReloadInFlight = (async () => {
427-
const reloaded = await AccountManager.loadFromDisk(authFallback);
428-
cachedAccountManager = reloaded;
429-
accountManagerPromise = Promise.resolve(reloaded);
430-
return reloaded;
431-
})();
432-
try {
433-
return await accountReloadInFlight;
434-
} finally {
435-
accountReloadInFlight = null;
436-
}
437-
};
432+
): Promise<AccountManager> =>
433+
reloadRuntimeAccountManager<AccountManager>({
434+
currentReloadInFlight: accountReloadInFlight,
435+
loadFromDisk: (fallback) => AccountManager.loadFromDisk(fallback),
436+
setCachedAccountManager: (value) => {
437+
cachedAccountManager = value;
438+
},
439+
setAccountManagerPromise: (value) => {
440+
accountManagerPromise = value;
441+
},
442+
setReloadInFlight: (value) => {
443+
accountReloadInFlight = value;
444+
},
445+
authFallback,
446+
});
438447

439448
const applyStorageScope = (
440449
pluginConfig: ReturnType<typeof loadPluginConfig>,
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import type { OAuthAuthDetails } from "../types.js";
2+
3+
export function invalidateRuntimeAccountManagerCache(deps: {
4+
setCachedAccountManager: (value: unknown) => void;
5+
setAccountManagerPromise: (value: Promise<unknown> | null) => void;
6+
}): void {
7+
deps.setCachedAccountManager(null);
8+
deps.setAccountManagerPromise(null);
9+
}
10+
11+
export async function reloadRuntimeAccountManager<TAccountManager>(deps: {
12+
currentReloadInFlight: Promise<TAccountManager> | null;
13+
loadFromDisk: (authFallback?: OAuthAuthDetails) => Promise<TAccountManager>;
14+
setCachedAccountManager: (value: TAccountManager) => void;
15+
setAccountManagerPromise: (value: Promise<TAccountManager> | null) => void;
16+
setReloadInFlight: (value: Promise<TAccountManager> | null) => void;
17+
authFallback?: OAuthAuthDetails;
18+
}): Promise<TAccountManager> {
19+
if (deps.currentReloadInFlight) {
20+
return deps.currentReloadInFlight;
21+
}
22+
23+
const reloadInFlight = (async () => {
24+
const reloaded = await deps.loadFromDisk(deps.authFallback);
25+
deps.setCachedAccountManager(reloaded);
26+
deps.setAccountManagerPromise(Promise.resolve(reloaded));
27+
return reloaded;
28+
})();
29+
30+
deps.setReloadInFlight(reloadInFlight);
31+
try {
32+
return await reloadInFlight;
33+
} finally {
34+
deps.setReloadInFlight(null);
35+
}
36+
}

0 commit comments

Comments
 (0)