Skip to content
This repository was archived by the owner on Aug 23, 2022. It is now read-only.

Commit 405d42c

Browse files
committed
Removing ad-hoc caching mechanism and fixing logic for setInitial. Fixes #782
1 parent 7ce0c1a commit 405d42c

File tree

3 files changed

+10
-35
lines changed

3 files changed

+10
-35
lines changed

src/reducers/form-actions-reducer.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import isValidityInvalid from '../utils/is-validity-invalid';
1515
import fieldActions from '../actions/field-actions';
1616
import toPath from '../utils/to-path';
1717
import initialFieldState from '../constants/initial-field-state';
18-
import { fieldOrForm, getMeta, updateFieldState } from '../utils/create-field';
18+
import { getMeta, fieldOrForm, updateFieldState } from '../utils/create-field';
1919
import assocIn from '../utils/assoc-in';
2020
import getFormValue from '../utils/get-form-value';
2121

@@ -30,7 +30,6 @@ const resetFieldState = (field, customInitialFieldState) => {
3030
intents.push({ type: 'load' });
3131
resetValue = loadedValue;
3232
}
33-
3433
return fieldOrForm(
3534
getMeta(field, 'model'),
3635
resetValue,
@@ -48,7 +47,10 @@ const setInitialFieldState = (customInitialFieldState) => (field, key) => {
4847
});
4948
}
5049

51-
if (field.$form) return mapValues(field, resetFieldState);
50+
if (field.$form) {
51+
return mapValues(field, (fieldState) =>
52+
resetFieldState(fieldState, customInitialFieldState));
53+
}
5254

5355
return updateFieldState(customInitialFieldState, {
5456
value: field.value,
@@ -359,8 +361,7 @@ export function createFormActionsReducer(options) {
359361
// If the form is invalid (due to async validity)
360362
// but its fields are valid and the value has changed,
361363
// the form should be "valid" again.
362-
if ((!Object.keys(parentForm.$form.validity).length
363-
|| !parentForm.$form.validity)
364+
if ((!parentForm.$form.validity || !Object.keys(parentForm.$form.validity).length)
364365
&& !parentForm.$form.valid
365366
&& isValid(parentForm, { async: false })) {
366367
return {

src/reducers/form/change-action-reducer.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,7 @@ export default function changeActionReducer(state, action, localPath) {
135135
const updatedState = assocIn(state, localPath, updatedField, (form) => {
136136
if (!form.$form) return form;
137137

138-
const formValue = action.state
139-
? get(action.state, form.$form.model)
140-
: getFormValue(form);
138+
const formValue = getFormValue(form);
141139

142140
const formUpdates = {
143141
...form.$form,

src/reducers/forms-reducer.js

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,6 @@ const defaultStrategy = {
2424
toJS: identity,
2525
};
2626

27-
const modelCache = {};
28-
29-
function cacheModelState(state) {
30-
const cacheId = Date.now();
31-
32-
modelCache[cacheId] = state;
33-
34-
return cacheId;
35-
}
36-
37-
function cacheReducer(reducer, model, cacheId) {
38-
return (state, action) => {
39-
const newState = reducer(state, action);
40-
41-
modelCache[cacheId][model] = newState;
42-
43-
return newState;
44-
};
45-
}
46-
4727
function createFormCombiner(strategy = defaultStrategy) {
4828
function createForms(forms, model = '', options = {}) {
4929
const formKeys = Object.keys(forms);
@@ -59,8 +39,6 @@ function createFormCombiner(strategy = defaultStrategy) {
5939
...formOptions,
6040
} = optionsWithDefaults;
6141

62-
const cacheId = cacheModelState({});
63-
6442
formKeys.forEach((formKey) => {
6543
const formValue = forms[formKey];
6644
const subModel = getSubModelString(model, formKey);
@@ -73,12 +51,10 @@ function createFormCombiner(strategy = defaultStrategy) {
7351
initialState = null;
7452
}
7553

76-
modelReducers[formKey] = cacheReducer(
77-
strategy.modeled(formValue, subModel), subModel, cacheId);
54+
modelReducers[formKey] = strategy.modeled(formValue, subModel);
7855
initialFormState[formKey] = initialState;
7956
} else {
80-
modelReducers[formKey] = cacheReducer(
81-
strategy.modelReducer(subModel, formValue), subModel, cacheId);
57+
modelReducers[formKey] = strategy.modelReducer(subModel, formValue);
8258
initialFormState[formKey] = strategy.toJS(formValue);
8359
}
8460
});
@@ -88,7 +64,7 @@ function createFormCombiner(strategy = defaultStrategy) {
8864
[key]: (state, action) => strategy.formReducer(model, initialFormState, {
8965
plugins,
9066
...formOptions,
91-
})(state, { ...action, state: Object.assign({}, modelCache[cacheId]) }),
67+
})(state, action),
9268
};
9369
}
9470

0 commit comments

Comments
 (0)