Skip to content
Merged
40 changes: 40 additions & 0 deletions apps/server/src/keybindings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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())),
);
});
43 changes: 36 additions & 7 deletions apps/server/src/keybindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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[];
Expand Down Expand Up @@ -220,6 +227,8 @@ export class Keybindings extends Context.Service<

readonly streamChanges: Stream.Stream<KeybindingsChangeEvent>;

readonly revalidate: Effect.Effect<void, KeybindingsConfigError>;

/** Acquire before reading a snapshot to buffer changes until the stream starts. */
readonly subscribeChanges: Effect.Effect<
Stream.Stream<KeybindingsChangeEvent>,
Expand Down Expand Up @@ -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<KeybindingsChangeEvent>();
const lastEmittedStateRef = yield* Ref.make<KeybindingsConfigState | null>(null);
const startedRef = yield* Ref.make(false);
const startedDeferred = yield* Deferred.make<void, KeybindingsConfigError>();
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(
Expand Down Expand Up @@ -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* () {
Expand Down Expand Up @@ -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);
Expand All @@ -624,6 +652,7 @@ const make = Effect.gen(function* () {
syncDefaultKeybindingsOnStartup,
loadConfigState: loadConfigStateFromCacheOrDisk,
getSnapshot: loadConfigStateFromCacheOrDisk,
revalidate: revalidateAndEmit,
get streamChanges() {
return Stream.fromPubSub(changesPubSub);
},
Expand Down
Loading