Skip to content

Commit 3ea253f

Browse files
fix(server): suppress redundant keybindings change emission on unchanged revalidation (#23775)
Signed-off-by: Shaswat Raj <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
1 parent 44a8e95 commit 3ea253f

2 files changed

Lines changed: 76 additions & 7 deletions

File tree

‎apps/server/src/keybindings.test.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import * as Effect from "effect/Effect";
66
import * as FileSystem from "effect/FileSystem";
77
import * as Layer from "effect/Layer";
88
import * as Logger from "effect/Logger";
9+
import * as Option from "effect/Option";
910
import * as Path from "effect/Path";
1011
import * as Schema from "effect/Schema";
12+
import * as Stream from "effect/Stream";
1113
import * as ServerConfig from "./config.ts";
1214
import * as Keybindings from "./keybindings.ts";
1315
import * as SharedKeybindings from "@t3tools/shared/keybindings";
@@ -616,4 +618,42 @@ it.layer(NodeServices.layer)("keybindings", (it) => {
616618
}
617619
}).pipe(Effect.provide(layerKeybindings())),
618620
);
621+
622+
it.effect(
623+
"suppresses change emission when revalidating unchanged keybindings configuration",
624+
() =>
625+
Effect.gen(function* () {
626+
const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
627+
yield* writeKeybindingsConfig(keybindingsConfigPath, [
628+
{ key: "mod+j", command: "terminal.toggle" },
629+
]);
630+
631+
const keybindings = yield* Keybindings.Keybindings;
632+
yield* keybindings.start;
633+
634+
const changes = yield* keybindings.subscribeChanges;
635+
636+
// Revalidate when config on disk has not changed (e.g. atime bump on Windows).
637+
// `revalidate` publishes before it returns, so a wrongly emitted event would be
638+
// buffered in the subscription ahead of the real change observed below.
639+
yield* keybindings.revalidate;
640+
641+
yield* writeKeybindingsConfig(keybindingsConfigPath, [
642+
{ key: "mod+j", command: "terminal.toggle" },
643+
{ key: "mod+k", command: "threadPanel.toggle" },
644+
]);
645+
yield* keybindings.revalidate;
646+
647+
// The first buffered event must be the real change, not the unchanged state.
648+
const change = Option.getOrThrow(yield* Stream.runHead(changes));
649+
assert.isTrue(
650+
change.keybindings.some(
651+
(rule) =>
652+
rule.command === "threadPanel.toggle" &&
653+
rule.shortcut.key === "k" &&
654+
rule.shortcut.modKey,
655+
),
656+
);
657+
}).pipe(Effect.scoped, Effect.provide(layerKeybindings())),
658+
);
619659
});

‎apps/server/src/keybindings.ts‎

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
ResolvedKeybindingsConfig,
99
type ServerRemoveKeybindingInput,
1010
type ServerUpsertKeybindingInput,
11-
type ServerConfigIssue,
11+
ServerConfigIssue,
1212
} from "@t3tools/contracts";
1313
import * as Array from "effect/Array";
1414
import * as Cache from "effect/Cache";
@@ -180,6 +180,13 @@ export interface KeybindingsConfigState {
180180
readonly issues: readonly ServerConfigIssue[];
181181
}
182182

