import type { CallStatus } from './api';
export const NO_ANSWER_MS = 60_000;
* The same when the callee's app was closed and they got a notification
* instead: time to see it, open Quple, and answer (its invite is kept 2 min).
*/
export const NOTIFIED_NO_ANSWER_MS = 100_000;
export const PEER_LOST_MS = 45_000;
export const JOIN_TIMEOUT_MS = 45_000;
* Alone in the room this long while the other side's call screen has been
* up just as long (their heartbeats): one of us is stuck, rejoin. Counted
* from whichever came later, so a callee still loading Jitsi isn't it.
*/
export const ALONE_REJOIN_MS = 30_000;
export const ALONE_END_MS = 120_000;
export const MAX_REJOINS = 3;
export function peerEnded(st: CallStatus, me: string): boolean {
return st.peerEnded ?? (!!st.endedBy && st.endedBy !== me);
}
export type WatchState = {
now: number;
startedAt: number;
attemptStartedAt: number;
rejoins: number;
joined: boolean;
remotes: number;
aloneSince: number | null;
peerSince: number | null;
status: CallStatus | null;
statusAt: number | null;
me: string;
};
export type Verdict =
| { action: 'none' }
| { action: 'rejoin'; why: string }
| { action: 'end'; reason: string };
export function decide(s: WatchState): Verdict {
const st = s.status;
const fresh = st !== null && s.statusAt !== null && s.now - s.statusAt < 10_000;
if (st && peerEnded(st, s.me)) {
return { action: 'end', reason: st.peerSeenMsAgo === null ? `${st.peer} didn't answer` : `${st.peer} hung up` };
}
if (s.remotes === 0 && fresh && st) {
if (st.peerSeenMsAgo === null && s.now - s.startedAt > NO_ANSWER_MS) {
return { action: 'end', reason: `${st.peer} didn't answer` };
}
if (st.peerSeenMsAgo !== null && st.peerSeenMsAgo > PEER_LOST_MS) {
return { action: 'end', reason: `Lost the connection to ${st.peer}` };
}
}
if (!s.joined && s.now - s.attemptStartedAt > JOIN_TIMEOUT_MS) {
return s.rejoins < MAX_REJOINS
? { action: 'rejoin', why: 'not joined' }
: { action: 'end', reason: "Couldn't connect the call" };
}
if (s.joined && s.remotes === 0 && s.aloneSince !== null) {
const alone = s.now - s.aloneSince;
if (alone > ALONE_END_MS) {
return { action: 'end', reason: st ? `Lost the connection to ${st.peer}` : 'Lost the connection' };
}
const peerHere = fresh && st && st.peerSeenMsAgo !== null && st.peerSeenMsAgo < 10_000 && s.peerSince !== null;
if (peerHere && s.now - Math.max(s.aloneSince, s.peerSince!) > ALONE_REJOIN_MS && s.rejoins < MAX_REJOINS) {
return { action: 'rejoin', why: 'peer in the call but not in the room' };
}
}
return { action: 'none' };
}