Skip to content

Commit

Permalink
feat: mark all state properties as readonly to make it more clear t…
Browse files Browse the repository at this point in the history
…he state is not meant to be modified directly
  • Loading branch information
MrWolfZ committed Apr 24, 2018
1 parent 89bba06 commit 291e0da
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 88 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ This release requires TypeScript >=2.8.0 for the conditional type support.
* due to rework of `updateArray`, `updateGroup`, and `updateRecursive` update functions it is now invalid to call any of these functions without parameters (which made no sense anyway) but it is still possible to call the functions with an empty array as parameter (which is useful in dynamic situations)
* remove `payload` property from all actions and move corresponding properties into action itself ([6f955e9](https://github.com/MrWolfZ/ngrx-forms/commit/6f955e9))
* change the reducer created by `createFormGroupReducerWithUpdate` to only apply the provided update function objects if the state changed as a result of applying the action to the form state (this is only relevant in cases where the update function is closing over variables that may change without the form state changing in which case you can simply manually call the `formGroupReducer` and the `updateGroup` function); this provides a performance improvement for large form states and their update function objects ([b1f49cd](https://github.com/MrWolfZ/ngrx-forms/commit/b1f49cd))
* mark all state properties as `readonly` to make it more clear the state is not meant to be modified directly

#### Features

Expand Down
2 changes: 1 addition & 1 deletion src/array/reducer/clear-async-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function clearAsyncErrorReducer<TValue>(

if (state.errors.hasOwnProperty(name)) {
errors = { ...state.errors };
delete errors[name];
delete (errors as any)[name];
}

const pendingValidations = state.pendingValidations.filter(v => v !== action.name);
Expand Down
8 changes: 4 additions & 4 deletions src/array/reducer/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { inferredStateReducer } from '../../inferred-reducer';
import { computeArrayState, FormArrayState, InferredControlState } from '../../state';

export function dispatchActionPerChild<TValue>(
controls: InferredControlState<TValue>[],
controls: ReadonlyArray<InferredControlState<TValue>>,
actionCreator: (controlId: string) => Actions<TValue>,
): InferredControlState<TValue>[] {
): ReadonlyArray<InferredControlState<TValue>> {
let hasChanged = false;
const newControls = controls
.map(state => {
Expand All @@ -18,9 +18,9 @@ export function dispatchActionPerChild<TValue>(
}

function callChildReducers<TValue>(
controls: InferredControlState<TValue>[],
controls: ReadonlyArray<InferredControlState<TValue>>,
action: Actions<TValue[]>,
): InferredControlState<TValue>[] {
): ReadonlyArray<InferredControlState<TValue>> {
let hasChanged = false;
const newControls = controls
.map(state => {
Expand Down
2 changes: 1 addition & 1 deletion src/control/reducer/clear-async-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function clearAsyncErrorReducer<TValue extends FormControlValueTypes>(

if (state.errors.hasOwnProperty(name)) {
errors = { ...state.errors };
delete errors[name];
delete (errors as any)[name];
}

const pendingValidations = state.pendingValidations.filter(v => v !== action.name);
Expand Down
2 changes: 1 addition & 1 deletion src/group/reducer/clear-async-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function clearAsyncErrorReducer<TValue extends KeyValue>(

if (errors.hasOwnProperty(name)) {
errors = { ...state.errors };
delete errors[name];
delete (errors as any)[name];
}

const pendingValidations = state.pendingValidations.filter(v => v !== action.name);
Expand Down
4 changes: 2 additions & 2 deletions src/group/reducer/set-value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ export function setValueReducer<TValue extends KeyValue>(
.reduce((c, key) => {
// tslint:disable-next-line:prefer-conditional-expression
if (!state.controls[key]) {
c[key] = createChildState<TValue[string]>(`${state.id}.${key}`, value[key]);
Object.assign(c, { [key]: createChildState<TValue[string]>(`${state.id}.${key}`, value[key]) });
} else {
c[key] = inferredStateReducer(state.controls[key], new SetValueAction(state.controls[key].id, value[key]));
Object.assign(c, { [key]: inferredStateReducer(state.controls[key], new SetValueAction(state.controls[key].id, value[key])) });
}
return c;
}, {} as FormGroupControls<TValue>);
Expand Down
2 changes: 1 addition & 1 deletion src/group/reducer/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function dispatchActionPerChild<TValue extends KeyValue>(
let hasChanged = false;
const newControls = Object.keys(controls)
.reduce((c, key) => {
c[key] = inferredStateReducer(controls[key], actionCreator(controls[key].id));
Object.assign(c, { [key]: inferredStateReducer(controls[key], actionCreator(controls[key].id)) });
hasChanged = hasChanged || c[key] !== controls[key];
return c;
}, {} as FormGroupControls<TValue>);
Expand Down
Loading

0 comments on commit 291e0da

Please sign in to comment.