183+
const isSameKeybindingsConfigState = Schema.toEquivalence(
184+
Schema.Struct({
185+
keybindings: ResolvedKeybindingsConfig,
186+
issues: Schema.Array(ServerConfigIssue),
187+
}),
188+
);
189+
183190
export interface KeybindingsChangeEvent {
184191
readonly keybindings: ResolvedKeybindingsConfig;
185192
readonly issues: readonly ServerConfigIssue[];
@@ -220,6 +227,8 @@ export class Keybindings extends Context.Service<
220227

221228
readonly streamChanges: Stream.Stream<KeybindingsChangeEvent>;
222229

230+
readonly revalidate: Effect.Effect<void, KeybindingsConfigError>;
231+
223232
/** Acquire before reading a snapshot to buffer changes until the stream starts. */
224233
readonly subscribeChanges: Effect.Effect<
225234
Stream.Stream<KeybindingsChangeEvent>,
@@ -248,12 +257,17 @@ const make = Effect.gen(function* () {
248257
const upsertSemaphore = yield* Semaphore.make(1);
249258
const resolvedConfigCacheKey = "resolved" as const;
250259
const changesPubSub = yield* PubSub.unbounded<KeybindingsChangeEvent>();
260+
const lastEmittedStateRef = yield* Ref.make<KeybindingsConfigState | null>(null);
251261
const startedRef = yield* Ref.make(false);
252262
const startedDeferred = yield* Deferred.make<void, KeybindingsConfigError>();
253263
const watcherScope = yield* Scope.make("sequential");
254264
yield* Effect.addFinalizer(() => Scope.close(watcherScope, Exit.void));
255-
const emitChange = (configState: KeybindingsConfigState) =>
256-
PubSub.publish(changesPubSub, configState).pipe(Effect.asVoid);
265+
const emitChange = Effect.fn("keybindings.emitChange")(function* (
266+
configState: KeybindingsConfigState,
267+
) {
268+
yield* Ref.set(lastEmittedStateRef, configState);
269+
yield* PubSub.publish(changesPubSub, configState);
270+
});
257271

258272
const readConfigExists = fs.exists(keybindingsConfigPath).pipe(
259273
Effect.mapError(
@@ -432,14 +446,27 @@ const make = Effect.gen(function* () {
432446

433447
const loadConfigStateFromCacheOrDisk = Cache.get(resolvedConfigCache, resolvedConfigCacheKey);
434448

435-
const reloadAndEmit = Effect.gen(function* () {
449+
const reloadConfigState = Effect.gen(function* () {
436450
yield* Cache.invalidate(resolvedConfigCache, resolvedConfigCacheKey);
437-
const configState = yield* loadConfigStateFromCacheOrDisk;
451+
return yield* loadConfigStateFromCacheOrDisk;
452+
});
453+
454+
const reloadAndEmit = Effect.gen(function* () {
455+
const configState = yield* reloadConfigState;
438456
yield* emitChange(configState);
439457
return configState;
440458
});
441459

442-
const revalidateAndEmit = upsertSemaphore.withPermits(1)(reloadAndEmit);
460+
const revalidateAndEmit = upsertSemaphore.withPermits(1)(
461+
Effect.gen(function* () {
462+
const previousState = yield* Ref.get(lastEmittedStateRef);
463+
const configState = yield* reloadConfigState;
464+
if (!(previousState && isSameKeybindingsConfigState(previousState, configState))) {
465+
yield* emitChange(configState);
466+
}
467+
return configState;
468+
}),
469+
);
443470

444471
const syncDefaultKeybindingsOnStartup = upsertSemaphore.withPermits(1)(
445472
Effect.gen(function* () {
@@ -606,7 +633,8 @@ const make = Effect.gen(function* () {
606633
yield* startWatcher;
607634
yield* syncDefaultKeybindingsOnStartup;
608635
yield* Cache.invalidate(resolvedConfigCache, resolvedConfigCacheKey);
609-
yield* loadConfigStateFromCacheOrDisk;
636+
const initialState = yield* loadConfigStateFromCacheOrDisk;
637+
yield* Ref.set(lastEmittedStateRef, initialState);
610638
});
611639

612640
const startupExit = yield* Effect.exit(startup);
@@ -624,6 +652,7 @@ const make = Effect.gen(function* () {
624652
syncDefaultKeybindingsOnStartup,
625653
loadConfigState: loadConfigStateFromCacheOrDisk,
626654
getSnapshot: loadConfigStateFromCacheOrDisk,
655+
revalidate: revalidateAndEmit,
627656
get streamChanges() {
628657
return Stream.fromPubSub(changesPubSub);
629658
},

0 commit comments

Comments
 (0)