Skip to content

Commit

Permalink
fix: prevent mixed locale due to race condition using ngx-translate s…
Browse files Browse the repository at this point in the history
…ervice use method (#207, #222)

Closes #207

Co-authored-by: Alpe Numan Genc <anumangenc@gmail.com>
  • Loading branch information
dhhyi and anumang committed Apr 30, 2020
1 parent 337d9d3 commit 7750993
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/app/core/store/configuration/configuration.effects.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,12 @@ describe('Configuration Effects', () => {

describe('setLocale$', () => {
it('should call TranslateService when locale was initialized', done => {
verify(translateServiceMock.use(anything())).once();
const params = capture(translateServiceMock.use).last();
expect(params[0]).toEqual('en_US');
setTimeout(done, 1000);
setTimeout(() => {
verify(translateServiceMock.use(anything())).once();
const params = capture(translateServiceMock.use).last();
expect(params[0]).toEqual('en_US');
done();
}, 1000);
});
});

Expand Down
5 changes: 4 additions & 1 deletion src/app/core/store/configuration/configuration.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { TranslateService } from '@ngx-translate/core';
import { defer, fromEvent, iif, merge } from 'rxjs';
import {
concatMap,
debounceTime,
distinctUntilChanged,
map,
mapTo,
Expand Down Expand Up @@ -100,8 +101,10 @@ export class ConfigurationEffects {
setLocale$ = this.store.pipe(
select(getCurrentLocale),
mapToProperty('lang'),
whenTruthy(),
distinctUntilChanged(),
// https://github.com/ngx-translate/core/issues/1030
debounceTime(0),
whenTruthy(),
tap(lang => this.translateService.use(lang))
);

Expand Down

0 comments on commit 7750993

Please sign in to comment.