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

duoControl.ts

113 lines3.6 KB

1// @effect-diagnostics globalTimers:off - The stream owns this browser control queue and its timeout.
2export const DUO_POSES = [
3 { id: "closed", label: "Closed", angle: 0 },
4 { id: "book", label: "Book", angle: 90 },
5 { id: "open", label: "Open", angle: 180 },
6 { id: "laptop", label: "Laptop", angle: 90 },
7 { id: "tent", label: "Tent", angle: 80 },
8] as const;
9export type DuoPose = (typeof DUO_POSES)[number]["id"];
10export type DuoOrientation =
11 | "portrait"
12 | "landscape_left"
13 | "portrait_upside_down"
14 | "landscape_right";
15export type DuoCommand =
16 | { control: "angle"; value: number }
17 | { control: "pose"; value: DuoPose }
18 | { control: "table"; value: boolean }
19 | { control: "physical"; value: "faceup" | "facedown" }
20 | { control: "orientation"; value: DuoOrientation };
21export type DuoControlState = {
22 pending: boolean;
23 requested: DuoCommand | null;
24 error: string | null;
25};
26​
27/** One in-flight native transaction. Hinge motion coalesces; presets replace queued motion. Nothing replays after reconnect. */
28export function createDuoControl(options: {
29 send: (request: { requestId: number; command: DuoCommand }) => boolean;
30 onChange: (state: DuoControlState) => void;
31 timeoutMs?: number;
32}) {
33 let nextId = 1;
34 let active: { requestId: number; command: DuoCommand } | null = null;
35 let queued: DuoCommand | null = null;
36 let timer: ReturnType<typeof setTimeout> | null = null;
37 const publish = (error: string | null = null) =>
38 options.onChange({ pending: !!active, requested: queued ?? active?.command ?? null, error });
39 const clear = (error: string | null = null) => {
40 if (timer) clearTimeout(timer);
41 timer = null;
42 active = null;
43 queued = null;
44 publish(error);
45 };
46 const drain = () => {
47 if (active || !queued) return;
48 active = { requestId: nextId++, command: queued };
49 queued = null;
50 const id = active.requestId;
51 timer = setTimeout(() => {
52 if (active?.requestId === id) clear("Device control timed out. Its position is unknown.");
53 }, options.timeoutMs ?? 5_000);
54 publish();
55 if (!options.send(active)) clear("Device is disconnected.");
56 };
57 return {
58 enqueue(command: DuoCommand) {
59 if (
60 command.control === "angle" &&
61 (!Number.isFinite(command.value) || command.value < 0 || command.value > 180)
62 )
63 return;
64 queued = command;
65 if (active) publish();
66 else drain();
67 },
68 receive(reply: { requestId: number; ok: boolean; error?: string }) {
69 if (!active || reply.requestId !== active.requestId) return;
70 if (timer) clearTimeout(timer);
71 timer = null;
72 active = null;
73 if (!reply.ok) {
74 clear(reply.error ?? "Device control failed. Its position is unknown.");
75 return;
76 }
77 if (queued) drain();
78 else publish();
79 },
80 clear,
81 };
83​
84/** A pinch keeps its own accumulator across asynchronous native acknowledgements. */
85export function createDuoPinch(options: {
86 angle: () => number;
87 contains: (x: number, y: number) => boolean;
88 change: (angle: number | null) => void;
89}) {
90 let angle: number | null = null;
91 return {
92 begin(x: number, y: number) {
93 if (!options.contains(x, y)) return false;
94 angle = Math.max(0, Math.min(180, options.angle()));
95 return true;
96 },
97 move(logScale: number) {
98 if (angle === null || !Number.isFinite(logScale)) return;
99 const next = Math.max(0, Math.min(180, angle + logScale * 120));
100 if (next === angle) return;
101 angle = next;
102 options.change(next);
103 },
104 end() {
105 if (angle === null) return;
106 angle = null;
107 options.change(null);
108 },
109 get active() {
110 return angle !== null;
111 },
112 };