adapter fix does not update max/minUserIndex
Closed this issue · 3 comments
I am using adapter.fix to try to update max index after adapter.update. But the max/minUserIndex does not get updated here:
vscroll/src/processes/adapter/fix.ts
Line 72 in 49e5e30
This causes an issue when we later on try to do adapter.reload with a startIndex equal to or greater than what we set the maxIndex to with adapter.fix, since it calls reset internally, which checks the maxUserIndex.
vscroll/src/processes/adapter/reload.ts
Line 12 in 49e5e30
@tmartinsson The Adapter.update
method provides all necessary internal changes, including min/max and absMin/absMax indexes adjustments (but not the user ones, which is intended, because they come from the user settings and retain the original values). So, I would not expect the App to update any internal Scroller params after the update
is done, expect the virtual updates case. Since the Adapter.update
method works only for the items that are present in the Scroller's Buffer (non-virtual items), updating virtual items (that are not in the Buffer) will have no effect. Internal indexes will not be updated as well.
Another point is synchronization of the Scroller's update with the Datasource at the App level, which should be done super carefully. So, the only situation when you may need to update internal indexes is when you update the Datasource and don't update the Scroller (because the Adapter.update
doesn't deal with virtual items).
The easiest approach in such situation would be not to use the Adapter.update method and provide virtual updates using the Adapter.insert/remove methods with index signatures instead. The Adapter.insert/remove methods maintain min/max indexes for both virtual and in-buffer changes. But they are less performant, because they need to be run one by one. From the other hand you may use the approach consisting of Adapter.update + conditional Adapter.fix({ maxIndex }) for virtual case. Conditional means you need to make sure that you deal with virtual update before using fix
.
I implemented all the requirements described above in the following demo: https://stackblitz.com/edit/ngx-ui-scroll-udapter-sync. It has the Update button which
- provides the remove/insert updates in the Datasource at the App level (via splice),
- provides both removing and inserting at the Scroller level (via
Adapter.update
), - checks if the
update
is virtual & shifts the internal max-index if necessary (viaAdapter.fix
).
I believe, you don't need to change user min/max indexes. But if you could update the demo and provide a scenario when you think it might be necessary, I would keep thinking.
Thanks for the quick response!
I see, then I think I understand my problem. We are using the user setting and trying to update like you mentioned. If I remove the maxIndex from the setting, it looks like it is working.
If you want to reproduce the problem we had I forked the stackblitz: https://ngx-ui-scroll-udapter-sync-bhhhmb.stackblitz.io
If you do update then reload, you can see that the last few items get cut off. But if we remove the setting, like the original stackblitz, it works fine.
So the solution then is to not use the user setting in this scenario.
@tmartinsson Ah, I got it. Your understanding and the fix are correct.