mirror of
https://github.com/clash-verge-rev/clash-verge-rev
synced 2025-05-04 16:47:33 +08:00
fix: the first function call creates multiple axios instances (#3273)
This commit is contained in:
parent
c894a15d13
commit
49c81f6201
@ -3,13 +3,9 @@ import { getClashInfo } from "./cmds";
|
|||||||
import { invoke } from "@tauri-apps/api/core";
|
import { invoke } from "@tauri-apps/api/core";
|
||||||
import { useLockFn } from "ahooks";
|
import { useLockFn } from "ahooks";
|
||||||
|
|
||||||
let axiosIns: AxiosInstance = null!;
|
let instancePromise: Promise<AxiosInstance> = null!;
|
||||||
|
|
||||||
/// initialize some information
|
|
||||||
/// enable force update axiosIns
|
|
||||||
export const getAxios = async (force: boolean = false) => {
|
|
||||||
if (axiosIns && !force) return axiosIns;
|
|
||||||
|
|
||||||
|
async function getInstancePromise() {
|
||||||
let server = "";
|
let server = "";
|
||||||
let secret = "";
|
let secret = "";
|
||||||
|
|
||||||
@ -26,13 +22,22 @@ export const getAxios = async (force: boolean = false) => {
|
|||||||
if (info?.secret) secret = info?.secret;
|
if (info?.secret) secret = info?.secret;
|
||||||
} catch {}
|
} catch {}
|
||||||
|
|
||||||
axiosIns = axios.create({
|
const axiosIns = axios.create({
|
||||||
baseURL: `http://${server}`,
|
baseURL: `http://${server}`,
|
||||||
headers: secret ? { Authorization: `Bearer ${secret}` } : {},
|
headers: secret ? { Authorization: `Bearer ${secret}` } : {},
|
||||||
timeout: 15000,
|
timeout: 15000,
|
||||||
});
|
});
|
||||||
axiosIns.interceptors.response.use((r) => r.data);
|
axiosIns.interceptors.response.use((r) => r.data);
|
||||||
return axiosIns;
|
return axiosIns;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// initialize some information
|
||||||
|
/// enable force update axiosIns
|
||||||
|
export const getAxios = async (force: boolean = false) => {
|
||||||
|
if (!instancePromise || force) {
|
||||||
|
instancePromise = getInstancePromise();
|
||||||
|
}
|
||||||
|
return instancePromise;
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Get Version
|
/// Get Version
|
||||||
|
Loading…
x
Reference in New Issue
Block a user