Skip to content

Add lazy property to ResourceOptions interface #58422

Closed as not planned
Closed as not planned

Description

@leonelvsc

Which @angular/* package(s) are relevant/related to the feature request?

core

Description

Looking at the actual implementation of the new resource api for signals it runs the loaderFn inside an effect which is fine because of the nature of the function it should run outside of the synchronous loop of the graph, i propose to add lazy: boolean to the ResourceOptions interface and make that a default of true. My concern is that new Angular devs are going to start using this new API without taking into consideration that the loaderFn will run at creation time if a request is added in the definition and has a value other than undefined.

Consider a plain app without lazy loading components or modules, at the start when the app boots all components that create a resource and have a value in the request signal will go and trigger the loaderFn degrading page loading performance.

It should behave like Observables combined with switchMap operators that run computations the first time a subscriber is added to the Observable

Proposed solution

Inside the loadEffect you could check for liveConsumers.length if there are no consumers and lazy is set to true then no call to loaderFn is made.


if (request.request === undefined) || (this.value[SIGNAL] as SignalNode<T>).liveConsumers.length === 0 ) ...

BUT: i don't know how to trigger the loaderFn when new consumer are added to the array with the current api

if (request.request === undefined) {

It's similar to set function checking for the version:

const prevVersion = (this.value[SIGNAL] as SignalNode<T>).version;

Alternatives considered

I think a hook function for SignalNode going live will be useful in this case

Activity

  1. added
    area: core
    on Oct 30, 2024
  2. modified the milestone: needsTriage on Oct 30, 2024
  3. imaksp commented on Nov 1, 2024

    @imaksp
    Contributor

    Hi, I don't see any issue with current behaviour, Tanstack query also starts fetching data automatically when query object is created, (& if enabled is true), here we don't have enabled support, and it doesn't start fetching if request is undefined. enabled field support would be helpful but without it current behaviour correct according to me.
    If developer wants to defer fetching he/she can defer whole component loading by using @defer on component or by making request field undefined.

  4. leonelvsc commented on Nov 1, 2024

    @leonelvsc
    ContributorAuthor

    Hi imaksp, take into account that resource api is not just component scoped, ie: it can be used inside services that can be injected in the root scope.

    I know about the undefined behaviour, I mention that on the first paragraph.

  5. imaksp commented on Nov 1, 2024

    @imaksp
    Contributor

    Hi, if you are using it in root level service then it will start fetching when service is created & if request is not undefined & response will stay cached in memory (as it won't get destroyed like component), I don't see anything wrong in this behaviour. this is could be helpful for storing global data.
    I believe data fetching should start as soon as resource is created , for better control explicit enabled field could be added. (if making request undefined seems confusing).

  6. leonelvsc commented on Nov 1, 2024

    @leonelvsc
    ContributorAuthor

    The point here is, why should you fetch data that is not consumed by no one?, that is the liveConsumers array inside a SignalNode.

    And as i said earlier:
    "
    My concern is that new Angular devs are going to start using this new API without taking into consideration that the loaderFn will run at creation time if a request is added in the definition and has a value other than undefined.

    Consider a plain app without lazy loading components or modules, at the start when the app boots all components that create a resource and have a value in the request signal will go and trigger the loaderFn degrading page loading performance.
    "

  7. imaksp commented on Nov 1, 2024

    @imaksp
    Contributor

    Hi, this could make sense in RxJS world , where it starts fetching only when someone subscribes, but now Angular is in process of making it optional. this is different API which also directly support Promises & I believe current behaviour is expected, and instead waiting for subscribe/consumers explicit enabled (with default true) like query would be great,
    anyways we can wait for official response on this.

  8. alxhub commented on Nov 1, 2024

    @alxhub
    Member

    This feature really wouldn't be aligned with our plans/goals around data fetching in Angular.

    In particular, we want to discourage architectures that use rendering to drive data fetching (that is, wait until data is requested from the UI before fetching it). In our experience, such architectures offer poor UX behavior, such empty content while loading and the potential for waterfalling loads.

    Our intention is to encourage architectures which lift data fetching higher up in the application stack, ideally at the level of routes or route pages.

  9. leonelvsc commented on Nov 1, 2024

    @leonelvsc
    ContributorAuthor

    I see, thanks for your response, but it's not just for UI, resource itself is a signal so can be used anywhere as you said, like routes, services and so on. In those cases liveConsumers.length will never be 0 ie: the router itself will be a consumer of that resource.

    Imagine that you define an abstract class that has a resource as a protected property, if you extend that class and never use that property the loaderFn will be called allways right?

  10. samuelfernandez commented on Nov 26, 2024

    @samuelfernandez

    This feature really wouldn't be aligned with our plans/goals around data fetching in Angular.

    In particular, we want to discourage architectures that use rendering to drive data fetching (that is, wait until data is requested from the UI before fetching it). In our experience, such architectures offer poor UX behavior, such empty content while loading and the potential for waterfalling loads.

    Our intention is to encourage architectures which lift data fetching higher up in the application stack, ideally at the level of routes or route pages.

    @alxhub just asking to make sure I understand your explanation on route pages, I understand routed components that are connected to the route through loadComponent, right?

    I understand that UIs that adopt incremental rendering, such as skeleton loading, or infinite scroll would somehow need UI driven data loading. Even though I agree that for most cases loading at the route with a resolver or similar is ideal. What are your thoughts on this? I'd like to make sure I am aligned with the design goals behind resource. (Note: it'd be great to document that in the docs site)

  11. imaksp commented on Nov 28, 2024

    @imaksp
    Contributor

    UI driven loading is already supported in new resource & it can be used when needed like for server paging on page change or on scroll for infinite scroll, resource will automatically refetch when request signal value changes, this is about adding lazy option for delaying initial fetch until someone subscribes & it does not align with its design goal.
    & you can still delay it by returning undefined to request, it could be useful if request/query is dependant on some value. its just not very obvious like enabled in tanstack query.

  12. johnsonav1992 commented on Dec 7, 2024

    @johnsonav1992

    While I do see @alxhub 's point of encouraging data fetching to happen sooner rather than later, I still think it is essential that there at least be an enabled property just like in Tanstack Query that allows finer-grained control of when the loader runs. I'm already finding in my app that I'm building that not having that control will be problematic.

  13. angular-automatic-lock-bot commented on Jan 7, 2025

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  14. locked and limited conversation to collaborators on Jan 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area: corecross-cutting: signals

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions