@@ -4,11 +4,12 @@ import {
44 ProjectMetadataWithoutFactoryInfo ,
55} from '@papi/core' ;
66import { logger } from '@papi/backend' ;
7- import { getErrorMessage , Mutex } from 'platform-bible-utils' ;
7+ import { Dispose , getErrorMessage , Mutex , PlatformEvent , Unsubscriber } from 'platform-bible-utils' ;
88import SlingshotProjectDataProviderEngine , {
99 SLINGSHOT_PROJECT_INTERFACES ,
1010} from './slingshot-project-data-provider-engine.model' ;
1111import ScriptureForgeApi , { ScriptureForgeProjectInfo } from './scripture-forge-api.model' ;
12+ import { NOT_LOGGED_IN_ERROR_MESSAGE } from '../auth/scripture-forge-authentication-provider.model' ;
1213
1314/**
1415 * Duration in milliseconds to throttle the `getProjects` API call. We will return the previous
@@ -30,16 +31,29 @@ function getSlingshotAppProjectId(projectId: string): string {
3031}
3132
3233export default class SlingshotProjectDataProviderEngineFactory
33- implements IProjectDataProviderEngineFactory < typeof SLINGSHOT_PROJECT_INTERFACES >
34+ implements IProjectDataProviderEngineFactory < typeof SLINGSHOT_PROJECT_INTERFACES > , Dispose
3435{
3536 private projectInfoByAppProjectId = new Map < string , ScriptureForgeProjectInfo > ( ) ;
3637 private pdpeByAppProjectId = new Map < string , SlingshotProjectDataProviderEngine > ( ) ;
3738 /** Last time we ran `getProjects` on the API so we can throttle it */
3839 private lastGetProjectsTime : number = 0 ;
3940 private lastAvailableProjects : ProjectMetadataWithoutFactoryInfo [ ] = [ ] ;
4041 private getProjectsMutex = new Mutex ( ) ;
41-
42- constructor ( private scriptureForgeApi : ScriptureForgeApi ) { }
42+ /**
43+ * If we know we are logged out and therefore shouldn't try to check for projects. If this is
44+ * `false`, it doesn't mean we are logged in but that we need to try getting projects again
45+ */
46+ private isLoggedOut = false ;
47+ private onDidSessionChangeUnsubscriber : Unsubscriber ;
48+
49+ constructor (
50+ private scriptureForgeApi : ScriptureForgeApi ,
51+ onDidSessionChange : PlatformEvent < undefined > ,
52+ ) {
53+ this . onDidSessionChangeUnsubscriber = onDidSessionChange ( ( ) => {
54+ this . isLoggedOut = false ;
55+ } ) ;
56+ }
4357
4458 async getAvailableProjects ( ) : Promise < ProjectMetadataWithoutFactoryInfo [ ] > {
4559 return this . #getAvailableProjects( ) ;
@@ -64,13 +78,20 @@ export default class SlingshotProjectDataProviderEngineFactory
6478 return pdpe ;
6579 }
6680
81+ async dispose ( ) : Promise < boolean > {
82+ return this . onDidSessionChangeUnsubscriber ( ) ;
83+ }
84+
6785 /**
6886 * Gets available projects from cache or reaches out to get new project info
6987 *
7088 * @param force If `true`, will get new project info
7189 */
7290 async #getAvailableProjects( force = false ) {
7391 if ( force ) this . lastGetProjectsTime = 0 ;
92+ // If we're not forcing and we know we're logged out, return empty array instead of failing
93+ // again because we know we're already logged out
94+ else if ( this . isLoggedOut ) return [ ] ;
7495
7596 if ( Date . now ( ) - this . lastGetProjectsTime < GET_PROJECTS_THROTTLE_MS )
7697 return this . lastAvailableProjects ;
@@ -86,9 +107,14 @@ export default class SlingshotProjectDataProviderEngineFactory
86107 try {
87108 projectsInfo = await this . scriptureForgeApi . getProjects ( ) ;
88109 } catch ( e ) {
110+ const errorMessage = getErrorMessage ( e ) ;
89111 logger . warn (
90- `Slingshot PDPEF caught Scripture Forge API error while getting available projects. ${ getErrorMessage ( e ) } ` ,
112+ `Slingshot PDPEF caught Scripture Forge API error while getting available projects. ${ errorMessage } ` ,
91113 ) ;
114+
115+ // Keep track if we're logged out so we don't keep spamming the logs
116+ if ( errorMessage === NOT_LOGGED_IN_ERROR_MESSAGE ) this . isLoggedOut = true ;
117+
92118 return [ ] ;
93119 }
94120
0 commit comments