Skip to content

Commit

Permalink
Add MobX action to $setPreferences
Browse files Browse the repository at this point in the history
  • Loading branch information
sveyret committed Nov 23, 2019
1 parent 870d4aa commit a98970d
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 5 deletions.
2 changes: 2 additions & 0 deletions doc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ Clone the given internationalization object with new preferences. In other words

Change the language preferences of this internationalization object. This is the only method which will modify the object state. If you need your object to be immutable, use the clone constructor instead (see above).

If the MobX library can be loaded, this function will automatically become an action, preventing it from throwing an exception if in MobX strict mode.

## \$preferences: ReadonlyArray\<string>

The preferences really used by the object. Only languages which are found in the language map are retained. This parameter is read-only.
Expand Down
2 changes: 2 additions & 0 deletions doc/fr/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ Cloner l'objet d'internationalisation donné avec de nouvelles préférences. Au

Modifier les préférences linguistique de cet objet d'internationalisation. C'est la seule méthode qui modifie l'état de l'objet. Si vous avez besoin d'utiliser un objet immuable, utilisez plutôt le constructeur de copie (voir ci-dessus).

Si la bibliothèque MobX peut être chargée, cette méthode deviend automatiquement une action, permettant de ne pas lever d'exception même avec MobX en mode strict.

## \$preferences: ReadonlyArray\<string>

Les préférences réellement utilisées par l'objet. Seules les langues trouvées dans la table de langues sont conservées. Ce paramètre est en lecture seulement.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "intl-ts",
"version": "4.0.2",
"version": "4.0.3",
"description": "Internationalization library for TypeScript",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand Down
6 changes: 4 additions & 2 deletions src/Intl.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
/* eslint-disable prefer-arrow-callback */
import { expect } from 'chai'
import { autorun, observable } from 'mobx'
import { autorun, configure as mobxConfigure, observable, runInAction } from 'mobx'

import Intl, { LanguageMap, Messages } from '.'
// eslint-disable-next-line @typescript-eslint/camelcase
import { allAvailables, en, eo, fr, fr_ca, langType, languageMap } from './LanguageMap.spec'

mobxConfigure({ enforceActions: 'observed' })

describe('Intl', function() {
const lang: Intl<langType> = new Intl(languageMap)

Expand Down Expand Up @@ -147,7 +149,7 @@ describe('Intl', function() {
}
const store = new Store()
const result = await testObservation(store, 'Jon', () => {
store.lang = new Intl(languageMap, ['fr'])
runInAction(() => (store.lang = new Intl(languageMap, ['fr'])))
})
expect(result).to.have.ordered.members(['Hello Jon', 'Bonjour Jon'])
})
Expand Down
10 changes: 8 additions & 2 deletions src/Intl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ function calculatePreferences(
}

/*
* Try to load the `mobx` library. If succeed, will give the ability to create observable properties.
* Try to load the `mobx` library. If succeed, will give the ability to create observable and actions.
*/
let mobx: typeof import('mobx') | undefined
try {
Expand All @@ -93,6 +93,8 @@ try {
} catch {
// Silent ignore
}

// Define a property which may be observable if `mobx` is available.
const defineObservableProperty: (o: any, p: string, value: any) => void =
!!mobx && !!mobx.extendObservable && !!mobx.observable && !!mobx.observable.ref
? (o: any, p: string, value: any) => {
Expand All @@ -116,7 +118,8 @@ const defineObservableProperty: (o: any, p: string, value: any) => void =
* @param this - The Intl object.
* @param mapOrSource - The Language map to use to get the messages, or the source Intl object to clone.
* @param preferences - The preferred languages, ordered.
* @param createGenerics - If true (default), create generic languages for preferences (e.g. Will add 'en' for 'en-US').
* @param createGenerics - If true (default), create generic languages for preferences (e.g. Will add 'en'
* for 'en-US').
*/
export const Intl: {
prototype: Intl<Messages>
Expand Down Expand Up @@ -177,6 +180,9 @@ Intl.prototype.$changePreferences = function<T extends Messages>(
)
return this
}
if (!!mobx && !!mobx.action) {
Intl.prototype.$changePreferences = mobx!.action(Intl.prototype.$changePreferences)
}

Intl.prototype.$getMessageFunction = function<T extends Messages, K extends keyof T>(
this: Intl<T>,
Expand Down

0 comments on commit a98970d

Please sign in to comment.