Skip to content

Commit

Permalink
angular: Add service method to get locale
Browse files Browse the repository at this point in the history
When querying the state from the `JsonFormsAngularService` via its `getState` method, the whole state is cloned.
This could lead to performance issues with the angular material renderer set because the `NumberControlRenderer` queried the state on every change detection cycle to get the locale. Thus, the whole state was cloned for every control of these types on every change.
To fix this, the `JsonFormsAngularService` now allows to query the locale directly without cloning the state

* Add method `getLocale` to `JsonFormsAngularService` that does not clone the state
* Use new method in `NumberControlRenderer`

Signed-off-by: Lucas Koehler <lkoehler@eclipsesource.com>
  • Loading branch information
lucas-koehler committed Sep 8, 2022
1 parent 908372a commit 867789c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
3 changes: 1 addition & 2 deletions packages/angular-material/src/controls/number.renderer.ts
Expand Up @@ -25,7 +25,6 @@
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { JsonFormsAngularService, JsonFormsControl } from '@jsonforms/angular';
import {
getLocale,
isIntegerControl,
isNumberControl,
or,
Expand Down Expand Up @@ -138,7 +137,7 @@ export class NumberControlRenderer extends JsonFormsControl {
this.max = this.scopedSchema.maximum;
this.multipleOf = this.scopedSchema.multipleOf || defaultStep;
const appliedUiSchemaOptions = merge({}, props.config, this.uischema.options);
const currentLocale = getLocale(this.jsonFormsService.getState());
const currentLocale = this.jsonFormsService.getLocale();
if (this.locale === undefined || this.locale !== currentLocale) {
this.locale = currentLocale;
this.numberFormat = new Intl.NumberFormat(this.locale, {
Expand Down
4 changes: 4 additions & 0 deletions packages/angular/src/jsonforms.service.ts
Expand Up @@ -186,6 +186,10 @@ export class JsonFormsAngularService {
}
}

getLocale(): string | undefined {
return this._state.i18n?.locale;
}

setLocale(locale: string): void {
this._state.i18n.locale = locale;
this.updateSubject();
Expand Down

0 comments on commit 867789c

Please sign in to comment.