@transifex/native 4.1.0 → 4.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@transifex/native",
3
- "version": "4.1.0",
3
+ "version": "4.2.0",
4
4
  "description": "i18n framework using Transifex Native",
5
5
  "keywords": [
6
6
  "transifex",
package/src/TxNative.js CHANGED
@@ -30,6 +30,8 @@ export default class TxNative {
30
30
  this.token = '';
31
31
  this.secret = '';
32
32
  this.filterTags = '';
33
+ this.fetchTimeout = 0;
34
+ this.fetchInterval = 250;
33
35
  this.cache = new MemoryCache();
34
36
  this.missingPolicy = new SourceStringPolicy();
35
37
  this.errorPolicy = new SourceErrorPolicy();
@@ -48,6 +50,8 @@ export default class TxNative {
48
50
  * @param {String} params.filterTags
49
51
  * @param {String} params.token
50
52
  * @param {String} params.secret
53
+ * @param {Number} params.fetchTimeout
54
+ * @param {Number} params.fetchInterval
51
55
  * @param {Function} params.cache
52
56
  * @param {Function} params.missingPolicy
53
57
  * @param {Function} params.errorPolicy
@@ -61,6 +65,8 @@ export default class TxNative {
61
65
  'secret',
62
66
  'cache',
63
67
  'filterTags',
68
+ 'fetchTimeout',
69
+ 'fetchInterval',
64
70
  'missingPolicy',
65
71
  'errorPolicy',
66
72
  'stringRenderer',
@@ -183,11 +189,17 @@ export default class TxNative {
183
189
  }
184
190
  }
185
191
 
192
+ const handleError = (err) => {
193
+ sendEvent(TRANSLATIONS_FETCH_FAILED, { localeCode, filterTags }, this);
194
+ return err;
195
+ };
196
+
186
197
  // contact CDS
187
198
  try {
188
199
  sendEvent(FETCHING_TRANSLATIONS, { localeCode, filterTags }, this);
189
200
  let response;
190
201
  let lastResponseStatus = 202;
202
+ const tsNow = Date.now();
191
203
  while (lastResponseStatus === 202) {
192
204
  /* eslint-disable no-await-in-loop */
193
205
  let url = `${this.cdsHost}/content/${localeCode}`;
@@ -201,8 +213,14 @@ export default class TxNative {
201
213
  'X-NATIVE-SDK': `txjs/${__PLATFORM__}/${__VERSION__}`,
202
214
  },
203
215
  });
204
- /* eslint-enable no-await-in-loop */
205
216
  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 */
206
224
  }
207
225
 
208
226
  const { data } = response;
@@ -216,12 +234,10 @@ export default class TxNative {
216
234
  this.cache.update(localeCode, hashmap);
217
235
  sendEvent(TRANSLATIONS_FETCHED, { localeCode, filterTags }, this);
218
236
  } 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'));
221
238
  }
222
239
  } catch (err) {
223
- sendEvent(TRANSLATIONS_FETCH_FAILED, { localeCode, filterTags }, this);
224
- throw err;
240
+ throw handleError(err);
225
241
  }
226
242
  }
227
243
 
@@ -355,11 +371,17 @@ export default class TxNative {
355
371
 
356
372
  if (!this.token) return [];
357
373
 
374
+ const handleError = (err) => {
375
+ sendEvent(LOCALES_FETCH_FAILED, null, this);
376
+ return err;
377
+ };
378
+
358
379
  // contact CDS
359
380
  try {
360
381
  sendEvent(FETCHING_LOCALES, null, this);
361
382
  let response;
362
383
  let lastResponseStatus = 202;
384
+ const tsNow = Date.now();
363
385
  while (lastResponseStatus === 202) {
364
386
  /* eslint-disable no-await-in-loop */
365
387
  response = await axios.get(`${this.cdsHost}/languages`, {
@@ -369,8 +391,14 @@ export default class TxNative {
369
391
  'X-NATIVE-SDK': `txjs/${__PLATFORM__}/${__VERSION__}`,
370
392
  },
371
393
  });
372
- /* eslint-enable no-await-in-loop */
373
394
  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 */
374
402
  }
375
403
 
376
404
  const { data } = response;
@@ -379,12 +407,10 @@ export default class TxNative {
379
407
  this.locales = this.languages.map((entry) => entry.code);
380
408
  sendEvent(LOCALES_FETCHED, null, this);
381
409
  } 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'));
384
411
  }
385
412
  } catch (err) {
386
- sendEvent(LOCALES_FETCH_FAILED, null, this);
387
- throw err;
413
+ throw handleError(err);
388
414
  }
389
415
 
390
416
  return [...this.locales];