-
Notifications
You must be signed in to change notification settings - Fork 4.7k
chore: added caching layer for table widget data #39703
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
d739bd1
chore: added caching layer for table widget data
b571cd1
fix: linting errors
072a4d1
Merge branch 'release' of github.com:appsmithorg/appsmith into chore/…
e032afc
Merge branch 'release' of github.com:appsmithorg/appsmith into chore/…
0ac4132
fix: merge from release, conflict resolve
b33d28f
chore: implement caching data
fb74765
Merge branch 'release' of github.com:appsmithorg/appsmith into chore/…
c5b6570
chore: implement caching data
2c7a97e
chore: test fixes
33119f8
chore: test fixes
83cbb39
Merge branch 'release' of github.com:appsmithorg/appsmith into chore/…
3527b8c
chore: test fixes
a8f30a8
chore: fix conditional caching data
332272d
Merge branch 'release' of github.com:appsmithorg/appsmith into chore/…
f310a2b
chore: updated cached data on flag infinite scroll changes, on mounti…
e90d04a
Merge branch 'release' of github.com:appsmithorg/appsmith into chore/…
9a48249
feat: added comments, changed condition for updating end of data
efead6b
feat: updated the end of data condition
8b59f5b
feat: updated the end of data condition
47b2775
Merge branch 'release' of github.com:appsmithorg/appsmith into chore/…
ca5081a
feat: updated the end of data condition
5629567
fix: added commitBatchMetaUpdates to push in the changes for infinite…
3812d2c
fix: updated has more data condition
51faddb
Merge branch 'release' of github.com:appsmithorg/appsmith into chore/…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
184 changes: 184 additions & 0 deletions
184
app/client/src/widgets/TableWidgetV2/component/useCachingVirtualization.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,184 @@ | ||
| import { concat } from "lodash"; | ||
| import { useCallback, useEffect, useMemo, useRef, useState } from "react"; | ||
| import type { EditableCell } from "../constants"; | ||
|
|
||
| export interface UseCachingVirtualizationProps { | ||
| editableCell: EditableCell; | ||
| data: Array<Record<string, unknown>>; | ||
| isLoading: boolean; | ||
| isInfiniteScrollEnabled: boolean; | ||
| nextPageClick: () => void; | ||
| pageSize: number; | ||
| totalRecordsCount?: number; | ||
| isAddRowInProgress: boolean; | ||
| } | ||
|
|
||
| export interface UseCachingTableDataReturn { | ||
| isLoadingData: boolean; | ||
| cachedRows: Array<Record<string, unknown>>; | ||
| loadMoreNextPage: ( | ||
| startIndex?: number, | ||
| stopIndex?: number, | ||
| ) => Promise<void> | void; | ||
| isItemLoaded: (index: number) => boolean; | ||
| } | ||
|
|
||
| export const useCachingVirtualization = ({ | ||
| data, | ||
| editableCell, | ||
| isAddRowInProgress, | ||
| isInfiniteScrollEnabled, | ||
| isLoading, | ||
| nextPageClick, | ||
| pageSize, | ||
| totalRecordsCount, | ||
| }: UseCachingVirtualizationProps): UseCachingTableDataReturn => { | ||
| const [cachedRows, setCachedRows] = useState<Array<Record<string, unknown>>>( | ||
| [], | ||
| ); | ||
| const [isLoadingData, setIsLoadingData] = useState(false); | ||
| const lastLoadedPageRef = useRef<number>(0); | ||
| const hasMoreDataRef = useRef<boolean>(true); | ||
| const isAddRowInProgressRef = useRef<{ | ||
| isAddRowInProgress: boolean; | ||
| addedNewRow: boolean; | ||
| }>({ isAddRowInProgress: false, addedNewRow: false }); | ||
|
|
||
| const editableCellRef = useRef<{ | ||
| isEditing: boolean; | ||
| editingCell?: EditableCell; | ||
| }>({ isEditing: false, editingCell: undefined }); | ||
|
|
||
| const maxPages = useMemo(() => { | ||
| if (!totalRecordsCount) return Infinity; | ||
|
|
||
| return Math.ceil(totalRecordsCount / pageSize); | ||
| }, [totalRecordsCount, pageSize]); | ||
|
|
||
| const isItemLoaded = useCallback( | ||
| (index: number) => { | ||
| const pageIndex = Math.floor(index / pageSize); | ||
|
|
||
| return ( | ||
| pageIndex >= maxPages || | ||
| pageIndex < lastLoadedPageRef.current || | ||
| !hasMoreDataRef.current | ||
| ); | ||
| }, | ||
| [pageSize, maxPages], | ||
| ); | ||
|
|
||
| useEffect(() => { | ||
| if (isInfiniteScrollEnabled) { | ||
| setCachedRows([]); | ||
| } | ||
| }, [isInfiniteScrollEnabled]); | ||
|
|
||
| useEffect(() => { | ||
| isAddRowInProgressRef.current.isAddRowInProgress = isAddRowInProgress; | ||
| }, [isAddRowInProgress]); | ||
|
|
||
| useEffect(() => { | ||
| if (editableCell.index !== -1) { | ||
| editableCellRef.current.editingCell = editableCell; | ||
| editableCellRef.current.isEditing = true; | ||
| setCachedRows((e) => { | ||
| const newRows = [...e]; | ||
|
|
||
| newRows[editableCell.index][editableCell.column] = | ||
| editableCell.inputValue; | ||
|
|
||
| return newRows; | ||
| }); | ||
| } else { | ||
| editableCellRef.current.editingCell = undefined; | ||
| } | ||
| }, [editableCell]); | ||
|
|
||
| useEffect(() => { | ||
| if (isInfiniteScrollEnabled) { | ||
| if (isAddRowInProgressRef.current.isAddRowInProgress) { | ||
| if (isAddRowInProgressRef.current.addedNewRow) { | ||
| setCachedRows((e) => concat([data[0]], e.slice(1))); | ||
| } else { | ||
| setCachedRows((e) => concat([data[0]], e)); | ||
| isAddRowInProgressRef.current.addedNewRow = true; | ||
| } | ||
|
|
||
| // TODO: If new rows are added, we would have to update the cached rows | ||
| return; | ||
| } else if (data.length > 0) { | ||
| if (isAddRowInProgressRef.current.addedNewRow) { | ||
| isAddRowInProgressRef.current.addedNewRow = false; | ||
| setCachedRows((e) => e.slice(1)); | ||
| } else if (editableCellRef.current.isEditing) { | ||
| editableCellRef.current.isEditing = false; | ||
| } else { | ||
| const currentPageIndex = lastLoadedPageRef.current; | ||
|
|
||
| // Only increment if we got a full page or some data | ||
| if (data.length === pageSize) { | ||
| lastLoadedPageRef.current = currentPageIndex + 1; | ||
| } else if (data.length < pageSize && data.length > 0) { | ||
| // If we got less than a full page, assume this is the last page | ||
| hasMoreDataRef.current = false; | ||
| } | ||
|
|
||
| setCachedRows((e) => concat(e, data)); | ||
| } | ||
| } else if (data.length === 0 && lastLoadedPageRef.current > 0) { | ||
| if (isAddRowInProgressRef.current.addedNewRow) { | ||
| isAddRowInProgressRef.current.addedNewRow = false; | ||
| setCachedRows((e) => concat(e.slice(1), data)); | ||
| } else { | ||
| // If no rows are returned and we've loaded at least one page, assume end of data | ||
| hasMoreDataRef.current = false; | ||
| } | ||
| } | ||
| } else { | ||
| if (data.length > 0) { | ||
| setCachedRows(data); | ||
| } | ||
| } | ||
| }, [data, isInfiniteScrollEnabled]); | ||
|
|
||
| useEffect(() => { | ||
| setIsLoadingData(isLoading); | ||
| }, [isLoading]); | ||
|
|
||
| const loadMoreNextPage = useCallback( | ||
| async (startIndex?: number, stopIndex?: number) => { | ||
| if (isInfiniteScrollEnabled) { | ||
| if (!isLoadingData && hasMoreDataRef.current && !isAddRowInProgress) { | ||
| const targetPage = Math.floor(stopIndex! / pageSize); | ||
|
|
||
| if ( | ||
| targetPage >= lastLoadedPageRef.current && | ||
| targetPage < maxPages | ||
| ) { | ||
| nextPageClick(); | ||
| } | ||
| } | ||
|
|
||
| return Promise.resolve(); | ||
| } else { | ||
| nextPageClick(); | ||
| } | ||
| }, | ||
| [ | ||
| isLoadingData, | ||
| nextPageClick, | ||
| isInfiniteScrollEnabled, | ||
| pageSize, | ||
| maxPages, | ||
| isAddRowInProgress, | ||
| ], | ||
| ); | ||
|
|
||
| return { | ||
| cachedRows, | ||
| isItemLoaded, | ||
| isLoadingData, | ||
| loadMoreNextPage, | ||
| }; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unnecessary space here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
row[ORIGINAL_INDEX_KEY]->row?.[ORIGINAL_INDEX_KEY]due to this change, new space / line is added. cc @jacquesikot