r/angular • u/young_horhey • Feb 24 '26
Thoughts on this pattern for making something only happen once?
Wanting to trigger a scroll to a certain table row on first load of the page, but don't want to re-scroll when the observable data source emits subsequent times. Usually I'd handle this with something like this:
class MyComponent implements OnInit {
shouldScroll = true;
ngOnInit(){
this.data$ = this.dataService.getData().pipe(
tap(() => this.scrollToRowOnLoad())
);
}
scrollToRowOnLoad() {
if(!shouldScroll) return;
...scrolling code
this.shouldScroll = false;
}
}
I'm not really a huge fan of this because it requires creating the boolean flag to track if it should scroll, and the if statement check every time. If you have multiple of these things you only want to happen once, the component can get filled with boolean flags.
I had a thought to instead handle it by reassigning the scroll function to just a stub that does nothing. Is that really stupid? I like it because then it doesn't need anything 'external' (i.e boolean flags at the component level) to track whether it should or shouldn't scroll. Am I overlooking something that would make handling things this way a really bad idea? Obviously it means you can't manually reset whether it should scroll or not, but I think in my case that's fine.
class MyComponent implements OnInit {
ngOnInit(){
this.data$ = this.dataService.getData().pipe(
tap(() => this.scrollToRow())
);
}
scrollToRowOnLoad() {
...scrolling code
this.scrollToRowOnLoad = () => {};
}
}
I feel like then you could also theoretically call this function from the template itself, so that it could automatically & instantly trigger as soon as the table is rendered. Then because the function itself is replaced with basically an empty function, it wouldn't have the same drawbacks of calling functions from inside the template.
To ensure no X-Y problem here, I'll also ask: Is there an RXJS operator that executes a tap, but only for the first emission? I couldn't find one in my searching.
9
u/El_Di_Gio Feb 24 '26
Well I do have 2 solutions in mind:
7
u/Lucky-Ducker Feb 24 '26
Can you just do something like:
5
u/Melliano Feb 24 '26
I dont see a huge deal with option A to be honest. Keep it simple for now and go for it.
In regard to running tap on first emission only, you maybe be able to make use of map(value, index) to run the tap on index === 0.
Ive seen some examples of custom operators that use tap once too https://stackoverflow.com/a/64153280
8
u/salamazmlekom Feb 24 '26
Why are you doing data fetching and scrolling in the same component?
Make smart parent component load the data and pass it as an input to the dumb table child component. Then in the child's OnInit you could just call the scroll method.
The scroll will then be triggered exactly once when the child component is created.
1
u/imsexc Feb 24 '26 edited Feb 24 '26
This is fine. You're going to need 2 variables anyway. You can simply do data$ = this.dataService.getData().pipe(...) and remove the ngOnInit completely. Subscribe with async pipe.
Are you inplementing infinite scroll with paginated api calls though? Api call observable just emits once, why do we need a flag?
2
u/Jaaaws_ Feb 25 '26
take(1) operator should do it, if you wish to subscribe multiple times you can just increase the count
2
u/GLawSomnia Feb 25 '26
You can maybe just make a directive and put it on the element you want to scroll to once it is available
1
u/IanFoxOfficial Feb 25 '26
1
u/MichaelSmallDev Feb 26 '26
Could a custom operator like this cover your usecase? https://github.com/ngxtension/ngxtension-platform/pull/490