Skip to content

Commit c0bc248

Browse files
refactor(client): drop unused options left for future callers
The cloud account service, VCS status revalidation, and the shortcut dispatcher carried options no caller on main uses: account preparation, rejected-activation retry, sign-out transition hook, and per-transition flags; VCS wake signals with debounce; and element-scoped command registration. Remove them and the tests that only exercised them. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent f898557 commit c0bc248

6 files changed

Lines changed: 85 additions & 321 deletions

File tree

‎apps/web/src/shortcuts/dispatcher.browser.test.ts‎

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
import type { KeybindingCommand, ResolvedKeybindingsConfig } from "@t3tools/contracts";
22
import { describe, expect, it, vi } from "vite-plus/test";
33

4-
import {
5-
commandScopeRef,
6-
dispatchMenuAction,
7-
registerCommands,
8-
registerMenuAction,
9-
} from "./dispatcher";
4+
import { dispatchMenuAction, registerCommands, registerMenuAction } from "./dispatcher";
105

116
function bindings(entries: ReadonlyArray<[string, KeybindingCommand]>): ResolvedKeybindingsConfig {
127
return entries.map(([key, command]) => ({
@@ -34,19 +29,15 @@ function press(key: string): KeyboardEvent {
3429
return event;
3530
}
3631

37-
function mountScope(...args: Parameters<typeof commandScopeRef>): () => void {
38-
return commandScopeRef(...args)(document.body) ?? (() => {});
39-
}
40-
4132
describe("command dispatcher", () => {
4233
it("runs the newest scope's handler and nothing below it", () => {
4334
const global = vi.fn();
4435
const outer = vi.fn();
4536
const inner = vi.fn();
4637
using stack = new DisposableStack();
4738
stack.defer(registerCommands({ "chat.new": global }, { keybindings }));
48-
stack.defer(mountScope({ "chat.new": outer }, { keybindings }));
49-
stack.defer(mountScope({ "chat.new": inner }, { keybindings }));
39+
stack.defer(registerCommands({ "chat.new": outer }, { keybindings }));
40+
stack.defer(registerCommands({ "chat.new": inner }, { keybindings }));
5041

5142
press("k");
5243

@@ -61,9 +52,9 @@ describe("command dispatcher", () => {
6152
const guarded = vi.fn();
6253
using stack = new DisposableStack();
6354
stack.defer(registerCommands({ "chat.new": global }, { keybindings }));
64-
stack.defer(mountScope({ "chat.new": declined }, { keybindings }));
65-
stack.defer(mountScope({ "chat.newLocal": vi.fn() }, { keybindings }));
66-
stack.defer(mountScope({ "chat.new": guarded }, { keybindings, when: () => false }));
55+
stack.defer(registerCommands({ "chat.new": declined }, { keybindings }));
56+
stack.defer(registerCommands({ "chat.newLocal": vi.fn() }, { keybindings }));
57+
stack.defer(registerCommands({ "chat.new": guarded }, { keybindings, when: () => false }));
6758

6859
press("k");
6960

@@ -72,33 +63,20 @@ describe("command dispatcher", () => {
7263
expect(global).toHaveBeenCalledOnce();
7364
});
7465

75-
it("element scopes win over global commands registered after them", () => {
76-
const element = vi.fn();
77-
const global = vi.fn();
78-
using stack = new DisposableStack();
79-
stack.defer(mountScope({ "chat.new": element }, { keybindings }));
80-
stack.defer(registerCommands({ "chat.new": global }, { keybindings }));
81-
82-
press("k");
83-
84-
expect(element).toHaveBeenCalledOnce();
85-
expect(global).not.toHaveBeenCalled();
86-
});
87-
8866
it("stops once a handler prevents default, and unregisters on cleanup", () => {
8967
const lower = vi.fn();
9068
const upper = vi.fn((event: KeyboardEvent) => {
9169
event.preventDefault();
9270
return false;
9371
});
9472
using stack = new DisposableStack();
95-
stack.defer(mountScope({ "chat.new": lower }, { keybindings }));
96-
const unmountUpper = mountScope({ "chat.new": upper }, { keybindings });
73+
stack.defer(registerCommands({ "chat.new": lower }, { keybindings }));
74+
const unregisterUpper = registerCommands({ "chat.new": upper }, { keybindings });
9775

9876
press("k");
9977
expect(lower).not.toHaveBeenCalled();
10078

101-
unmountUpper();
79+
unregisterUpper();
10280
press("k");
10381
expect(upper).toHaveBeenCalledOnce();
10482
expect(lower).toHaveBeenCalledOnce();
@@ -108,7 +86,7 @@ describe("command dispatcher", () => {
10886
const onEscape = vi.fn((event: KeyboardEvent) => (event.key === "Escape" ? undefined : false));
10987
const command = vi.fn();
11088
using stack = new DisposableStack();
111-
stack.defer(mountScope({ "chat.new": command }, { keybindings, onKeyDown: onEscape }));
89+
stack.defer(registerCommands({ "chat.new": command }, { keybindings, onKeyDown: onEscape }));
11290

11391
press("Escape");
11492
press("k");
@@ -123,7 +101,9 @@ describe("command dispatcher", () => {
123101
stack.defer(
124102
registerCommands({}, { observe: (_event, consumed) => calls.push(`observe:${consumed}`) }),
125103
);
126-
stack.defer(mountScope({ "chat.new": () => void calls.push("command") }, { keybindings }));
104+
stack.defer(
105+
registerCommands({ "chat.new": () => void calls.push("command") }, { keybindings }),
106+
);
127107

128108
press("k");
129109
press("x");

‎apps/web/src/shortcuts/dispatcher.ts‎

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ export interface CommandScopeOptions {
2222
/** For shortcuts that are not keybinding commands (Escape, "/"); runs before command lookup. */
2323
readonly onKeyDown?: CommandHandler;
2424
/**
25-
* App-global only: run in the capture phase, ahead of focused widgets that consume the key
25+
* Run in the capture phase, ahead of focused widgets that consume the key
2626
* themselves (the terminal encodes Ctrl+Shift+T and stops propagation).
2727
*/
2828
readonly capture?: boolean;
2929
/**
30-
* App-global only: hear every bubbling keydown after the scopes ran, with whether one of them
30+
* Hear every bubbling keydown after the scopes ran, with whether one of them
3131
* (or the page) consumed it. Observers never consume. For listeners that react to keys other
3232
* scopes may also handle, such as scroll keys.
3333
*/
@@ -39,8 +39,7 @@ interface CommandScope {
3939
readonly options: CommandScopeOptions;
4040
}
4141

42-
// Element scopes sit above app-global scopes; within each list the newest wins.
43-
const elementScopes: CommandScope[] = [];
42+
// Within each list the newest scope wins.
4443
const globalScopes: CommandScope[] = [];
4544
const captureScopes: CommandScope[] = [];
4645
const observerScopes: CommandScope[] = [];
@@ -81,8 +80,7 @@ function dispatchCaptureKeyDown(event: KeyboardEvent): void {
8180
}
8281

8382
function dispatchKeyDown(event: KeyboardEvent): void {
84-
const consumed =
85-
runScopes(elementScopes, event) || runScopes(globalScopes, event) || event.defaultPrevented;
83+
const consumed = runScopes(globalScopes, event) || event.defaultPrevented;
8684
for (const scope of observerScopes.slice()) scope.options.observe?.(event, consumed);
8785
}
8886

@@ -112,18 +110,6 @@ export function registerCommands(
112110
return pushScope(scopes, { commands, options });
113111
}
114112

115-
/**
116-
* Ref callback that keeps a command scope registered while its element is
117-
* mounted. Handlers should read changing values from stores at event time so
118-
* the callback stays stable across renders.
119-
*/
120-
export function commandScopeRef(
121-
commands: CommandHandlers,
122-
options: CommandScopeOptions = {},
123-
): (node: Element | null) => (() => void) | undefined {
124-
return (node) => (node === null ? undefined : pushScope(elementScopes, { commands, options }));
125-
}
126-
127113
type MenuActionHandler = () => void;
128114
const menuActionHandlers = new Map<string, MenuActionHandler[]>();
129115
let menuActionInstalled = false;

‎packages/client-runtime/src/relay/cloudAccountService.test.ts‎

Lines changed: 29 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,17 @@ function makeService() {
2626
const log: string[] = [];
2727
const activated: Array<PromiseWithResolvers<void>> = [];
2828
const cleanedUp: Array<PromiseWithResolvers<void>> = [];
29-
const gates = new Map<
30-
string,
31-
{ readonly started: PromiseWithResolvers<void>; readonly release: PromiseWithResolvers<void> }
32-
>();
29+
const cleanUpGates = new Map<string, PromiseWithResolvers<void>>();
3330
const service = createCloudAccountService<CloudAccountSource>({
3431
deactivate: () => log.push("deactivate"),
3532
detach: () => log.push("detach"),
36-
onSignOutTransition: () => log.push("signOutTransition"),
37-
cleanUp: async ({ accountId, wasActive }) => {
38-
log.push(`cleanUp:${accountId}:${wasActive}`);
33+
cleanUp: async ({ accountId }) => {
34+
log.push(`cleanUp:${accountId}`);
35+
await cleanUpGates.get(accountId ?? "")?.promise;
3936
cleanedUp.shift()?.resolve();
4037
},
41-
prepare: async ({ accountId }) => {
42-
log.push(`prepare:${accountId}`);
43-
const gate = gates.get(accountId);
44-
gate?.started.resolve();
45-
await gate?.release.promise;
46-
},
47-
activate: ({ accountId, isAccountTransition }) => {
48-
log.push(`activate:${accountId}:${isAccountTransition}`);
38+
activate: ({ accountId }) => {
39+
log.push(`activate:${accountId}`);
4940
activated.shift()?.resolve();
5041
},
5142
});
@@ -59,29 +50,24 @@ function makeService() {
5950
cleanedUp.push(deferred);
6051
return deferred.promise;
6152
};
62-
const gate = (accountId: string) => {
63-
const deferred = {
64-
started: Promise.withResolvers<void>(),
65-
release: Promise.withResolvers<void>(),
66-
};
67-
gates.set(accountId, deferred);
53+
const holdCleanUp = (accountId: string) => {
54+
const deferred = Promise.withResolvers<void>();
55+
cleanUpGates.set(accountId, deferred);
6856
return deferred;
6957
};
70-
return { service, log, nextActivation, nextCleanUp, gate };
58+
return { service, log, nextActivation, nextCleanUp, holdCleanUp };
7159
}
7260

7361
describe("planCloudAccountStep", () => {
74-
it("treats the first observation as a non-transition", () => {
62+
it("plans the first observation from no previous account", () => {
7563
expect(planCloudAccountStep(undefined, "a")).toEqual({
7664
_tag: "SignIn",
7765
accountId: "a",
7866
switchFromAccountId: null,
79-
isAccountTransition: false,
8067
});
8168
expect(planCloudAccountStep(undefined, null)).toEqual({
8269
_tag: "SignOut",
8370
previousAccountId: null,
84-
isAccountTransition: false,
8571
});
8672
});
8773

@@ -92,7 +78,6 @@ describe("planCloudAccountStep", () => {
9278
_tag: "SignIn",
9379
accountId: "b",
9480
switchFromAccountId: "a",
95-
isAccountTransition: true,
9681
});
9782
});
9883
});
@@ -104,15 +89,14 @@ describe("createCloudAccountService", () => {
10489
service.attach(source);
10590

10691
emit(undefined);
107-
expect(service.getState()).toEqual({ _tag: "Unobserved" });
92+
expect(log).toEqual([]);
10893
const activation = nextActivation();
10994
emit({ id: "a" });
11095
emit({ id: "a" });
111-
expect(service.getState()).toEqual({ _tag: "Activating", accountId: "a" });
11296
await activation;
97+
emit({ id: "a" });
11398

114-
expect(service.getState()).toEqual({ _tag: "Active", accountId: "a" });
115-
expect(log).toEqual(["prepare:a", "activate:a:false"]);
99+
expect(log).toEqual(["activate:a"]);
116100
});
117101

118102
it("drops credentials synchronously and cleans up before activating the next account", async () => {
@@ -129,7 +113,7 @@ describe("createCloudAccountService", () => {
129113
expect(log).toEqual(["deactivate"]);
130114
await second;
131115

132-
expect(log).toEqual(["deactivate", "cleanUp:a:true", "prepare:b", "activate:b:true"]);
116+
expect(log).toEqual(["deactivate", "cleanUp:a", "activate:b"]);
133117
});
134118

135119
it("cleans up on sign-out, including a signed-out first observation", async () => {
@@ -140,7 +124,7 @@ describe("createCloudAccountService", () => {
140124
const initialCleanUp = nextCleanUp();
141125
emit(null);
142126
await initialCleanUp;
143-
expect(log).toEqual(["deactivate", "cleanUp:null:false"]);
127+
expect(log).toEqual(["deactivate", "cleanUp:null"]);
144128

145129
const activation = nextActivation();
146130
emit({ id: "a" });
@@ -150,70 +134,27 @@ describe("createCloudAccountService", () => {
150134
const cleanUp = nextCleanUp();
151135
emit(null);
152136
await cleanUp;
153-
expect(service.getState()).toEqual({ _tag: "SignedOut" });
154-
expect(log).toEqual(["signOutTransition", "deactivate", "cleanUp:a:true"]);
137+
expect(log).toEqual(["deactivate", "cleanUp:a"]);
155138
});
156139

157140
it("never activates an account that signed out while it was activating", async () => {
158-
const { service, log, nextCleanUp, gate } = makeService();
141+
const { service, log, nextActivation, nextCleanUp, holdCleanUp } = makeService();
159142
const { source, emit } = makeSource();
160143
service.attach(source);
161-
const prepared = gate("a");
144+
const first = nextActivation();
162145
emit({ id: "a" });
163-
await prepared.started.promise;
164-
const cleanUp = nextCleanUp();
165-
emit(null);
166-
prepared.release.resolve();
167-
await cleanUp;
168-
169-
expect(service.getState()).toEqual({ _tag: "SignedOut" });
170-
expect(log).toEqual(["prepare:a", "signOutTransition", "deactivate", "cleanUp:a:false"]);
171-
});
146+
await first;
147+
log.length = 0;
172148

173-
it("retries a rejected activation on the next emission for the same account", async () => {
174-
const log: string[] = [];
175-
const failed = Promise.withResolvers<void>();
176-
const activated = Promise.withResolvers<void>();
177-
let attempts = 0;
178-
const service = createCloudAccountService<CloudAccountSource>({
179-
deactivate: () => log.push("deactivate"),
180-
detach: () => log.push("detach"),
181-
cleanUp: async ({ accountId, wasActive }) => {
182-
log.push(`cleanUp:${accountId}:${wasActive}`);
183-
},
184-
prepare: async ({ accountId }) => {
185-
attempts += 1;
186-
log.push(`prepare:${accountId}`);
187-
if (attempts === 1) {
188-
// Queued before the rejection, so the service's catch runs before
189-
// the test's `await failed.promise` continuation.
190-
queueMicrotask(failed.resolve);
191-
throw new Error("restore failed");
192-
}
193-
},
194-
activate: ({ accountId, isAccountTransition }) => {
195-
log.push(`activate:${accountId}:${isAccountTransition}`);
196-
activated.resolve();
197-
},
198-
});
199-
const { source, emit } = makeSource();
200-
service.attach(source);
149+
const heldCleanUp = holdCleanUp("a");
150+
emit({ id: "b" });
201151
emit(null);
202-
emit({ id: "a" });
203-
await failed.promise;
204-
expect(service.getState()).toMatchObject({ _tag: "Failed", accountId: "a" });
152+
void nextCleanUp();
153+
const signOutCleanUp = nextCleanUp();
154+
heldCleanUp.resolve();
155+
await signOutCleanUp;
205156

206-
emit({ id: "a" });
207-
await activated.promise;
208-
209-
expect(service.getState()).toEqual({ _tag: "Active", accountId: "a" });
210-
expect(log).toEqual([
211-
"deactivate",
212-
"cleanUp:null:false",
213-
"prepare:a",
214-
"prepare:a",
215-
"activate:a:true",
216-
]);
157+
expect(log).toEqual(["deactivate", "deactivate", "cleanUp:a", "cleanUp:b"]);
217158
});
218159

219160
it("detaches from a replaced Clerk instance", () => {
@@ -238,7 +179,6 @@ describe("createCloudAccountService", () => {
238179

239180
detach();
240181
expect(isSubscribed()).toBe(false);
241-
expect(service.getState()).toEqual({ _tag: "Unobserved" });
242-
expect(log).toEqual(["prepare:a", "activate:a:false", "detach"]);
182+
expect(log).toEqual(["activate:a", "detach"]);
243183
});
244184
});

0 commit comments

Comments
 (0)