Giter VIP home page Giter VIP logo

Comments (5)

gbj avatar gbj commented on July 4, 2024

Not sure what the goal is during the example -- I see the panic and it is during initial route generation on the server. This kind of error would be suppressed during route generation using something like a resource. Is the idea here to run some async task, independent of the rendering process, but while rendering? There's no way to wait for the task you spawned to finish before rendering HTML.

from leptos.

gbj avatar gbj commented on July 4, 2024

Or of course you can just not unwrap in the places you're unwrapping; this version works fine:

#[server]
pub async fn get_user_data() -> Result<UserData, ServerFnError> {
    use actix_web::HttpRequest;
    use leptos_actix::extract;

    let req: HttpRequest = extract().await?;
    println!("Method: {:?}", req.method());

    return Ok(UserData {
        name: "Luxalpa".to_string(),
    });
}

#[component]
pub fn FrontPage() -> impl IntoView {
    spawn_local(async move {
        if let Ok(site_data) = get_user_data().await {
        println!("Site Data: {:?}", site_data);

        }
    });

    view! { <div> "Front Page" </div> }
}

from leptos.

luxalpa avatar luxalpa commented on July 4, 2024

The code above is just a minimalistic example. My application is actually using a resource that is set as pending like this:

let resource = create_blocking_resource(
    move || {},
    // TODO: Figure out if this future needs to be manually cleaned up somehow
    move |_| future::pending::<CardWithPrinting>(),
);

and then later (in the same function) it is fetching the data:

spawn_local(async move {
    let result = get_card_printings(ids).await.unwrap().0;
    self.add_to_cache(result);
});

and in add_to_cache it is setting the resource:

// Notify resources
if let Some(resources) = in_use.remove(&card.printing.id) {
    resources.iter().for_each(|r| {
        r.try_set(card.clone());
    });
}

Because the application needs to cache the data that comes from these resources while also providing SSR for them too. This architecture was inspired by leptos_query, but apparently it does not work well with server functions.

from leptos.

gbj avatar gbj commented on July 4, 2024

To be clear the panic is unrelated to server functions, it specifically has to do with unwrapping the attempt to extract an HttpRequest during the server's initial route generation, which runs the application once to figure out all the routes you've defined. It suppresses resource loads during this, but the spawn_local still runs, and panics when unwrapping the HttpRequest extractor. (There is no HTTP request during that route generation process.)

In the actual example you provide, I assume that just not unwrapping here will fix your issue:

spawn_local(async move {
    let result = get_card_printings(ids).await.unwrap().0;
    self.add_to_cache(result);
});

// instead
spawn_local(async move {
    // I'm missing the .0 here but hopefully you see what I mean
    if let Ok(result) = get_card_printings(ids).await {
      self.add_to_cache(result);
    }
});

and likewise removing the .unwrap() in get_card_printings and using ? instead if the extractor fails.

from leptos.

luxalpa avatar luxalpa commented on July 4, 2024

ok, thanks a lot, that actually explains some things and allowed me to workaround this.

from leptos.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.