@@ -30,6 +30,8 @@ export default class TxNative {
3030 this . token = '' ;
3131 this . secret = '' ;
3232 this . filterTags = '' ;
33+ this . fetchTimeout = 0 ;
34+ this . fetchInterval = 250 ;
3335 this . cache = new MemoryCache ( ) ;
3436 this . missingPolicy = new SourceStringPolicy ( ) ;
3537 this . errorPolicy = new SourceErrorPolicy ( ) ;
@@ -48,6 +50,8 @@ export default class TxNative {
4850 * @param {String } params.filterTags
4951 * @param {String } params.token
5052 * @param {String } params.secret
53+ * @param {Number } params.fetchTimeout
54+ * @param {Number } params.fetchInterval
5155 * @param {Function } params.cache
5256 * @param {Function } params.missingPolicy
5357 * @param {Function } params.errorPolicy
@@ -61,6 +65,8 @@ export default class TxNative {
6165 'secret' ,
6266 'cache' ,
6367 'filterTags' ,
68+ 'fetchTimeout' ,
69+ 'fetchInterval' ,
6470 'missingPolicy' ,
6571 'errorPolicy' ,
6672 'stringRenderer' ,
@@ -183,11 +189,17 @@ export default class TxNative {
183189 }
184190 }
185191
192+ const handleError = ( err ) => {
193+ sendEvent ( TRANSLATIONS_FETCH_FAILED , { localeCode, filterTags } , this ) ;
194+ return err ;
195+ } ;
196+
186197 // contact CDS
187198 try {
188199 sendEvent ( FETCHING_TRANSLATIONS , { localeCode, filterTags } , this ) ;
189200 let response ;
190201 let lastResponseStatus = 202 ;
202+ const tsNow = Date . now ( ) ;
191203 while ( lastResponseStatus === 202 ) {
192204 /* eslint-disable no-await-in-loop */
193205 let url = `${ this . cdsHost } /content/${ localeCode } ` ;
@@ -201,8 +213,14 @@ export default class TxNative {
201213 'X-NATIVE-SDK' : `txjs/${ __PLATFORM__ } /${ __VERSION__ } ` ,
202214 } ,
203215 } ) ;
204- /* eslint-enable no-await-in-loop */
205216 lastResponseStatus = response . status ;
217+ if ( this . fetchTimeout > 0 && ( Date . now ( ) - tsNow ) >= this . fetchTimeout ) {
218+ throw handleError ( new Error ( 'Fetch translations timeout' ) ) ;
219+ }
220+ if ( lastResponseStatus === 202 && this . fetchInterval > 0 ) {
221+ await sleep ( this . fetchInterval ) ;
222+ }
223+ /* eslint-enable no-await-in-loop */
206224 }
207225
208226 const { data } = response ;
@@ -216,12 +234,10 @@ export default class TxNative {
216234 this . cache . update ( localeCode , hashmap ) ;
217235 sendEvent ( TRANSLATIONS_FETCHED , { localeCode, filterTags } , this ) ;
218236 } else {
219- sendEvent ( TRANSLATIONS_FETCH_FAILED , { localeCode, filterTags } , this ) ;
220- throw new Error ( 'Could not fetch translations' ) ;
237+ throw handleError ( new Error ( 'Could not fetch translations' ) ) ;
221238 }
222239 } catch ( err ) {
223- sendEvent ( TRANSLATIONS_FETCH_FAILED , { localeCode, filterTags } , this ) ;
224- throw err ;
240+ throw handleError ( err ) ;
225241 }
226242 }
227243
@@ -355,11 +371,17 @@ export default class TxNative {
355371
356372 if ( ! this . token ) return [ ] ;
357373
374+ const handleError = ( err ) => {
375+ sendEvent ( LOCALES_FETCH_FAILED , null , this ) ;
376+ return err ;
377+ } ;
378+
358379 // contact CDS
359380 try {
360381 sendEvent ( FETCHING_LOCALES , null , this ) ;
361382 let response ;
362383 let lastResponseStatus = 202 ;
384+ const tsNow = Date . now ( ) ;
363385 while ( lastResponseStatus === 202 ) {
364386 /* eslint-disable no-await-in-loop */
365387 response = await axios . get ( `${ this . cdsHost } /languages` , {
@@ -369,8 +391,14 @@ export default class TxNative {
369391 'X-NATIVE-SDK' : `txjs/${ __PLATFORM__ } /${ __VERSION__ } ` ,
370392 } ,
371393 } ) ;
372- /* eslint-enable no-await-in-loop */
373394 lastResponseStatus = response . status ;
395+ if ( this . fetchTimeout > 0 && ( Date . now ( ) - tsNow ) >= this . fetchTimeout ) {
396+ throw handleError ( new Error ( 'Get locales timeout' ) ) ;
397+ }
398+ if ( lastResponseStatus === 202 && this . fetchInterval > 0 ) {
399+ await sleep ( this . fetchInterval ) ;
400+ }
401+ /* eslint-enable no-await-in-loop */
374402 }
375403
376404 const { data } = response ;
@@ -379,12 +407,10 @@ export default class TxNative {
379407 this . locales = this . languages . map ( ( entry ) => entry . code ) ;
380408 sendEvent ( LOCALES_FETCHED , null , this ) ;
381409 } else {
382- sendEvent ( LOCALES_FETCH_FAILED , null , this ) ;
383- throw new Error ( 'Could not fetch languages' ) ;
410+ throw handleError ( new Error ( 'Could not fetch languages' ) ) ;
384411 }
385412 } catch ( err ) {
386- sendEvent ( LOCALES_FETCH_FAILED , null , this ) ;
387- throw err ;
413+ throw handleError ( err ) ;
388414 }
389415
390416 return [ ...this . locales ] ;
0 commit comments