@@ -319,36 +319,43 @@ export class LogbookItemComponent implements OnInit, AfterViewInit, OnDestroy {
319319 }
320320
321321 startNotificationManager ( ) {
322- if ( typeof this . config != 'undefined' ) {
323- if ( this . changeStreamSubscriptions . length > 0 ) {
324- this . changeStreamSubscriptions . forEach ( ( sub ) => sub . unsubscribe ( ) ) ;
325- }
326- let logbooks = [ this . config . filter . targetId , ...this . config . filter ?. additionalLogbooks ] ;
327- console . log ( 'Subscribing to the following logbooks: ' , logbooks ) ;
328- logbooks . forEach ( ( log ) => {
329- this . changeStreamSubscriptions . push (
330- this . notificationService . getNotification ( log , this . config ) . subscribe ( ( notification ) => {
331- console . log ( notification ) ;
332- this . notifications . push ( notification ) ;
333- this . parseNotification ( notification ) ;
334- } ) ,
335- ) ;
336- } ) ;
322+ if ( ! this . config ) return ; // Clean old changeStream subscriptions
323+ if ( this . changeStreamSubscriptions . length > 0 ) {
324+ this . changeStreamSubscriptions . forEach ( ( s ) => s . unsubscribe ( ) ) ;
325+ this . changeStreamSubscriptions = [ ] ;
326+ }
327+ const logbooks = [
328+ this . config . filter . targetId ,
329+ ...( this . config . filter ?. additionalLogbooks ?? [ ] ) ,
330+ ] ;
337331
338- if ( this . dataService == null ) {
339- this . dataService = this . data . currentMessage . subscribe ( ( message ) => {
340- // console.log(message);
341- this . message = message ;
342- if ( message != null && Object . keys ( this . message ) . length != 0 ) {
343- // console.log(this.message);
344- this . submitContent ( message ) ;
345- this . message = null ;
346- }
332+ logbooks . forEach ( ( log ) => {
333+ const sub = this . notificationService
334+ . getNotification ( log , this . config )
335+ . subscribe ( ( notification ) => {
336+ this . parseNotification ( notification ) ;
347337 } ) ;
348- }
338+
339+ this . changeStreamSubscriptions . push ( sub ) ;
340+ } ) ;
341+
342+ // ALWAYS unsubscribe previous message subscription
343+ if ( this . dataService ) {
344+ this . dataService . unsubscribe ( ) ;
345+ this . dataService = null ;
349346 }
347+
348+ this . dataService = this . data . currentMessage$ . subscribe ( ( message ) => {
349+ if ( ! message ) return ;
350+
351+ // Only main widget should submit
352+ if ( this . configIndex !== 1 ) return ;
353+
354+ this . submitContent ( message ) ;
355+ } ) ;
350356 }
351357
358+
352359 async parseNotification ( notification : ChangeStreamNotification ) {
353360 switch ( notification . operationType ) {
354361 case 'update' :
0 commit comments