@transifex/native 2.2.0-alpha.0 → 2.3.2

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": "2.2.0-alpha.0",
3
+ "version": "2.3.2",
4
4
  "description": "Transifex Native for Javascript",
5
5
  "keywords": [
6
6
  "transifex",
package/src/TxNative.js CHANGED
@@ -35,6 +35,7 @@ export default class TxNative {
35
35
  this.currentLocale = '';
36
36
  this.locales = [];
37
37
  this.languages = [];
38
+ this.childInstances = [];
38
39
  }
39
40
 
40
41
  /**
@@ -52,7 +53,6 @@ export default class TxNative {
52
53
  */
53
54
  init(params) {
54
55
  const that = this;
55
-
56
56
  [
57
57
  'cdsHost',
58
58
  'token',
@@ -68,7 +68,6 @@ export default class TxNative {
68
68
  that[value] = params[value];
69
69
  }
70
70
  });
71
-
72
71
  this.fetchedTags = {}; // {langCode: [tag1, tag2, ...], ...}
73
72
  }
74
73
 
@@ -301,14 +300,27 @@ export default class TxNative {
301
300
  * @returns {Promise}
302
301
  */
303
302
  async setCurrentLocale(localeCode) {
304
- if (this.isCurrent(localeCode)) return;
303
+ if (this.isCurrent(localeCode)) {
304
+ await this._syncInstances(this.childInstances);
305
+ return;
306
+ }
307
+
305
308
  if (!localeCode) {
309
+ // update controller
306
310
  this.currentLocale = '';
311
+ await this._syncInstances(this.childInstances);
307
312
  sendEvent(LOCALE_CHANGED, this.currentLocale, this);
308
313
  return;
309
314
  }
315
+
316
+ // Fetch translations for controller instance
310
317
  await this.fetchTranslations(localeCode);
311
318
  this.currentLocale = localeCode;
319
+
320
+ // Update children
321
+ await this._syncInstances(this.childInstances);
322
+
323
+ // Trigger controller change
312
324
  sendEvent(LOCALE_CHANGED, localeCode, this);
313
325
  }
314
326
 
@@ -328,4 +340,63 @@ export default class TxNative {
328
340
  await this.getLocales(params);
329
341
  return [...this.languages];
330
342
  }
343
+
344
+ /**
345
+ * Connect a child instance with this instance as controller.
346
+ * When the language is changing on this instance, all child
347
+ * instances will be updated as well.
348
+ *
349
+ * @param {*} instance
350
+ * @returns {Promise}
351
+ */
352
+ async controllerOf(instance) {
353
+ if (instance === this) {
354
+ throw new Error('Cannot add self as instance');
355
+ }
356
+ if (instance.childInstances.indexOf(this) !== -1) {
357
+ throw new Error('Cycle reference error, instance is controller of this');
358
+ }
359
+
360
+ this.childInstances.push(instance);
361
+ await this._syncInstances([instance]);
362
+
363
+ return instance;
364
+ }
365
+
366
+ /**
367
+ * Private function to sync controller with
368
+ * child instance.
369
+ *
370
+ * @param {Array} instances
371
+ * @memberof TxNative
372
+ */
373
+ async _syncInstances(instances) {
374
+ // update instance language
375
+ const localeCode = this.getCurrentLocale();
376
+
377
+ // update children instances
378
+ if (localeCode) {
379
+ for (let i = 0; i < instances.length; i++) {
380
+ // do not fetch language if not needed
381
+ if (instances[i].getCurrentLocale() !== localeCode) {
382
+ // Fetch translations for additional instance without blocking
383
+ // anything else in case of missing language
384
+ try {
385
+ // eslint-disable-next-line no-await-in-loop
386
+ await instances[i].fetchTranslations(localeCode);
387
+ } catch (e) {
388
+ // no-op
389
+ }
390
+ }
391
+ }
392
+ }
393
+ // Reloop through the instances to avoid content flash
394
+ instances.forEach((instance) => {
395
+ if (instance.getCurrentLocale() !== localeCode) {
396
+ // eslint-disable-next-line no-param-reassign
397
+ instance.currentLocale = localeCode;
398
+ sendEvent(LOCALE_CHANGED, localeCode, instance);
399
+ }
400
+ });
401
+ }
331
402
  }
package/src/index.d.ts CHANGED
@@ -6,4 +6,5 @@ declare module '@transifex/native' {
6
6
  export const SourceErrorPolicy: any;
7
7
  export const ThrowErrorPolicy: any;
8
8
  export const MessageFormatRenderer: any;
9
+ export const createNativeInstance: any;
9
10
  }
package/src/index.js CHANGED
@@ -7,6 +7,15 @@ import _SourceErrorPolicy from './policies/SourceErrorPolicy';
7
7
  import _ThrowErrorPolicy from './policies/ThrowErrorPolicy';
8
8
  import _MessageFormatRenderer from './renderers/MessageFormatRenderer';
9
9
 
10
+ function _createNativeInstance(initOptions) {
11
+ const instance = new TxNative();
12
+ instance.t = instance.translate.bind(instance);
13
+ if (initOptions) {
14
+ instance.init(initOptions);
15
+ }
16
+ return instance;
17
+ }
18
+
10
19
  export * from './utils';
11
20
  export * from './events';
12
21
 
@@ -15,6 +24,7 @@ export const SourceStringPolicy = _SourceStringPolicy;
15
24
  export const SourceErrorPolicy = _SourceErrorPolicy;
16
25
  export const ThrowErrorPolicy = _ThrowErrorPolicy;
17
26
  export const MessageFormatRenderer = _MessageFormatRenderer;
27
+ export const createNativeInstance = _createNativeInstance;
18
28
 
19
29
  export const tx = new TxNative();
20
30
  export const t = tx.translate.bind(tx);