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

Reconsider RootStore update behavior #858

Open
haukesomm opened this issue Mar 21, 2024 · 2 comments
Open

Reconsider RootStore update behavior #858

haukesomm opened this issue Mar 21, 2024 · 2 comments
Assignees
Labels
discussion Extra attention is needed enhancement New feature or request

Comments

@haukesomm
Copy link
Collaborator

haukesomm commented Mar 21, 2024

Current behavior

Currently, a RootStore's value is only updated (i.e. data flow emits a new value) if the Store receives a new value.
If the Store receives a value that is equal to the current value, no value is emitted.

This is due to the fact that a MutableStateFlow is used to keep track of the current value which is explicitly designed to behave this way.

Why this is problematic

Imagine a Lens that is used to strip illegal characters from an input (e.g. characters from a phone number). The expected behavior would be like this:

  1. Add invalid characters to an input field with a valid input
  2. Invalid characters get stripped by the Lens
  3. The store is updated
  4. The formatting Lens passes the sanitized value upstream to the input
  5. The input field is updated to have the sanitized value

Instead, the following happens:

  1. Add invalid characters to an input field with a valid input
  2. Invalid characters get stripped by the Lens
  3. The store is not updated since the sanitized input is the same as the current value
  4. Nothing is passed upstream
  5. The input field keeps the illegal input

Example code

val allowedCharacters = "abc".toCharArray().toSet()

val sanitizingLens: Lens<String, String> = lensOf(
    format = { it },
    parse = { it.filter { c -> c in allowedCharacters } }
)

val store = storeOf("").map(sanitizingLens)

div("space-y-4") {
    input {
        type("text")
    }.changes.values() handledBy store.update

    p {
        store.data.renderText(into = this)
    }
}

Proposal

Consider one of the following options:

  1. Introduce a flag to explictly update the Store's data on any new input
  2. Change the Store's default behavior to always emit a new value, regardless of the equality of the input
@haukesomm haukesomm added enhancement New feature or request discussion Extra attention is needed labels Mar 21, 2024
@haukesomm haukesomm self-assigned this Mar 21, 2024
@Lysander
Copy link
Collaborator

This needs carefully investigation in order to keep the current and long time established behaviour stable. I am in favor of some flag or somehow "store"-overloading / variant / behaviour approach right now. On the other hand besides "legacy" code we want to support users with our framework to write idiomatic code. That would mean that the current behaviour should rather be treated and branded as deprecated, which in dead would mean some API change.

@haukesomm
Copy link
Collaborator Author

haukesomm commented Mar 22, 2024

In an internal meeting @Lysander suggested to change the default behavior of the RootStore class and filter out identical values on the rendering level instead.

This way the stores would always update but identical contents would not be rendered multiple times.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion Extra attention is needed enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants