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

Cleanup "Resource B" depends on "Resource A" #3372

Open
dev-inigmas opened this issue Dec 16, 2024 · 3 comments
Open

Cleanup "Resource B" depends on "Resource A" #3372

dev-inigmas opened this issue Dec 16, 2024 · 3 comments
Labels
good first issue Good for newcomers

Comments

@dev-inigmas
Copy link

Is your feature request related to a problem? Please describe.

The Leptos 0.7 Release Notes shows:

let user = Resource::new(|| (), |_| user_id());
let posts = Resource::new(
    move || user.get(),
    move |_| async move {
        let user = user.await?;
        get_posts(user).await
    },
);

view! {
    <Suspense>
        ...

However, this causes Leptos to throw warnings in development mode - which may obscure warnings that a developer would need to see.

Putting the Resource definitions in a <Suspense> causes hydration issues.
Using .track() rather than .get() also doesn't work, because Resources are memoized.

Describe the solution you'd like

Either squelch warnings emitted while using this pattern. Or, allow .track() to be sufficient within the source function. Maybe, there could be a different Resource generating function that doesn't require unique return values from the source function for memoization.

Describe alternatives you've considered

I guess I could return a random value from the source function...

@dev-inigmas
Copy link
Author

This hack appears to work. I suspect that's possible for it to not work perfectly every time though:

  let my_resource: Resource<MyResource> = Resource::new(
    move || {
      resource_a.track();
      resource_b.track();
      RandomState::new().build_hasher().finish()
    },
    move |_| fetch_my_resource(resource_a, resource_b),
  );

@gbj gbj added the good first issue Good for newcomers label Dec 20, 2024
@gbj
Copy link
Collaborator

gbj commented Dec 20, 2024

The correct answer here in the short term is definitely just to suppress the false-positive warning. (My reading of the issue is that the example in the release notes works as expected, but issues a warning, which is why I say "false positive." Let me know if this is incorrect.)

In the medium-to-long term removing the memoization in resources so that .track() can work may be the right idea (the current API has generated some confusion, see e.g., #3308), but it would be a breaking change so couldn't happen until 0.8 in any case.

Do you have any interest in working on a PR?

@dev-inigmas
Copy link
Author

I'm interested in contributing. Though my time is very limited. I'm also hoping to reproduce some errors for other tickets.

I use Leptos at work - and right now, management is much more interested in getting new features delivered. So, I've been prioritizing finding workarounds for issues as they come up. I got things working well enough that I merged my migration into main.

Maybe I can get some work done for Leptos itself this weekend.

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

No branches or pull requests

2 participants