TypeScript
refactor(client): drop the VCS wake and dispatcher element scope options nobody uses (#25152)
Julius Marminge7a95549b9474
diff --git a/apps/web/src/shortcuts/dispatcher.browser.test.ts b/apps/web/src/shortcuts/dispatcher.browser.test.tsindex 308b26fdf26..377f3d3f107 100644--- a/apps/web/src/shortcuts/dispatcher.browser.test.ts+++ b/apps/web/src/shortcuts/dispatcher.browser.test.ts@@ -1,12 +1,7 @@import type { KeybindingCommand, ResolvedKeybindingsConfig } from "@t3tools/contracts";import { describe, expect, it, vi } from "vite-plus/test";-import {- commandScopeRef,- dispatchMenuAction,- registerCommands,- registerMenuAction,-} from "./dispatcher";+import { dispatchMenuAction, registerCommands, registerMenuAction } from "./dispatcher";function bindings(entries: ReadonlyArray<[string, KeybindingCommand]>): ResolvedKeybindingsConfig {return entries.map(([key, command]) => ({@@ -34,10 +29,6 @@ function press(key: string): KeyboardEvent {return event;}-function mountScope(...args: Parameters<typeof commandScopeRef>): () => void {- return commandScopeRef(...args)(document.body) ?? (() => {});-}-describe("command dispatcher", () => {it("runs the newest scope's handler and nothing below it", () => {const global = vi.fn();@@ -45,8 +36,8 @@ describe("command dispatcher", () => {const inner = vi.fn();using stack = new DisposableStack();stack.defer(registerCommands({ "chat.new": global }, { keybindings }));- stack.defer(mountScope({ "chat.new": outer }, { keybindings }));- stack.defer(mountScope({ "chat.new": inner }, { keybindings }));+ stack.defer(registerCommands({ "chat.new": outer }, { keybindings }));+ stack.defer(registerCommands({ "chat.new": inner }, { keybindings }));press("k");@@ -61,9 +52,9 @@ describe("command dispatcher", () => {const guarded = vi.fn();using stack = new DisposableStack();stack.defer(registerCommands({ "chat.new": global }, { keybindings }));- stack.defer(mountScope({ "chat.new": declined }, { keybindings }));- stack.defer(mountScope({ "chat.newLocal": vi.fn() }, { keybindings }));- stack.defer(mountScope({ "chat.new": guarded }, { keybindings, when: () => false }));+ stack.defer(registerCommands({ "chat.new": declined }, { keybindings }));+ stack.defer(registerCommands({ "chat.newLocal": vi.fn() }, { keybindings }));+ stack.defer(registerCommands({ "chat.new": guarded }, { keybindings, when: () => false }));press("k");@@ -72,19 +63,6 @@ describe("command dispatcher", () => {expect(global).toHaveBeenCalledOnce();});- it("element scopes win over global commands registered after them", () => {- const element = vi.fn();- const global = vi.fn();- using stack = new DisposableStack();- stack.defer(mountScope({ "chat.new": element }, { keybindings }));- stack.defer(registerCommands({ "chat.new": global }, { keybindings }));-- press("k");-- expect(element).toHaveBeenCalledOnce();- expect(global).not.toHaveBeenCalled();- });-it("stops once a handler prevents default, and unregisters on cleanup", () => {const lower = vi.fn();const upper = vi.fn((event: KeyboardEvent) => {@@ -92,13 +70,13 @@ describe("command dispatcher", () => {return false;});using stack = new DisposableStack();- stack.defer(mountScope({ "chat.new": lower }, { keybindings }));- const unmountUpper = mountScope({ "chat.new": upper }, { keybindings });+ stack.defer(registerCommands({ "chat.new": lower }, { keybindings }));+ const unregisterUpper = registerCommands({ "chat.new": upper }, { keybindings });press("k");expect(lower).not.toHaveBeenCalled();- unmountUpper();+ unregisterUpper();press("k");expect(upper).toHaveBeenCalledOnce();expect(lower).toHaveBeenCalledOnce();@@ -108,7 +86,7 @@ describe("command dispatcher", () => {const onEscape = vi.fn((event: KeyboardEvent) => (event.key === "Escape" ? undefined : false));const command = vi.fn();using stack = new DisposableStack();- stack.defer(mountScope({ "chat.new": command }, { keybindings, onKeyDown: onEscape }));+ stack.defer(registerCommands({ "chat.new": command }, { keybindings, onKeyDown: onEscape }));press("Escape");press("k");@@ -123,7 +101,9 @@ describe("command dispatcher", () => {stack.defer(registerCommands({}, { observe: (_event, consumed) => calls.push(`observe:${consumed}`) }),);- stack.defer(mountScope({ "chat.new": () => void calls.push("command") }, { keybindings }));+ stack.defer(+ registerCommands({ "chat.new": () => void calls.push("command") }, { keybindings }),+ );press("k");press("x");diff --git a/apps/web/src/shortcuts/dispatcher.ts b/apps/web/src/shortcuts/dispatcher.tsindex bb5b13a9980..c5ef4274aed 100644--- a/apps/web/src/shortcuts/dispatcher.ts+++ b/apps/web/src/shortcuts/dispatcher.ts@@ -22,12 +22,12 @@ export interface CommandScopeOptions {/** For shortcuts that are not keybinding commands (Escape, "/"); runs before command lookup. */readonly onKeyDown?: CommandHandler;/**- * App-global only: run in the capture phase, ahead of focused widgets that consume the key+ * Run in the capture phase, ahead of focused widgets that consume the key* themselves (the terminal encodes Ctrl+Shift+T and stops propagation).*/readonly capture?: boolean;/**- * App-global only: hear every bubbling keydown after the scopes ran, with whether one of them+ * Hear every bubbling keydown after the scopes ran, with whether one of them* (or the page) consumed it. Observers never consume. For listeners that react to keys other* scopes may also handle, such as scroll keys.*/@@ -39,8 +39,7 @@ interface CommandScope {readonly options: CommandScopeOptions;}-// Element scopes sit above app-global scopes; within each list the newest wins.-const elementScopes: CommandScope[] = [];+// Within each list the newest scope wins.const globalScopes: CommandScope[] = [];const captureScopes: CommandScope[] = [];const observerScopes: CommandScope[] = [];@@ -81,8 +80,7 @@ function dispatchCaptureKeyDown(event: KeyboardEvent): void {}function dispatchKeyDown(event: KeyboardEvent): void {- const consumed =- runScopes(elementScopes, event) || runScopes(globalScopes, event) || event.defaultPrevented;+ const consumed = runScopes(globalScopes, event) || event.defaultPrevented;for (const scope of observerScopes.slice()) scope.options.observe?.(event, consumed);}@@ -112,18 +110,6 @@ export function registerCommands(return pushScope(scopes, { commands, options });}-/**- * Ref callback that keeps a command scope registered while its element is- * mounted. Handlers should read changing values from stores at event time so- * the callback stays stable across renders.- */-export function commandScopeRef(- commands: CommandHandlers,- options: CommandScopeOptions = {},-): (node: Element | null) => (() => void) | undefined {- return (node) => (node === null ? undefined : pushScope(elementScopes, { commands, options }));-}-type MenuActionHandler = () => void;const menuActionHandlers = new Map<string, MenuActionHandler[]>();let menuActionInstalled = false;diff --git a/packages/client-runtime/src/state/vcsStatusRevalidation.test.ts b/packages/client-runtime/src/state/vcsStatusRevalidation.test.tsindex 9a1d8c3dd27..ed2b7f56acf 100644--- a/packages/client-runtime/src/state/vcsStatusRevalidation.test.ts+++ b/packages/client-runtime/src/state/vcsStatusRevalidation.test.ts@@ -6,28 +6,15 @@ import { createVcsStatusRevalidationAtomFamily } from "./vcsStatusRevalidation.tconst environmentId = EnvironmentId.make("environment-1");-function setup(options: { readonly connected?: boolean; readonly wake?: boolean }) {- const connectedAtom = Atom.make(options.connected ?? false);- const wakes = new Set<() => void>();+function setup(options: { readonly connected: boolean }) {+ const connectedAtom = Atom.make(options.connected);const run = vi.fn(async () => AsyncResult.success(null));const family = createVcsStatusRevalidationAtomFamily({refreshStatus: { label: "refresh-status", run },- ...(options.connected === undefined ? {} : { connected: () => connectedAtom }),- ...(options.wake- ? {- subscribeWake: (wake: () => void) => {- wakes.add(wake);- return () => wakes.delete(wake);- },- wakeDebounceMs: 250,- }- : {}),+ connected: () => connectedAtom,});const registry = AtomRegistry.make();- const wake = () => {- for (const listener of wakes) listener();- };- return { registry, family, connectedAtom, run, wakes, wake };+ return { registry, family, connectedAtom, run };}describe("createVcsStatusRevalidationAtomFamily", () => {@@ -59,35 +46,9 @@ describe("createVcsStatusRevalidationAtomFamily", () => {unmount();});- it("debounces wake signals and stops listening with the last subscriber", () => {- vi.useFakeTimers();- try {- const { registry, family, run, wakes, wake } = setup({ wake: true });- const unmount = registry.mount(family({ environmentId, cwd: "/repo" }));- expect(run).not.toHaveBeenCalled();-- wake();- vi.advanceTimersByTime(100);- wake();- vi.advanceTimersByTime(249);- expect(run).not.toHaveBeenCalled();- vi.advanceTimersByTime(1);- expect(run).toHaveBeenCalledTimes(1);-- wake();- unmount();- vi.runAllTimers();- expect(run).toHaveBeenCalledTimes(1);- expect(wakes.size).toBe(0);- } finally {- vi.useRealTimers();- }- });-it("does nothing for an idle target", () => {- const { registry, family, run, wakes } = setup({ connected: true, wake: true });+ const { registry, family, run } = setup({ connected: true });registry.mount(family(null))();expect(run).not.toHaveBeenCalled();- expect(wakes.size).toBe(0);});});diff --git a/packages/client-runtime/src/state/vcsStatusRevalidation.ts b/packages/client-runtime/src/state/vcsStatusRevalidation.tsindex b248d280c0d..bb3cc43a875 100644--- a/packages/client-runtime/src/state/vcsStatusRevalidation.ts+++ b/packages/client-runtime/src/state/vcsStatusRevalidation.ts@@ -1,4 +1,3 @@-// @effect-diagnostics globalTimers:off - The wake debounce runs in an atom subscription, outside an Effect runtime.import type { EnvironmentId } from "@t3tools/contracts";import { Atom, type AtomRegistry } from "effect/reactivity";@@ -10,10 +9,9 @@ interface VcsStatusRevalidationTarget {}/**- * One atom per environment and cwd that refreshes VCS status while mounted and- * shared by every subscriber. Refreshes only when `connected` is given and true- * (on mount and on each reconnect), and on `subscribeWake` (debounced); the wake- * subscription and timer end with the last subscriber.+ * One atom per environment and cwd, shared by every subscriber, that refreshes+ * VCS status while mounted: on mount and on each reconnect, once `connected`+ * is true.*/export function createVcsStatusRevalidationAtomFamily<R>(input: {readonly refreshStatus: AtomCommand<@@ -21,9 +19,7 @@ export function createVcsStatusRevalidationAtomFamily<R>(input: {R,unknown>;- readonly connected?: (environmentId: EnvironmentId) => Atom.Atom<boolean>;- readonly subscribeWake?: (wake: () => void) => () => void;- readonly wakeDebounceMs?: number;+ readonly connected: (environmentId: EnvironmentId) => Atom.Atom<boolean>;readonly onRefreshed?: (result: AtomCommandResult<R, unknown>) => void;}) {const family = Atom.family((target: VcsStatusRevalidationTarget) => {@@ -40,29 +36,9 @@ export function createVcsStatusRevalidationAtomFamily<R>(input: {input.onRefreshed?.(result);};- const wakeAtom = Atom.make((get) => {- const subscribeWake = input.subscribeWake;- if (subscribeWake === undefined) return;- const registry = get.registry;- let timer: ReturnType<typeof setTimeout> | undefined;- const unsubscribe = subscribeWake(() => {- if (timer !== undefined) clearTimeout(timer);- timer = setTimeout(() => {- timer = undefined;- void refresh(registry);- }, input.wakeDebounceMs ?? 0);- });- get.addFinalizer(() => {- if (timer !== undefined) clearTimeout(timer);- unsubscribe();- });- }).pipe(Atom.withLabel(`vcs-status-revalidation:wake:${key}`));-return Atom.make((get) => {- get(wakeAtom);- const connected = input.connected;// Rebuilds only when the connection flag flips, so each reconnect refreshes once.- if (connected !== undefined && get(connected(environmentId))) {+ if (get(input.connected(environmentId))) {void refresh(get.registry);}}).pipe(Atom.withLabel(`vcs-status-revalidation:${key}`));