Skip to content

Commit e94c3a7

Browse files
authored
fix(server): streamed clicks no longer wait behind stale hovers (#22739)
1 parent 0070b27 commit e94c3a7

4 files changed

Lines changed: 348 additions & 3 deletions

File tree

‎apps/server/src/preview/ServerBrowser.test.ts‎

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -977,6 +977,133 @@ it.live(
977977
).pipe(Effect.provide(layer)),
978978
);
979979

980+
it.effect.each([
981+
{
982+
name: "hover burst before a click",
983+
inputs: [
984+
...Array.from({ length: 120 }, (_, x) => ({
985+
type: "mouse",
986+
action: "move",
987+
x,
988+
buttons: 0,
989+
button: "none",
990+
})),
991+
{ type: "mouse", action: "down", x: 119, buttons: 1, button: "left" },
992+
{ type: "mouse", action: "up", x: 119, buttons: 0, button: "left" },
993+
],
994+
expected: [
995+
{ type: "mouseMoved", x: -1 },
996+
{ type: "mouseMoved", x: 119 },
997+
{ type: "mousePressed", x: 119 },
998+
{ type: "mouseReleased", x: 119 },
999+
],
1000+
},
1001+
{
1002+
name: "modifier changes and drag points",
1003+
inputs: [
1004+
{ type: "mouse", action: "move", x: 1, buttons: 0, button: "none" },
1005+
{ type: "mouse", action: "move", x: 2, buttons: 0, button: "none", modifiers: 2 },
1006+
{ type: "mouse", action: "move", x: 3, buttons: 0, button: "none", modifiers: 2 },
1007+
{ type: "mouse", action: "down", x: 3, buttons: 1, button: "left" },
1008+
{ type: "mouse", action: "move", x: 4, buttons: 1, button: "left" },
1009+
{ type: "mouse", action: "move", x: 5, buttons: 1, button: "left" },
1010+
{ type: "mouse", action: "up", x: 5, buttons: 0, button: "left" },
1011+
],
1012+
expected: [
1013+
{ type: "mouseMoved", x: -1 },
1014+
{ type: "mouseMoved", x: 1, modifiers: 0 },
1015+
{ type: "mouseMoved", x: 3, modifiers: 2 },
1016+
{ type: "mousePressed", x: 3 },
1017+
{ type: "mouseMoved", x: 4 },
1018+
{ type: "mouseMoved", x: 5 },
1019+
{ type: "mouseReleased", x: 5 },
1020+
],
1021+
},
1022+
{
1023+
name: "wheel and keyboard boundaries",
1024+
inputs: [
1025+
{ type: "mouse", action: "move", x: 1, buttons: 0, button: "none" },
1026+
{ type: "wheel", x: 1, deltaY: 20 },
1027+
{ type: "wheel", x: 1, deltaY: 30 },
1028+
{ type: "mouse", action: "move", x: 2, buttons: 0, button: "none" },
1029+
{ type: "key", action: "down", key: "a", code: "KeyA", text: "a" },
1030+
{ type: "mouse", action: "move", x: 3, buttons: 0, button: "none" },
1031+
],
1032+
expected: [
1033+
{ type: "mouseMoved", x: -1 },
1034+
{ type: "mouseMoved", x: 1 },
1035+
{ type: "mouseWheel", deltaY: 20 },
1036+
{ type: "mouseWheel", deltaY: 30 },
1037+
{ type: "mouseMoved", x: 2 },
1038+
{ type: "keyDown", key: "a" },
1039+
{ type: "mouseMoved", x: 3 },
1040+
],
1041+
},
1042+
{
1043+
name: "text, navigation and resize boundaries",
1044+
inputs: [
1045+
{ type: "mouse", action: "move", x: 1, buttons: 0, button: "none" },
1046+
{ type: "text", text: "typed" },
1047+
{ type: "mouse", action: "move", x: 2, buttons: 0, button: "none" },
1048+
{ type: "navigate", url: "http://localhost:5173/next" },
1049+
{ type: "mouse", action: "move", x: 3, buttons: 0, button: "none" },
1050+
{ type: "resize", width: 640, height: 480 },
1051+
{ type: "mouse", action: "move", x: 4, buttons: 0, button: "none" },
1052+
],
1053+
expected: [
1054+
{ type: "mouseMoved", x: -1 },
1055+
{ type: "mouseMoved", x: 1 },
1056+
{ text: "typed" },
1057+
{ type: "mouseMoved", x: 2 },
1058+
{ type: "mouseMoved", x: 3 },
1059+
{ type: "mouseMoved", x: 4 },
1060+
],
1061+
},
1062+
{
1063+
name: "ambiguous button fields",
1064+
inputs: [
1065+
{ type: "mouse", action: "move", x: 1, button: "none" },
1066+
{ type: "mouse", action: "move", x: 2, button: "none" },
1067+
{ type: "mouse", action: "move", x: 3, buttons: 0 },
1068+
{ type: "mouse", action: "move", x: 4, buttons: 0 },
1069+
],
1070+
expected: [-1, 1, 2, 3, 4].map((x) => ({ type: "mouseMoved", x })),
1071+
},
1072+
])("orders streamed input correctly for $name", (testCase) =>
1073+
Effect.scoped(
1074+
Effect.gen(function* () {
1075+
const { browser, tabId } = yield* ready;
1076+
const viewer = yield* browser.attachViewer(viewerInput(tabId, true));
1077+
yield* viewer.input({ type: "takeControl" });
1078+
const session = contexts[0]!.sessions.at(-1)!;
1079+
const send = session.send.getMockImplementation()!;
1080+
const started = Promise.withResolvers<void>();
1081+
const finish = Promise.withResolvers<void>();
1082+
yield* Effect.addFinalizer(() => Effect.sync(() => finish.resolve()));
1083+
session.send.mockClear();
1084+
session.send.mockImplementationOnce(async (method, input) => {
1085+
started.resolve();
1086+
await finish.promise;
1087+
return send(method, input);
1088+
});
1089+
const running = yield* viewer
1090+
.input({ type: "mouse", action: "move", x: -1, button: "none", buttons: 0 })
1091+
.pipe(Effect.forkScoped({ startImmediately: true }));
1092+
yield* Effect.promise(() => started.promise);
1093+
const waiting = yield* Effect.forEach(testCase.inputs, (input) =>
1094+
viewer.input(input).pipe(Effect.forkScoped({ startImmediately: true })),
1095+
);
1096+
finish.resolve();
1097+
yield* Effect.forEach([running, ...waiting], Fiber.join);
1098+
const dispatched = session.send.mock.calls.flatMap(([method, input]) =>
1099+
method.startsWith("Input.") ? [input] : [],
1100+
);
1101+
expect(dispatched).toHaveLength(testCase.expected.length);
1102+
expect(dispatched).toMatchObject(testCase.expected);
1103+
}),
1104+
).pipe(Effect.provide(layer)),
1105+
);
1106+
9801107
it.live("only the controlling viewer's copy and cut keys reach its clipboard", () =>
9811108
Effect.scoped(
9821109
Effect.gen(function* () {

‎apps/server/src/preview/ServerBrowser.ts‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2550,9 +2550,17 @@ const make = Effect.gen(function* () {
25502550
: {}),
25512551
});
25522552
} else {
2553-
await tab.control.human(viewer.id, () =>
2554-
dispatchViewerInput(tab, session, viewer, message),
2555-
);
2553+
const dispatch = () => dispatchViewerInput(tab, session, viewer, message);
2554+
if (
2555+
message.type === "mouse" &&
2556+
message.action === "move" &&
2557+
message.button === "none" &&
2558+
message.buttons === 0
2559+
) {
2560+
await tab.control.humanLatest(viewer.id, `hover:${modifiersOf(message)}`, dispatch);
2561+
} else {
2562+
await tab.control.human(viewer.id, dispatch);
2563+
}
25562564
}
25572565
} catch {
25582566
broadcastControl(tab);

‎apps/server/src/preview/SessionControl.test.ts‎

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,193 @@ import { describe, expect, it } from "vite-plus/test";
33
import { BrowserControlInterrupted, SessionControl } from "./SessionControl.ts";
44

55
describe("SessionControl", () => {
6+
it("replaces waiting hover work without changing the running action or delaying the following click", async () => {
7+
const control = new SessionControl("agent");
8+
await control.take("viewer");
9+
const started = Promise.withResolvers<void>();
10+
const finish = Promise.withResolvers<void>();
11+
const events: Array<number | string> = [];
12+
const running = control.humanLatest("viewer", "hover:0", async () => {
13+
events.push("running");
14+
started.resolve();
15+
await finish.promise;
16+
});
17+
await started.promise;
18+
const moves = Array.from({ length: 120 }, (_, position) =>
19+
control.humanLatest("viewer", "hover:0", async () => {
20+
events.push(position);
21+
}),
22+
);
23+
const click = control.human("viewer", async () => {
24+
events.push("click");
25+
});
26+
finish.resolve();
27+
await Promise.all([running, ...moves, click]);
28+
expect(events).toEqual(["running", 119, "click"]);
29+
});
30+
31+
it.each(["human", "system", "modifiers"] as const)(
32+
"does not merge waiting hover work across a %s action",
33+
async (barrier) => {
34+
const control = new SessionControl("agent");
35+
await control.take("viewer");
36+
const finish = Promise.withResolvers<void>();
37+
const events: string[] = [];
38+
const running = control.human("viewer", () => finish.promise);
39+
const before = control.humanLatest("viewer", "hover:0", async () => {
40+
events.push("before");
41+
});
42+
const recordBarrier = async () => {
43+
events.push("barrier");
44+
};
45+
const between =
46+
barrier === "modifiers"
47+
? control.humanLatest("viewer", "hover:2", recordBarrier)
48+
: barrier === "system"
49+
? control.system(recordBarrier)
50+
: control.human("viewer", recordBarrier);
51+
const after = control.humanLatest("viewer", "hover:0", async () => {
52+
events.push("after");
53+
});
54+
finish.resolve();
55+
await Promise.all([running, before, between, after]);
56+
expect(events).toEqual(["before", "barrier", "after"]);
57+
},
58+
);
59+
60+
it("does not let another viewer replace or split the owner's waiting hover", async () => {
61+
const control = new SessionControl("agent");
62+
await control.take("viewer");
63+
const finish = Promise.withResolvers<void>();
64+
const events: string[] = [];
65+
const running = control.human("viewer", () => finish.promise);
66+
const stale = control.humanLatest("viewer", "hover", async () => {
67+
events.push("stale");
68+
});
69+
await expect(
70+
control.humanLatest("other", "hover", async () => {
71+
events.push("other");
72+
}),
73+
).rejects.toThrow(BrowserControlInterrupted);
74+
const latest = control.humanLatest("viewer", "hover", async () => {
75+
events.push("latest");
76+
});
77+
finish.resolve();
78+
await Promise.all([running, stale, latest]);
79+
expect(events).toEqual(["latest"]);
80+
});
81+
82+
it.each(["release", "disconnect"] as const)(
83+
"does not revive waiting hover work after %s and retaking control",
84+
async (handoff) => {
85+
const control = new SessionControl("agent");
86+
await control.take("viewer");
87+
const started = Promise.withResolvers<void>();
88+
const finish = Promise.withResolvers<void>();
89+
const events: string[] = [];
90+
const running = control.human("viewer", () => {
91+
started.resolve();
92+
return finish.promise;
93+
});
94+
await started.promise;
95+
const stale = expect(
96+
control.humanLatest("viewer", "hover", async () => {
97+
events.push("stale");
98+
}),
99+
).rejects.toThrow(BrowserControlInterrupted);
100+
const released = control[handoff]("viewer");
101+
const retaken = control.take("viewer");
102+
const fresh = control.humanLatest("viewer", "hover", async () => {
103+
events.push("fresh");
104+
});
105+
const latest = control.humanLatest("viewer", "hover", async () => {
106+
events.push("latest");
107+
});
108+
finish.resolve();
109+
await Promise.all([running, stale, released, retaken, fresh, latest]);
110+
expect(events).toEqual(["latest"]);
111+
},
112+
);
113+
114+
it("rejects every caller of a merged hover when the tab closes", async () => {
115+
const control = new SessionControl("agent");
116+
await control.take("viewer");
117+
const started = Promise.withResolvers<void>();
118+
const finish = Promise.withResolvers<void>();
119+
const events: string[] = [];
120+
const running = control.human("viewer", () => {
121+
started.resolve();
122+
return finish.promise;
123+
});
124+
await started.promise;
125+
const waiting = [1, 2].map((position) =>
126+
expect(
127+
control.humanLatest("viewer", "hover", async () => {
128+
events.push(`hover ${position}`);
129+
}),
130+
).rejects.toThrow("closed"),
131+
);
132+
const closed = control.close();
133+
await expect(
134+
control.humanLatest("viewer", "hover", async () => {
135+
events.push("after close");
136+
}),
137+
).rejects.toThrow("closed");
138+
finish.resolve();
139+
await Promise.all([running, ...waiting, closed]);
140+
expect(events).toEqual([]);
141+
});
142+
143+
it("shares a merged dispatch failure and accepts fresh hover work afterwards", async () => {
144+
const control = new SessionControl("agent");
145+
await control.take("viewer");
146+
const finish = Promise.withResolvers<void>();
147+
const events: string[] = [];
148+
const running = control.human("viewer", () => finish.promise);
149+
const first = expect(
150+
control.humanLatest("viewer", "hover", async () => {
151+
events.push("superseded");
152+
}),
153+
).rejects.toThrow("dispatch failed");
154+
const last = expect(
155+
control.humanLatest("viewer", "hover", async () => {
156+
throw new Error("dispatch failed");
157+
}),
158+
).rejects.toThrow("dispatch failed");
159+
finish.resolve();
160+
await Promise.all([running, first, last]);
161+
await control.humanLatest("viewer", "hover", async () => {
162+
events.push("recovered");
163+
});
164+
expect(events).toEqual(["recovered"]);
165+
});
166+
167+
it("waits for tracked navigation before dispatching the latest human hover", async () => {
168+
const control = new SessionControl("agent");
169+
await control.take("viewer");
170+
const committed = Promise.withResolvers<void>();
171+
const events: string[] = [];
172+
const navigation = control.human("viewer", async () => {
173+
control.track(
174+
committed.promise.then(() => {
175+
events.push("navigation committed");
176+
}),
177+
);
178+
return "started";
179+
});
180+
const first = control.humanLatest("viewer", "hover", async () => {
181+
events.push("superseded");
182+
});
183+
const latest = control.humanLatest("viewer", "hover", async () => {
184+
events.push("latest");
185+
});
186+
await expect(navigation).resolves.toBe("started");
187+
expect(events).toEqual([]);
188+
committed.resolve();
189+
await Promise.all([first, latest]);
190+
expect(events).toEqual(["navigation committed", "latest"]);
191+
});
192+
6193
it("returns an action result before tracked navigation but drains it before already queued actions", async () => {
7194
const control = new SessionControl("agent");
8195
const committed = Promise.withResolvers<void>();

‎apps/server/src/preview/SessionControl.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ export class SessionControl {
2222
readonly agentId: string | null;
2323
private readonly onGenerationChange: () => void;
2424
private tail: Promise<unknown> = Promise.resolve();
25+
private latestHuman: {
26+
entry: { viewerId: string; key: string; run: () => Promise<void> };
27+
result: Promise<void>;
28+
} | null = null;
2529
private pending: Promise<void> = Promise.resolve();
2630
private readonly handoff = new Set<Promise<void>>();
2731
private owner: string | null = null;
@@ -42,6 +46,7 @@ export class SessionControl {
4246
}
4347

4448
private enqueue<A>(run: () => Promise<A>): Promise<A> {
49+
this.latestHuman = null;
4550
const result = this.tail.then(run);
4651
this.tail = result.then(
4752
() => this.pending,
@@ -68,6 +73,7 @@ export class SessionControl {
6873
}
6974

7075
private changeOwner(owner: string | null) {
76+
this.latestHuman = null;
7177
this.owner = owner;
7278
this.epoch++;
7379
this.onGenerationChange();
@@ -128,6 +134,23 @@ export class SessionControl {
128134
return this.action(() => this.owner === viewerId, run);
129135
}
130136

137+
/** Replace only adjacent waiting work; the running action and intervening actions keep their order. */
138+
humanLatest(viewerId: string, key: string, run: () => Promise<void>): Promise<void> {
139+
if (this.closed || this.owner !== viewerId) return this.human(viewerId, run);
140+
const latest = this.latestHuman;
141+
if (latest?.entry.viewerId === viewerId && latest.entry.key === key) {
142+
latest.entry.run = run;
143+
return latest.result;
144+
}
145+
const entry = { viewerId, key, run };
146+
const result = this.human(viewerId, async () => {
147+
if (this.latestHuman?.entry === entry) this.latestHuman = null;
148+
await entry.run();
149+
});
150+
this.latestHuman = { entry, result };
151+
return result;
152+
}
153+
131154
async take(viewerId: string) {
132155
this.assertOpen();
133156
if (this.owner !== null && this.owner !== viewerId) {

0 commit comments

Comments
 (0)