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

Only serialize used rx.Base fields #3845

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

abulvenz
Copy link
Contributor

@abulvenz abulvenz commented Aug 27, 2024

If you check the payloads when running the example below, you might notice that value is only serialized when you comment in line 43 where the value is displayed.
Fields are checked on class level, so always the union of all used fields will be serialized (Not per usage, which would be cool, but more sophisticated.)

from __future__ import annotations
import reflex as rx
from reflex.base import SlimBase


def on_frontend_exception(exception: Exception) -> None:
    pass

app: rx.App = rx.App(frontend_exception_handler=on_frontend_exception)


class SmartObjectChild(SlimBase):
    name: str = "Smart Object Child"
    value: int = 0

class SmartObject(SlimBase):
    name: str = "Smart Object"
    value: int = 0
    child: SmartObjectChild = SmartObjectChild()
    children: list[SmartObjectChild] = [SmartObjectChild(), SmartObjectChild()]

    def increment(self):
        self.value += 1

    def decrement(self):
        self.value -= 1


class LSpace(rx.State):
    computed_var: str = "Computed var"
    some_list: list[SmartObject] = [SmartObject(), SmartObject()]

    smartie: SmartObject = SmartObject()

    def set_smartie_name(self, name: str) -> None:
        self.smartie.name = name


def index() -> rx.Component:
    return rx.box(
        rx.heading("Test smart object!"),
        rx.foreach(LSpace.some_list, lambda x: rx.box(rx.text(x.name))),
       # rx.text(LSpace.smartie.value),
        rx.text(LSpace.smartie.child.name),        #rx.text(LState.smartie.child.value),

        rx.foreach(LSpace.smartie.children, lambda x: rx.box(rx.text(x.name))),


        rx.button("Decrement", on_click=LSpace.set_smartie_name("Decremented")),
        rx.button("Increment", on_click=LSpace.set_smartie_name("Incremented")),
    )

app.add_page(index)

Merge remote-tracking branch 'upstream/main' into do-not-serialize-unused-rxbase-fields
Merge remote-tracking branch 'upstream/main' into do-not-serialize-unused-rxbase-fields
@benedikt-bartscher
Copy link
Contributor

@abulvenz I think we can rebase this and mark it as ready
should we make SlimBase the default Base?

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

Successfully merging this pull request may close these issues.

2 participants