Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement isChanged flags #125

Merged
merged 2 commits into from
Sep 5, 2022
Merged

feat: implement isChanged flags #125

merged 2 commits into from
Sep 5, 2022

Conversation

mixvar
Copy link
Collaborator

@mixvar mixvar commented Aug 28, 2022

closes #117

published as 0.2.21-beta.1

demo: https://codesandbox.io/s/pizza-form-v2-czt6cw?file=/src/order-pizza-form.tsx (see updated pizza form example!)

@mixvar mixvar requested a review from pidkopajo August 28, 2022 22:47
controller?: FormController
): FormHandle<Values, Err> => {
const { state, methods } = useFormtsContext<Values, Err>(controller);

// TODO: create cache for form handle atoms to avoid memory leaks and redundant computations
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will be dealt with in next PR

@pidkopajo pidkopajo added the VB label Sep 4, 2022
value,
changed,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can compute changed here based on value, without the need to introduce a new Atom out of .fuse (line 57), by simply comparing value and initialValue in the combinator body

Suggested change
changed,
changed: !deepEqual(value, initialValue)

Copy link
Collaborator Author

@mixvar mixvar Sep 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd keep it - there is a small difference - as a separate atom deepEqual will only run on value changes, but when put in the combinator fn it would run on change of any dependency (and it is somewhat heavy operation). I think we will need to remove FieldDependenciesAtom and move error and validating state computations here in the future as part #127 (so it will make bigger difference)

Comment on lines +70 to +81
Atom.fuse(
(...fieldsChanged) => fieldsChanged.some(Boolean),
...values(Schema).map(field => {
const fieldLens = impl(field).__lens;
const initialValue = fieldLens.get(state.initialValues);
const fieldAtom = Atom.entangle(state.values, fieldLens);
return Atom.fuse(
fieldValue => !deepEqual(fieldValue, initialValue),
fieldAtom
);
})
),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't deepEqual on state.values and state.initialValues be enough?

Suggested change
Atom.fuse(
(...fieldsChanged) => fieldsChanged.some(Boolean),
...values(Schema).map(field => {
const fieldLens = impl(field).__lens;
const initialValue = fieldLens.get(state.initialValues);
const fieldAtom = Atom.entangle(state.values, fieldLens);
return Atom.fuse(
fieldValue => !deepEqual(fieldValue, initialValue),
fieldAtom
);
})
),
Atom.fuse(values => !deepEqual(values, state.initialValues), state.values),

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, this is a bit of an optimisation - we don't throw the entire form values into deepEqual every time any field changes, but only compare the field that did change. If the entire from is nested in object field then it does not matter, but I thought it could be worth it in some cases.

@mixvar mixvar merged commit 92515a8 into master Sep 5, 2022
@mixvar mixvar deleted the feat/is-changed-flags branch September 5, 2022 17:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

add .isChanged getter to FieldHandle and FormHandle
2 participants