diff --git a/apps/server/src/keybindings.test.ts b/apps/server/src/keybindings.test.ts index cb92c24242d47..7bff1a6823df1 100644 --- a/apps/server/src/keybindings.test.ts +++ b/apps/server/src/keybindings.test.ts @@ -6,8 +6,10 @@ import * as Effect from "effect/Effect"; import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; import * as Logger from "effect/Logger"; +import * as Option from "effect/Option"; import * as Path from "effect/Path"; import * as Schema from "effect/Schema"; +import * as Stream from "effect/Stream"; import * as ServerConfig from "./config.ts"; import * as Keybindings from "./keybindings.ts"; import * as SharedKeybindings from "@t3tools/shared/keybindings"; @@ -616,4 +618,42 @@ it.layer(NodeServices.layer)("keybindings", (it) => { } }).pipe(Effect.provide(layerKeybindings())), ); + + it.effect( + "suppresses change emission when revalidating unchanged keybindings configuration", + () => + Effect.gen(function* () { + const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig; + yield* writeKeybindingsConfig(keybindingsConfigPath, [ + { key: "mod+j", command: "terminal.toggle" }, + ]); + + const keybindings = yield* Keybindings.Keybindings; + yield* keybindings.start; + + const changes = yield* keybindings.subscribeChanges; + + // Revalidate when config on disk has not changed (e.g. atime bump on Windows). + // `revalidate` publishes before it returns, so a wrongly emitted event would be + // buffered in the subscription ahead of the real change observed below. + yield* keybindings.revalidate; + + yield* writeKeybindingsConfig(keybindingsConfigPath, [ + { key: "mod+j", command: "terminal.toggle" }, + { key: "mod+k", command: "threadPanel.toggle" }, + ]); + yield* keybindings.revalidate; + + // The first buffered event must be the real change, not the unchanged state. + const change = Option.getOrThrow(yield* Stream.runHead(changes)); + assert.isTrue( + change.keybindings.some( + (rule) => + rule.command === "threadPanel.toggle" && + rule.shortcut.key === "k" && + rule.shortcut.modKey, + ), + ); + }).pipe(Effect.scoped, Effect.provide(layerKeybindings())), + ); }); diff --git a/apps/server/src/keybindings.ts b/apps/server/src/keybindings.ts index 0a5bfdd4246ca..49a7d85d4fa36 100644 --- a/apps/server/src/keybindings.ts +++ b/apps/server/src/keybindings.ts @@ -8,7 +8,7 @@ import { ResolvedKeybindingsConfig, type ServerRemoveKeybindingInput, type ServerUpsertKeybindingInput, - type ServerConfigIssue, + ServerConfigIssue, } from "@t3tools/contracts"; import * as Array from "effect/Array"; import * as Cache from "effect/Cache"; @@ -180,6 +180,13 @@ export interface KeybindingsConfigState { readonly issues: readonly ServerConfigIssue[]; } +const isSameKeybindingsConfigState = Schema.toEquivalence( + Schema.Struct({ + keybindings: ResolvedKeybindingsConfig, + issues: Schema.Array(ServerConfigIssue), + }), +); + export interface KeybindingsChangeEvent { readonly keybindings: ResolvedKeybindingsConfig; readonly issues: readonly ServerConfigIssue[]; @@ -220,6 +227,8 @@ export class Keybindings extends Context.Service< readonly streamChanges: Stream.Stream; + readonly revalidate: Effect.Effect; + /** Acquire before reading a snapshot to buffer changes until the stream starts. */ readonly subscribeChanges: Effect.Effect< Stream.Stream, @@ -248,12 +257,17 @@ const make = Effect.gen(function* () { const upsertSemaphore = yield* Semaphore.make(1); const resolvedConfigCacheKey = "resolved" as const; const changesPubSub = yield* PubSub.unbounded(); + const lastEmittedStateRef = yield* Ref.make(null); const startedRef = yield* Ref.make(false); const startedDeferred = yield* Deferred.make(); const watcherScope = yield* Scope.make("sequential"); yield* Effect.addFinalizer(() => Scope.close(watcherScope, Exit.void)); - const emitChange = (configState: KeybindingsConfigState) => - PubSub.publish(changesPubSub, configState).pipe(Effect.asVoid); + const emitChange = Effect.fn("keybindings.emitChange")(function* ( + configState: KeybindingsConfigState, + ) { + yield* Ref.set(lastEmittedStateRef, configState); + yield* PubSub.publish(changesPubSub, configState); + }); const readConfigExists = fs.exists(keybindingsConfigPath).pipe( Effect.mapError( @@ -432,14 +446,27 @@ const make = Effect.gen(function* () { const loadConfigStateFromCacheOrDisk = Cache.get(resolvedConfigCache, resolvedConfigCacheKey); - const reloadAndEmit = Effect.gen(function* () { + const reloadConfigState = Effect.gen(function* () { yield* Cache.invalidate(resolvedConfigCache, resolvedConfigCacheKey); - const configState = yield* loadConfigStateFromCacheOrDisk; + return yield* loadConfigStateFromCacheOrDisk; + }); + + const reloadAndEmit = Effect.gen(function* () { + const configState = yield* reloadConfigState; yield* emitChange(configState); return configState; }); - const revalidateAndEmit = upsertSemaphore.withPermits(1)(reloadAndEmit); + const revalidateAndEmit = upsertSemaphore.withPermits(1)( + Effect.gen(function* () { + const previousState = yield* Ref.get(lastEmittedStateRef); + const configState = yield* reloadConfigState; + if (!(previousState && isSameKeybindingsConfigState(previousState, configState))) { + yield* emitChange(configState); + } + return configState; + }), + ); const syncDefaultKeybindingsOnStartup = upsertSemaphore.withPermits(1)( Effect.gen(function* () { @@ -606,7 +633,8 @@ const make = Effect.gen(function* () { yield* startWatcher; yield* syncDefaultKeybindingsOnStartup; yield* Cache.invalidate(resolvedConfigCache, resolvedConfigCacheKey); - yield* loadConfigStateFromCacheOrDisk; + const initialState = yield* loadConfigStateFromCacheOrDisk; + yield* Ref.set(lastEmittedStateRef, initialState); }); const startupExit = yield* Effect.exit(startup); @@ -624,6 +652,7 @@ const make = Effect.gen(function* () { syncDefaultKeybindingsOnStartup, loadConfigState: loadConfigStateFromCacheOrDisk, getSnapshot: loadConfigStateFromCacheOrDisk, + revalidate: revalidateAndEmit, get streamChanges() { return Stream.fromPubSub(changesPubSub); },