@kirz/expo-speedtest
Version:
Measure internet latency, download and upload speed
53 lines • 1.89 kB
JavaScript
import { startMeasure, ping } from "./speedtest";
const defaultState = {
progress: null,
results: { download: null, upload: null, ping: null },
};
export const createZustandStore = (create) => {
return create((set) => ({
status: "ready",
...defaultState,
start: async ({ duration, tests = ["download", "upload", "ping"], } = {}) => {
try {
set({ status: "testing", ...defaultState });
set({
results: { download: null, upload: null, ping: null },
});
const results = {};
await startMeasure({
types: tests,
duration,
onMeasureStart(type) {
set({
progress: { type, result: 0, percent: 0 },
});
},
onMeasureFinish(type, result) {
results[type] = result;
set({ results });
},
onMeasureProgress(type, result, progress) {
set({
progress: { type, result, percent: progress },
});
},
});
}
finally {
set({ status: "ready", progress: null });
}
},
getIpAddress: async () => {
const response = await fetch("https://api.ipify.org?format=json");
const data = await response.json();
return data.ip;
},
getIpInfo: async () => {
const response = await fetch("https://ipapi.co/json/");
const data = await response.json();
return data.ip;
},
ping,
}));
};
//# sourceMappingURL=create-zustand-store.js.map