88 ResolvedKeybindingsConfig ,
99 type ServerRemoveKeybindingInput ,
1010 type ServerUpsertKeybindingInput ,
11- type ServerConfigIssue ,
11+ ServerConfigIssue ,
1212} from "@t3tools/contracts" ;
1313import * as Array from "effect/Array" ;
1414import * 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+
183190export 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