Skip to content

Commit

Permalink
auto switch api all in once
Browse files Browse the repository at this point in the history
  • Loading branch information
ytshen committed Jan 17, 2025
1 parent 58af59a commit b3e5e38
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 14 deletions.
58 changes: 46 additions & 12 deletions src/modules/addonInfo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Sources, autoSource, currentSource, setAutoSource } from "../utils/configuration";
import { Source, Sources, autoSource, currentSource, setAutoSource } from "../utils/configuration";
// @ts-ignore
const { XPIDatabase } = ChromeUtils.import("resource://gre/modules/addons/XPIDatabase.jsm");
// @ts-ignore
Expand Down Expand Up @@ -334,18 +334,52 @@ export class AddonInfoManager {
* @returns AddonInfos from automatic source
*/
static async autoSwitchAvaliableApi(timeout = 3000) {
for (const source of Sources) {
if (!source.api) { continue; }
const infos = await AddonInfoAPI.fetchAddonInfos(source.api, timeout, () => {
ztoolkit.log(`check source from ${source.api} timeout!`);
interface ApiResult {
source: Source & { api: string };
infos: AddonInfo[];
}
const sourcesWithApi = Sources.filter((source): source is Source & { api: string } => !!source.api);
const sourcePromises: Promise<ApiResult>[] = sourcesWithApi.map((source): Promise<ApiResult> => {
return new Promise(async (resolve, reject) => {
try {
const infos = await AddonInfoAPI.fetchAddonInfos(source.api, timeout, () => {
ztoolkit.log(`check source from ${source.api} timeout!`);
});

if (infos.length > 0) {
resolve({ source, infos });
} else {
reject(new Error('No infos'));
}
} catch (error) {
ztoolkit.log(`Error fetching from ${source.api}: ${error}`);
reject(error);
}
});
if (infos.length > 0) {
this.shared.sourceInfos[source.api] = [new Date(), infos];
setAutoSource(source);
ztoolkit.log(`switch to ${source.id} automatically`);
return infos;
}
});
try {
const result: ApiResult = await Promise.any(sourcePromises);
const { source, infos } = result;
this.shared.sourceInfos[source.api] = [new Date(), infos];
setAutoSource(source);
ztoolkit.log(`switch to ${source.id} automatically`);
return infos;
} catch (error) {
return [];
}
return [];

// for (const source of Sources) {
// if (!source.api) { continue; }
// const infos = await AddonInfoAPI.fetchAddonInfos(source.api, timeout, () => {
// ztoolkit.log(`check source from ${source.api} timeout!`);
// });
// if (infos.length > 0) {
// this.shared.sourceInfos[source.api] = [new Date(), infos];
// setAutoSource(source);
// ztoolkit.log(`switch to ${source.id} automatically`);
// return infos;
// }
// }
// return [];
}
}
4 changes: 2 additions & 2 deletions src/utils/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { getPref, setPref } from "./prefs";
/**
* Add-on Source ID
*/
type SourceID =
export type SourceID =
"source-auto" |
"source-zotero-chinese-github" |
"source-zotero-chinese-gitee" |
Expand All @@ -20,7 +20,7 @@ type SourceID =
* id: Source ID
* api: Retrieve the JSON of addonInfo through this URL
*/
interface Source {
export interface Source {
id: SourceID;
api?: string;
};
Expand Down

0 comments on commit b3e5e38

Please sign in to comment.