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

Make it easier to provide context to fullstack #2515

Merged
merged 9 commits into from
Jun 19, 2024

Conversation

ealmloff
Copy link
Member

@ealmloff ealmloff commented Jun 13, 2024

This PR changes the context function in the launch builder to provide context throughout the fullstack application including in server functions. You can now consume context in server functions with an extractor:

LaunchBuilder::new().with_context(1234567890u32).launch(app);

#[server]
async fn get_server_data() -> Result<String, ServerFnError> {
    let FromContext(context): FromContext<u32> = extract().await?;
    let text = reqwest::get("https://httpbin.org/ip").await?.text().await?;
    Ok(format!("context {context} Your IP address is: {text} 🌎"))
}

Closes #2131
Closes #1688

@ealmloff ealmloff added enhancement New feature or request fullstack related to the fullstack crate breaking This is a breaking change labels Jun 13, 2024
@ealmloff ealmloff marked this pull request as ready for review June 13, 2024 19:05
@ealmloff ealmloff merged commit b0caf44 into DioxusLabs:main Jun 19, 2024
5 of 10 checks passed
@ealmloff ealmloff deleted the fullstack-context branch June 19, 2024 01:38
@scullionw
Copy link

how would .with_context be used to pass resources that require async to be built?

like if we want to store a database connection? (let conn = connect_to_db().await)

something like:

LaunchBuilder::new().with_context_future(async { connect_to_db().await }).launch(app);

?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking This is a breaking change enhancement New feature or request fullstack related to the fullstack crate
Projects
None yet
3 participants