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: extend useStableO to support an optional Eq argument #8

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

ahrjarrett
Copy link

Currently users can only provide primitive values to useStableO, which means it effectively doesn't support Options containing any non-primitive types.

The current workaround is to use useStable and manually wrap the Eq in O.getEq. Handling this for users is the reason useStableO exists, so it seems counter-intuitive to have users drop down to a lower-level construct, especially since it's not immediately obvious that that combination is how we support this use case.

It would be nice if useStableO supported this out of the box.

This PR extends useStableO to support this use case by adding an optional second argument to useStableO, which is the custom Eq that, if provided, will be used to determine whether state should be updated.

By having the argument default to Eq.eqStrict, this new behavior is opt-in. Things will continue working as before for existing users, so a minor bump is all that's necessary.

I can add tests and documentation if this is a change y'all think makes sense, and I'm happy to make whatever adjustments that are requested during review.

===

I also added useStableRef, which works the same way as useStable: setting ref.current will only succeed when the values are different according to the Eq they provided. I think it probably makers sense for that to be in a separate PR, but I thought it would be useful to get feedback on the addition.

Copy link
Member

@jleider jleider left a comment

Choose a reason for hiding this comment

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

Can you also add some tests for the new functionality?

useStable(initState, E.getEq(Eq.eqStrict, Eq.eqStrict));
export function useStableE<E, A>(initialState: Either<E, A>): StateTuple<Either<E, A>>;
export function useStableE<E, A>(initialState: Either<E, A>, leftEq: Eq<E>, rightEq: Eq<A>): StateTuple<Either<E, A>>;
export function useStableE<E, A>(initialState: Either<E, A>, leftEq?: Eq<E>, rightEq?: Eq<A>): StateTuple<Either<E, A>> {
Copy link
Member

Choose a reason for hiding this comment

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

Its possible with this signature that a user could provide a left but not a right Eq or vice versa if undefined is passed as a left. In that case this function does not behave as you would expect since its explicitly checking if both exist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants