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

deprecate get_ methods for router_data, use BaseVars instead #1967

Merged
merged 5 commits into from
Oct 17, 2023

Conversation

Lendemor
Copy link
Collaborator

@Lendemor Lendemor commented Oct 13, 2023

New API for accessing router_data.

Deprecate all the previous methods: get_token, get_headers, ....

Introduced 3 new BaseVar in State: State.headers, State.session, State.page

Will fix the units tests soon.

def index() -> rx.Component:
    return rx.vstack(
        rx.text(f"State.page.path: {State.page.path}"),
        rx.text(f"State.page.raw_path: {State.page.raw_path}"),
        rx.text(f"State.page.full_path: {State.page.full_path}"),
        rx.text(f"State.page.full_raw_path: {State.page.full_raw_path}"),
        rx.text(f"State.page.host: {State.page.host}"),
        rx.text(f"State.headers.origin: {State.headers.origin}"),
        rx.text(f"State.session.client_token: {State.session.client_token}"),
        rx.text(f"State.session.client_ip: {State.session.client_ip}"),
        rx.text(f"State.session.session_id: {State.session.session_id}"),
        rx.foreach(State.page.params, lambda k, i: rx.text(f"{k[0]}: {k[1]}")),
    )

@masenf
Copy link
Collaborator

masenf commented Oct 13, 2023

My initial gut reaction is, "what if i want my own state vars called session or page?"

To provide any API on the State class naturally precludes users from choosing the names they would prefer, but maybe we could make these like second-order names, like state.router.page and state.router.session? At least then we're only blocking a single name ("router"). Or maybe reflex would be a better entry point? state.reflex.page? Then we could put all other public API entry points hanging off of .reflex and that would be the only name users couldn't use.

Thoughts?

@Lendemor
Copy link
Collaborator Author

Lendemor commented Oct 13, 2023

My initial gut reaction is, "what if i want my own state vars called session or page?"

To provide any API on the State class naturally precludes users from choosing the names they would prefer, but maybe we could make these like second-order names, like state.router.page and state.router.session? At least then we're only blocking a single name ("router"). Or maybe reflex would be a better entry point? state.reflex.page? Then we could put all other public API entry points hanging off of .reflex and that would be the only name users couldn't use.

Thoughts?

I think it's a good point, router has much less chance of being used yeah.

We could make a

class RouterData(Base):
    ...

that contains everything.

My initial idea was that I didn't want to have something too long to access when rendering.
State.router.page.full_path can be a bit long, but still acceptable I think.

Copy link
Collaborator

@masenf masenf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm realizing that a consequence of this approach is that we're now sending back all of the router data vars over the websocket with any navigation event. Maybe that's okay, it's certainly more convenient if we need to render these values.

@Lendemor
Copy link
Collaborator Author

I'm realizing that a consequence of this approach is that we're now sending back all of the router data vars over the websocket with any navigation event. Maybe that's okay, it's certainly more convenient if we need to render these values.

I think it's okay yeah.
If there is an issue with it later we can figure out a solution.

@picklelo picklelo merged commit b1bab12 into main Oct 17, 2023
@picklelo picklelo deleted the lendemor/state_router_api branch November 1, 2023 19:10
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.

3 participants