@@ -37,8 +37,8 @@ import {
3737} from 'realtime-server/lib/esm/scriptureforge/models/note-thread' ;
3838import { SF_PROJECT_RIGHTS , SFProjectDomain } from 'realtime-server/lib/esm/scriptureforge/models/sf-project-rights' ;
3939import { fromVerseRef } from 'realtime-server/lib/esm/scriptureforge/models/verse-ref-data' ;
40- import { BehaviorSubject , combineLatest , firstValueFrom , merge , Observable , Subscription } from 'rxjs' ;
41- import { filter } from 'rxjs/operators' ;
40+ import { BehaviorSubject , combineLatest , defer , firstValueFrom , from , merge , Observable , Subscription } from 'rxjs' ;
41+ import { filter , switchMap } from 'rxjs/operators' ;
4242import { DataLoadingComponent } from 'xforge-common/data-loading-component' ;
4343import { DialogService } from 'xforge-common/dialog.service' ;
4444import { I18nService } from 'xforge-common/i18n.service' ;
@@ -354,7 +354,11 @@ export class BiblicalTermsComponent extends DataLoadingComponent implements OnDe
354354 // Subscribe to any project, book, chapter, verse, locale, biblical term, or note changes
355355 this . loadingStarted ( ) ;
356356 this . categoriesLoading = true ;
357- const biblicalTermsAndNotesChanges$ : Observable < any > = await this . getBiblicalTermsAndNotesChanges ( projectId ) ;
357+ const biblicalTermsNotesChanges$ : Observable < any > = await this . getBiblicalTermsNotesChanges ( projectId ) ;
358+ const biblicalTermsChanges$ : Observable < any > = this . bookNum$ . pipe (
359+ filter ( bookNum => bookNum !== 0 ) ,
360+ switchMap ( bookNum => this . getBiblicalTermsChanges ( projectId , bookNum ) )
361+ ) ;
358362
359363 this . biblicalTermSub ?. unsubscribe ( ) ;
360364
@@ -363,18 +367,21 @@ export class BiblicalTermsComponent extends DataLoadingComponent implements OnDe
363367 this . chapter$ ,
364368 this . verse$ ,
365369 this . i18n . locale$ ,
366- biblicalTermsAndNotesChanges$
370+ biblicalTermsChanges$ ,
371+ biblicalTermsNotesChanges$
367372 ] )
368373 . pipe (
369374 filter ( ( [ bookNum , chapter , verse ] ) => bookNum !== 0 && chapter !== 0 && verse !== null ) ,
370375 quietTakeUntilDestroyed ( this . destroyRef )
371376 )
372377 . subscribe ( ( [ bookNum , chapter , verse ] ) => {
373378 this . filterBiblicalTerms ( bookNum , chapter , verse ) ;
379+ this . biblicalTermsLoaded = true ;
374380 this . categoriesLoading = false ;
381+ this . loadingFinished ( ) ;
375382 } ) ;
376383
377- if ( ! this . appOnline && biblicalTermsAndNotesChanges$ . pipe ( filter ( val => val == null ) ) ) {
384+ if ( ! this . appOnline && ! this . biblicalTermsLoaded ) {
378385 this . loadingFinished ( ) ;
379386 } else {
380387 this . biblicalTermsLoaded = true ;
@@ -533,23 +540,27 @@ export class BiblicalTermsComponent extends DataLoadingComponent implements OnDe
533540 this . loadingFinished ( ) ;
534541 }
535542
536- private async getBiblicalTermsAndNotesChanges ( sfProjectId : string ) : Promise < Observable < any > > {
537- // Clean up existing queries
538- this . biblicalTermQuery ?. dispose ( ) ;
543+ private getBiblicalTermsChanges ( sfProjectId : string , bookNum : number ) : Observable < any > {
544+ return defer ( ( ) => {
545+ this . biblicalTermQuery ?. dispose ( ) ;
546+ return from ( this . projectService . queryBiblicalTerms ( sfProjectId , bookNum , this . destroyRef ) ) . pipe (
547+ switchMap ( query => {
548+ this . biblicalTermQuery = query ;
549+ return merge ( query . ready$ . pipe ( filter ( isReady => isReady ) ) , query . remoteChanges$ , query . remoteDocChanges$ ) ;
550+ } )
551+ ) ;
552+ } ) ;
553+ }
554+
555+ private async getBiblicalTermsNotesChanges ( sfProjectId : string ) : Promise < Observable < any > > {
556+ // Clean up existing query
539557 this . noteThreadQuery ?. dispose ( ) ;
540558
541- // Get the Biblical Terms and Notes
542- [ this . biblicalTermQuery , this . noteThreadQuery ] = await Promise . all ( [
543- this . projectService . queryBiblicalTerms ( sfProjectId , this . destroyRef ) ,
544- this . projectService . queryBiblicalTermNoteThreads ( sfProjectId , this . destroyRef )
545- ] ) ;
559+ // Get the Biblical Terms Notes
560+ this . noteThreadQuery = await this . projectService . queryBiblicalTermNoteThreads ( sfProjectId , this . destroyRef ) ;
546561
547562 // Return a merged observable to monitor changes
548-
549563 return merge (
550- this . biblicalTermQuery . ready$ . pipe ( filter ( isReady => isReady ) ) ,
551- this . biblicalTermQuery . remoteChanges$ ,
552- this . biblicalTermQuery . remoteDocChanges$ ,
553564 this . noteThreadQuery . localChanges$ ,
554565 this . noteThreadQuery . ready$ . pipe ( filter ( isReady => isReady ) ) ,
555566 this . noteThreadQuery . remoteChanges$ ,
0 commit comments