@@ -3,8 +3,9 @@ import { ConfigService } from '@nestjs/config';
33import { EventEmitter2 } from '@nestjs/event-emitter' ;
44import type { OutgoingHttpHeaders } from 'node:http2' ;
55
6+ import { isEqual } from 'lodash-es' ;
67import { Subscription } from 'rxjs' ;
7- import { bufferTime , filter } from 'rxjs/operators' ;
8+ import { debounceTime , filter } from 'rxjs/operators' ;
89
910import { ConnectionMetadata , MinigraphStatus , MyServersConfig } from '../config/connect.config.js' ;
1011import { EVENTS } from '../helper/nest-tokens.js' ;
@@ -58,6 +59,7 @@ export class MothershipConnectionService implements OnModuleInit, OnModuleDestro
5859 } ;
5960
6061 private identitySubscription : Subscription | null = null ;
62+ private lastIdentity : Partial < IdentityState > | null = null ;
6163 private metadataChangedSubscription : Subscription | null = null ;
6264
6365 constructor (
@@ -83,12 +85,22 @@ export class MothershipConnectionService implements OnModuleInit, OnModuleDestro
8385 this . identitySubscription = this . configService . changes$
8486 . pipe (
8587 filter ( ( change ) => Object . values ( this . configKeys ) . includes ( change . path ) ) ,
86- bufferTime ( 25 )
88+ // debouncing is necessary here (instead of buffering/batching) to prevent excess emissions
89+ // because the store.* config values will change frequently upon api boot
90+ debounceTime ( 25 )
8791 )
8892 . subscribe ( {
8993 next : ( ) => {
94+ const { state } = this . getIdentityState ( ) ;
95+ if ( isEqual ( state , this . lastIdentity ) ) {
96+ this . logger . debug ( 'Identity unchanged; skipping event emission' ) ;
97+ return ;
98+ }
99+ this . lastIdentity = structuredClone ( state ) ;
90100 const success = this . eventEmitter . emit ( EVENTS . IDENTITY_CHANGED ) ;
91- if ( ! success ) {
101+ if ( success ) {
102+ this . logger . debug ( 'Emitted IDENTITY_CHANGED event' ) ;
103+ } else {
92104 this . logger . warn ( 'Failed to emit IDENTITY_CHANGED event' ) ;
93105 }
94106 } ,
0 commit comments