Created hook called multiple times
okonomiyaki3000 opened this issue · 4 comments
The created hook seems to get called on all components every time any new component is created. If that can't be helped then it needs to be idempotent but it's not. If vm.$options.subscriptions
is a function, it will be called again each and every time a component is created. So if you have multiple components on a page, and one of them does some expensive things like making some requests in the subscriptions
function, it will end up making those requests again and again for each subsequently created component.
@okonomiyaki3000 This is the expected result.
My guess it that you want share Observable's pipe line between Components
Try operator .share()
pass over components or other ajax callback resolutions like inflight
No. This cannot possibly be what anyone would expect or want. Let's say I have a page with component A and component B which are two completely different and unrelated components. They each have a subscriptions function. Obviously I want component A to run its subscriptions function one time when it is created and I want component B to run its subscriptions function one time when it is created. That is not what happens. What actually happens is that A's subscriptions function is run when A is created and then run again when B is created. This is not solved by using .share()
because, when A's subscriptions function is run a second time, we are creating entirely new observables, not just resubscribing to the existing ones. This gets worse for every component you add to the page. If I add component C, even if it doesn't have a subscriptions function, it will cause the subscription functions of both A and B to be called one more time. Every new component that gets added to the page means an extra run of all existing subscriptions functions.
If this is really the expected behavior, what is the use case for it? In what situation would you want your component to rerun its subscriptions function every time a totally unrelated component is created?
What actually happens is that A's subscriptions function is run when A is created and then run again when B is created.
@okonomiyaki3000 Can you provide a clean online version to reproduce this?
@regou I made a fiddle with the intention of demonstrating this but then it all worked as one would expect. Which is weird because I've definitely had this problem in other cases. So I may be back if I can duplicate the issue again but, for now, maybe it's fine.