Skip to content

Should be able to yield chained events from a computed var #5956

@masenf

Description

@masenf

Describe the Features
When a computed var is being computed, you cannot reliably modify the state nor chain to any other event (especially background events).

It would be really nice if you could just yield chained events from a computed var, then return the final value for the var.

It would also be really nice if the get_delta computation in state, would re-run the computed var calculation until some interation limit or until no further dirty vars are generated. That way computed vars could directly modify state state vars and have them reliably persisted and sent to the frontend.

  • What is the purpose of the feature?
    Make computed vars more useful/less edge cases.

  • Show an example / use cases for the new feature.

class MyState(rx.State):
    _user_token: str
    logged_in: bool = False

    @rx.var
    def user_is_valid(self) -> bool:
        if not self._user_token:
            return False
        if not validate(self._user_token):
            yield rx.toast("Token was found to be invalid")
            self.logged_in = False
            self._user_token = ""
            return False
        return True

In this case, the user_is_valid setting self._user_token to blank would trigger a re-calculation of user_is_valid immediately, which would take the early exit path. The delta will include logged_in = False. A toast will be displayed to the user from the event which triggered this recalculation of user_is_valid.

Since computed vars are always recalculated during delta computation and the lock is held during this process, it's always safe to manipulate the state, the problem was that the final delta was not updated based on changes that occured to the state during computed var resolution.

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestA feature you wanted added to reflex

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions