reflex icon indicating copy to clipboard operation
reflex copied to clipboard

Cannot `.mark_dirty` a ComputedVar directly in 0.6.0+

Open masenf opened this issue 1 year ago β€’ 3 comments

Describe the bug Because the computed var assigned to the State class isn't really a ComputedVar, but actually a ToOperation with a _js_expr based on the state, when _cache_attr is accessed to delete it (in mark_dirty), it returns the wrong attribute and doesn't actually clear the cache.

To Reproduce

import datetime

import reflex as rx


class State(rx.State):
    @rx.var(cache=True)
    def dt(self) -> datetime.datetime:
        return datetime.datetime.now()

    def dt_reset(self):
        type(self).dt.mark_dirty(self)
        self.dirty_vars.add("dt")
        self._mark_dirty()


def index() -> rx.Component:
    # Welcome Page (Index)
    return rx.container(
        rx.color_mode.button(position="top-right"),
        rx.vstack(
            rx.heading(State.dt),
            rx.button("Reset Cache", on_click=State.dt_reset),
        ),
        rx.logo(),
    )


app = rx.App()
app.add_page(index)

Expected behavior In reflex 0.5.10, each time the Reset Cache button is clicked, the timestamp updates.

In reflex 0.6.0, nothing happens.

Specifics (please complete the following information):

  • Python Version: 3.12
  • Reflex Version: 0.6.0+
  • OS:
  • Browser (Optional):

Additional context I think the _cache_attr should be based on some data intrinsic to the ComputedVar, not something that could be changed downstream by some operation

masenf avatar Oct 18 '24 20:10 masenf

As a workaround, it seems you can do self.computed_vars["dt"].mark_dirty(self) 😁

masenf avatar Oct 18 '24 20:10 masenf

generally speaking, it's hard to make this work all the time, what if the computed var was an object and doing .make_dirty on it would result on that field itself like obj['make_dirty'], in which case this operation isn't coherent

adhami3310 avatar Oct 21 '24 20:10 adhami3310