Repository navigation
Description
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
imaksp commentedon Nov 1, 2024
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.
enabledfield 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.leonelvsc commentedon Nov 1, 2024
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.
imaksp commentedon Nov 1, 2024
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
enabledfield could be added. (if making request undefined seems confusing).leonelvsc commentedon Nov 1, 2024
The point here is, why should you fetch data that is not consumed by no one?, that is the
liveConsumersarray inside aSignalNode.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.
"
imaksp commentedon Nov 1, 2024
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.
alxhub commentedon Nov 1, 2024
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.
leonelvsc commentedon Nov 1, 2024
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.lengthwill 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
loaderFnwill be called allways right?samuelfernandez commentedon Nov 26, 2024
@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)
imaksp commentedon Nov 28, 2024
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,
resourcewill 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
undefinedto request, it could be useful if request/query is dependant on some value. its just not very obvious likeenabledin tanstack query.johnsonav1992 commentedon Dec 7, 2024
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
enabledproperty 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.angular-automatic-lock-bot commentedon Jan 7, 2025
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.