Open History to see the last commit
1import type { DeviceHubAccess } from "@t3tools/client-runtime/state/deviceHubAccess";
2import * as Effect from "effect/Effect";
3import * as Schedule from "effect/Schedule";
4import * as Stream from "effect/Stream";
5import { AsyncResult, Atom } from "effect/reactivity";
6
7import {
8 appendDeviceEventLog,
9 type DeviceAxElement,
10 type DeviceEventLogEntry,
11 type DeviceForegroundInfo,
12 type DeviceHubTarget,
13 fetchDeviceAxTree,
14 subscribeDeviceEventLog,
15 subscribeDeviceForeground,
16} from "./deviceHubApi";
17import { AndroidFoldError, readAndroidFold, type AndroidFoldState } from "./deviceFold";
18
19const AX_POLL_INTERVAL_MS = 2_000;
20const FOLD_RETRY_MS = 3_000;
21
22/** `undefined` until the hub reports an app; the device detail's app fills in until then. */
23export const deviceForegroundAtom = Atom.family((target: DeviceHubTarget) =>
24 Atom.make<DeviceForegroundInfo | null | undefined>((get) => {
25 get.addFinalizer(subscribeDeviceForeground(target, (app) => get.setSelf(app)));
26 return undefined;
27 }).pipe(Atom.withLabel(`device-foreground:${target.deviceId}`)),
28);
29
30export const deviceEventLogAtom = Atom.family((target: DeviceHubTarget) =>
31 Atom.make<ReadonlyArray<DeviceEventLogEntry>>((get) => {
32 let entries: ReadonlyArray<DeviceEventLogEntry> = [];
33 get.addFinalizer(
34 subscribeDeviceEventLog(target, (incoming, reset) => {
35 entries = appendDeviceEventLog(entries, incoming, reset);
36 get.setSelf(entries);
37 }),
38 );
39 return entries;
40 }).pipe(Atom.withLabel(`device-event-log:${target.deviceId}`)),
41);
42
43// A failed poll keeps the last frames on screen; the next poll replaces them.
44export const deviceAxElementsAtom = Atom.family((target: DeviceHubTarget) =>
45 Atom.make(
46 Stream.fromEffectSchedule(
47 Effect.tryPromise((signal) => fetchDeviceAxTree(target, signal)).pipe(
48 Effect.map((tree): ReadonlyArray<DeviceAxElement> | null => tree.elements),
49 Effect.orElseSucceed(() => null),
50 ),
51 Schedule.spaced(AX_POLL_INTERVAL_MS),
52 ).pipe(Stream.filter((elements) => elements !== null)),
53 ).pipe(Atom.withLabel(`device-ax-elements:${target.deviceId}`)),
54);
55
56export interface AndroidFoldView {
57 readonly fold: AndroidFoldState | null;
58 /** Hinge angle the 3D phone shows, including an optimistic one while a change is in flight. */
59 readonly angle: number | null;
60}
61
62export const foldAngle = (fold: AndroidFoldState) =>
63 fold.hingeAngle ?? (fold.posture === "closed" ? 0 : 180);
64
65/**
66 * The fold controls and the 3D phone share one view of a device's fold. Keyed by
67 * host and device, not by hub access, so a renewed ticket keeps the known posture.
68 */
69export const androidFoldViewAtom = Atom.family((deviceKey: string) =>
70 Atom.make<AndroidFoldView>({ fold: null, angle: null }).pipe(
71 Atom.withLabel(`android-fold-view:${deviceKey}`),
72 ),
73);
74
75/**
76 * Reads the fold while mounted and records it in the device's fold view. The
77 * screen size is part of the key because folding resizes the screen, so a new
78 * size means the posture may have changed. An expired ticket stops retrying:
79 * renewed access is a new key and reads again.
80 */
81export const androidFoldReadAtom = Atom.family(
82 (input: {
83 readonly deviceKey: string;
84 readonly access: DeviceHubAccess;
85 readonly deviceId: string;
86 readonly screenWidth: number;
87 readonly screenHeight: number;
88 }) =>
89 Atom.make((get) =>
90 Effect.tryPromise({
91 try: (signal) => readAndroidFold(input.access, input.deviceId, signal),
92 catch: (cause) => ({ expired: cause instanceof AndroidFoldError && cause.status === 401 }),
93 }).pipe(
94 Effect.retry({
95 while: (failure) => !failure.expired,
96 schedule: Schedule.spaced(FOLD_RETRY_MS),
97 }),
98 Effect.tap((fold) =>
99 Effect.sync(() =>
100 get.set(androidFoldViewAtom(input.deviceKey), {
101 fold,
102 angle: fold.supported ? foldAngle(fold) : null,
103 }),
104 ),
105 ),
106 Effect.asVoid,
107 ),
108 ).pipe(Atom.withLabel(`android-fold-read:${input.deviceKey}`)),
109);
110
111export const NOOP_ATOM = Atom.make(AsyncResult.initial<void, never>()).pipe(
112 Atom.withLabel("device:noop"),
113);
114
115export const NO_FOREGROUND_ATOM = Atom.make<DeviceForegroundInfo | null | undefined>(
116 undefined,
117).pipe(Atom.withLabel("device-foreground:none"));