ZupIT/nimbus

setState may not be fast enough

Tiagoperes opened this issue · 1 comments

When typing into a text field with its value binded to a server driven state, if we type too fast, we see the application is not as responsive as it should be.

I tested this on Nimbus Compose. This is probably a problem within the core, but could it not be?

Considering it's a problem in the core, what's making setState slow? From what I remember of the implementation, it has a very low cost: O(n) where n is the number of children of the node that declared the state. In my example (next), n = 2, which is extremely small.

If we don't find a way to do this, we'll need a work around for components like text fields, which is not something I like =(.

Example I used:

{
  "_:component": "layout:column",
  "state": {
    "id": "street",
    "value": ""
  },
  "children": [
    {
      "_:component": "store:textInput",
      "properties": {
        "value": "@{street}",
        "label": "Street",
        "onChange": [
          {
            "_:action": "setState",
            "properties": {
              "path": "street",
              "value": "@{onChange}"
            }
          }
        ]
      }
    },
    {
      "_:component": "layout:text",
      "properties": {
        "text": "Current value: @{street}"
      }
    }
  ]
}

My TextInput implementation:

typealias InputCallback = (value: String) -> Unit

@Composable
fun TextInput(
    label: String,
    value: String,
    onChange: InputCallback? = null,
    onBlur: InputCallback? = null,
    onFocus: InputCallback? = null,
) {
    TextField(
        value = value,
        onValueChange = {
            if (onChange != null) onChange(it)
        },
        label = { Text(label) },
        modifier = Modifier.onFocusChanged {
            if (it.isFocused && onFocus != null) onFocus(value)
            else if (onBlur != null) onBlur(value)
        }
    )
}

This part of the code has been fully rewritten and is not just a translation of Beagle Web anymore.

#51