Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

wakeups.ts

53 lines1.7 KB

1import * as Context from "effect/Context";
2import * as Layer from "effect/Layer";
3import type * as Stream from "effect/Stream";
4​
5export type ConnectionWakeup =
6 | "application-active"
7 | "application-active-probe"
8 | "application-active-long"
9 | "application-focused"
10 | "credentials-changed"
11 | "network-changed";
12​
13function isApplicationActiveWakeup(reason: ConnectionWakeup): boolean {
14 return (
15 reason === "application-active" ||
16 reason === "application-active-probe" ||
17 reason === "application-active-long"
18 );
20​
21export function resetsRetryBackoff(reason: ConnectionWakeup): boolean {
22 return isApplicationActiveWakeup(reason) || reason === "network-changed";
24​
25export function shouldResubscribeAfterWakeup(reason: ConnectionWakeup): boolean {
26 return isApplicationActiveWakeup(reason);
28​
29// A brief app switch (the mobile probe) does not justify listing the whole account again.
30export function shouldRelistAccountAfterWakeup(reason: ConnectionWakeup): boolean {
31 return (
32 reason === "application-active" ||
33 reason === "application-active-long" ||
34 reason === "application-focused"
35 );
37​
38// Focus fires on every window switch; any wakeup would arm a running probe's silent-socket check.
39export function reachesConnectionSupervisor(reason: ConnectionWakeup): boolean {
40 return reason !== "application-focused";
42​
43export class ConnectionWakeups extends Context.Service<
44 ConnectionWakeups,
45 {
46 readonly changes: Stream.Stream<ConnectionWakeup>;
47 }
48>()("@t3tools/client-runtime/connection/wakeups/ConnectionWakeups") {}
49​
50const make = (service: ConnectionWakeups["Service"]) => ConnectionWakeups.of(service);
51​
52export const layer = (service: ConnectionWakeups["Service"]) =>
53 Layer.succeed(ConnectionWakeups, make(service));