(fix): connections
This commit is contained in:
parent
a43c05f035
commit
676dea2aa4
7 changed files with 92 additions and 127 deletions
|
|
@ -27,7 +27,6 @@ import { log } from "@tensamin/shared/log";
|
|||
import { useStorage } from "@tensamin/storage/context";
|
||||
|
||||
import {
|
||||
PING_INTERVAL,
|
||||
RECONNECT_RESET,
|
||||
RECONNECT_TRIES,
|
||||
RETRY_INTERVAL,
|
||||
|
|
@ -77,8 +76,6 @@ type ContextType = {
|
|||
subscribePush: (handler: PushHandler) => () => void;
|
||||
addInterceptor: (interceptor: MTPInterceptor) => () => void;
|
||||
readyState: number;
|
||||
ownPing: number;
|
||||
iotaPing: number;
|
||||
identified: boolean;
|
||||
freshContacts: Contacts;
|
||||
freshCommunities: Communities;
|
||||
|
|
@ -152,9 +149,6 @@ export function Provider(props: {
|
|||
const [identified, setIdentified] = useState<boolean>(false);
|
||||
const [identifying, setIdentifying] = useState<boolean>(false);
|
||||
|
||||
const [ownPing, setOwnPing] = useState<number>(0);
|
||||
const [iotaPing, setIotaPing] = useState<number>(0);
|
||||
|
||||
const [freshCommunities, setFreshCommunities] = useState<Communities>([]);
|
||||
const [freshContacts, setFreshContacts] = useState<Contacts>([]);
|
||||
const [freshCalls, setFreshCalls] = useState<Calls>([]);
|
||||
|
|
@ -232,32 +226,6 @@ export function Provider(props: {
|
|||
return () => interceptorsRef.current.delete(interceptor);
|
||||
}, []);
|
||||
|
||||
// Custom Pings
|
||||
useEffect(() => {
|
||||
if (!connected || !identified) {
|
||||
return;
|
||||
}
|
||||
|
||||
const interval = setInterval(async () => {
|
||||
try {
|
||||
const originalNow = Date.now();
|
||||
const data = await send("ClientPing", { LastPing: originalNow });
|
||||
setOwnPing(Date.now() - originalNow);
|
||||
|
||||
const remotePing = data.data.PingIota;
|
||||
if (typeof remotePing === "number") {
|
||||
setIotaPing(remotePing);
|
||||
}
|
||||
} catch (intervalError) {
|
||||
log(1, "mtp", "yellow", "Ping failed", intervalError);
|
||||
}
|
||||
}, PING_INTERVAL);
|
||||
|
||||
return () => {
|
||||
clearInterval(interval);
|
||||
};
|
||||
}, [connected, identified, send]);
|
||||
|
||||
// Reconnect stuff
|
||||
const resolveConnectionRef = useRef(() => {});
|
||||
useEffect(() => {
|
||||
|
|
@ -619,8 +587,6 @@ export function Provider(props: {
|
|||
subscribePush,
|
||||
addInterceptor,
|
||||
readyState,
|
||||
ownPing,
|
||||
iotaPing,
|
||||
identified,
|
||||
freshContacts,
|
||||
freshCommunities,
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
export const RETRY_INTERVAL = 3_000;
|
||||
export const PING_INTERVAL = 3_000;
|
||||
export const RECONNECT_TRIES = 3;
|
||||
export const RECONNECT_RESET = 6;
|
||||
|
|
|
|||
|
|
@ -225,14 +225,6 @@ export const mtp = {
|
|||
request: user.partial(),
|
||||
response: z.object({}),
|
||||
},
|
||||
ClientPing: {
|
||||
request: z.object({
|
||||
LastPing: z.number(),
|
||||
}),
|
||||
response: z.object({
|
||||
PingIota: z.number(),
|
||||
}),
|
||||
},
|
||||
MessageDelete: {
|
||||
request: z.object({
|
||||
ChatPartnerId: z.number(),
|
||||
|
|
|
|||
|
|
@ -61,17 +61,18 @@ export default function UserProvider(props: { children: ReactNode }) {
|
|||
|
||||
const request = (async () => {
|
||||
const cache = accountId ? createCache(String(accountId)) : null;
|
||||
const cached =
|
||||
const cachedValue =
|
||||
storageRef.current[userId] ?? (await cache?.profiles.get(userId));
|
||||
const cachedResult =
|
||||
schemas.GetUserData.response.safeParse(cachedValue);
|
||||
const cached = cachedResult.success ? cachedResult.data : undefined;
|
||||
if (cached) storageRef.current[userId] = cached;
|
||||
try {
|
||||
const userData = await send("GetUserData", { UserId: userId });
|
||||
const user = {
|
||||
...userData.data,
|
||||
avatar: userData.data.Avatar
|
||||
? `data:image/webp;base64,${atob(userData.data.Avatar)}`
|
||||
: undefined,
|
||||
};
|
||||
if (userData.type.startsWith("Error")) {
|
||||
throw new Error(`GetUserData failed: ${userData.type}`);
|
||||
}
|
||||
const user = userData.data;
|
||||
storageRef.current[userId] = user;
|
||||
return user;
|
||||
} catch (error) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue