Skip to content

Commit

Permalink
fix(List): fix initial loading also visible on reload
Browse files Browse the repository at this point in the history
  • Loading branch information
mfal committed Nov 15, 2024
1 parent 80449fa commit 52caca6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { times } from "remeda";
import { IncrementalLoaderState } from "@/components/List/model/loading/IncrementalLoaderState";
import { hash } from "object-code";

type AsyncResourceLoadingState = AsyncResource["state"]["value"];

const emptyData: never[] = [];

export class IncrementalLoader<T> {
Expand Down Expand Up @@ -97,14 +99,26 @@ export class IncrementalLoader<T> {
asyncResource: AsyncResource<DataLoaderResult<T>>,
batchIndex: number,
): void {
const skipReloadState = (
newState: AsyncResourceLoadingState,
): AsyncResourceLoadingState => {
if (this.loaderState.isBatchLoaded(batchIndex)) {
return "loaded";
}
return newState;
};

useEffect(() => {
this.loaderState.setBatchLoadingState(
batchIndex,
asyncResource.state.value,
skipReloadState(asyncResource.state.value),
);

return asyncResource.state.observe((newState) => {
this.loaderState.setBatchLoadingState(batchIndex, newState);
this.loaderState.setBatchLoadingState(
batchIndex,
skipReloadState(newState),
);
});
}, [asyncResource, batchIndex]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,8 @@ export class IncrementalLoaderState<T> {
public useIsLoading(): boolean {
return useSelector(() => this.isLoading);
}

public isBatchLoaded(batchIndex: number) {
return batchIndex in this.dataBatches;
}
}

0 comments on commit 52caca6

Please sign in to comment.