@@ -75,68 +75,67 @@ export const fetchWorkspaceState = async ({
7575/**
7676 * Add a listener to check if the workspace state has expired with a certain interval
7777 */
78- export const addWorkspaceStateExpiryCheckListener =
79- async ( ) : Promise < void > => {
80- const appConfig = await RNConfig . getInstance ( ) ;
81- const logger = Logger . getInstance ( ) ;
82-
83- const updateInterval = 1000 * 60 ; // every minute
84-
85- if ( workspaceSyncIntervalId === null ) {
86- const intervalHandler = async ( ) : Promise < void > => {
87- const expiresAt = appConfig . get ( ) . workspace . expiresAt ;
88-
89- try {
90- // check if the workspace state has not expired yet
91- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- expiresAt is checked for null
92- if ( expiresAt && new Date ( expiresAt ) >= new Date ( ) ) {
93- return ;
94- }
95-
96- logger . debug ( "Workspace state has expired. Starting sync." ) ;
97-
98- const personState = appConfig . get ( ) . user ;
99- const workspace = await fetchWorkspaceState ( {
100- appUrl : appConfig . get ( ) . appUrl ,
101- workspaceId : appConfig . get ( ) . workspaceId ,
102- } ) ;
78+ export const addWorkspaceStateExpiryCheckListener = async ( ) : Promise < void > => {
79+ const appConfig = await RNConfig . getInstance ( ) ;
80+ const logger = Logger . getInstance ( ) ;
81+
82+ const updateInterval = 1000 * 60 ; // every minute
83+
84+ if ( workspaceSyncIntervalId === null ) {
85+ const intervalHandler = async ( ) : Promise < void > => {
86+ const expiresAt = appConfig . get ( ) . workspace . expiresAt ;
87+
88+ try {
89+ // check if the workspace state has not expired yet
90+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- expiresAt is checked for null
91+ if ( expiresAt && new Date ( expiresAt ) >= new Date ( ) ) {
92+ return ;
93+ }
94+
95+ logger . debug ( "Workspace state has expired. Starting sync." ) ;
96+
97+ const personState = appConfig . get ( ) . user ;
98+ const workspace = await fetchWorkspaceState ( {
99+ appUrl : appConfig . get ( ) . appUrl ,
100+ workspaceId : appConfig . get ( ) . workspaceId ,
101+ } ) ;
102+
103+ if ( workspace . ok ) {
104+ const { data : state } = workspace ;
105+ const filteredSurveys = filterSurveys ( state , personState ) ;
103106
104- if ( workspace . ok ) {
105- const { data : state } = workspace ;
106- const filteredSurveys = filterSurveys ( state , personState ) ;
107-
108- appConfig . update ( {
109- ...appConfig . get ( ) ,
110- workspace : state ,
111- filteredSurveys,
112- } ) ;
113- } else {
114- // eslint-disable-next-line @typescript-eslint/only-throw-error -- error is an ApiErrorResponse
115- throw workspace . error ;
116- }
117- } catch ( e ) {
118- console . error ( `Error during expiry check: ` , e ) ;
119- logger . debug ( "Extending config and try again later." ) ;
120- const existingConfig = appConfig . get ( ) ;
121107 appConfig . update ( {
122- ...existingConfig ,
123- workspace : {
124- ...existingConfig . workspace ,
125- expiresAt : new Date ( Date . now ( ) + 1000 * 60 * 30 ) , // 30 minutes
126- } ,
108+ ...appConfig . get ( ) ,
109+ workspace : state ,
110+ filteredSurveys,
127111 } ) ;
112+ } else {
113+ // eslint-disable-next-line @typescript-eslint/only-throw-error -- error is an ApiErrorResponse
114+ throw workspace . error ;
128115 }
129- } ;
116+ } catch ( e ) {
117+ console . error ( `Error during expiry check: ` , e ) ;
118+ logger . debug ( "Extending config and try again later." ) ;
119+ const existingConfig = appConfig . get ( ) ;
120+ appConfig . update ( {
121+ ...existingConfig ,
122+ workspace : {
123+ ...existingConfig . workspace ,
124+ expiresAt : new Date ( Date . now ( ) + 1000 * 60 * 30 ) , // 30 minutes
125+ } ,
126+ } ) ;
127+ }
128+ } ;
130129
131- workspaceSyncIntervalId = setInterval (
132- ( ) => void intervalHandler ( ) ,
133- updateInterval ,
134- ) as unknown as number ;
135- }
136- } ;
130+ workspaceSyncIntervalId = setInterval (
131+ ( ) => void intervalHandler ( ) ,
132+ updateInterval ,
133+ ) as unknown as number ;
134+ }
135+ } ;
137136
138137export const clearWorkspaceStateExpiryCheckListener = ( ) : void => {
139- if ( workspaceSyncIntervalId ) {
138+ if ( workspaceSyncIntervalId !== null ) {
140139 clearInterval ( workspaceSyncIntervalId ) ;
141140 workspaceSyncIntervalId = null ;
142141 }
0 commit comments