Skip to content

Commit 8a94e8d

Browse files
authored
feat: Make hook viewportSubscriptionOptions in viewport hooks partial (#2627)
I'm attempting to use one of these hooks for DH-21214. I have an array column that I need to pull all the data out of, so I need to customize the viewportSubscriptionOptions. Currently these hooks require you to pass in the whole object, but I think the rows and columns should be automatically replaced if you just want to customize the other options.
1 parent c46fbaf commit 8a94e8d

3 files changed

Lines changed: 91 additions & 8 deletions

File tree

packages/jsapi-components/src/useSetPaddedViewportCallback.test.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,35 @@ import { TableUtils } from '@deephaven/jsapi-utils';
55
import useSetPaddedViewportCallback from './useSetPaddedViewportCallback';
66

77
let table: dh.Table;
8+
let table2: dh.Table;
89
let viewportOptions: dh.ViewportSubscriptionOptions;
10+
let viewportOptionsMissingRows: Partial<dh.ViewportSubscriptionOptions>;
11+
let viewportOptionsMissingColumns: Partial<dh.ViewportSubscriptionOptions>;
12+
let viewportOptionsMissingBoth: Partial<dh.ViewportSubscriptionOptions>;
913
const viewportSize = 10;
1014
const viewportPadding = 4;
1115

1216
beforeEach(() => {
1317
jest.clearAllMocks();
1418
table = TestUtils.createMockProxy<dh.Table>({ size: 100 });
19+
table2 = TestUtils.createMockProxy<dh.Table>({ size: 101 });
1520
viewportOptions = {
1621
rows: {
1722
first: 0,
1823
last: 0,
1924
},
2025
columns: table.columns,
2126
};
27+
viewportOptionsMissingRows = {
28+
columns: table2.columns,
29+
};
30+
viewportOptionsMissingColumns = {
31+
rows: {
32+
first: 5,
33+
last: 15,
34+
},
35+
};
36+
viewportOptionsMissingBoth = {};
2237
});
2338

2439
it('should create a callback that sets a padded viewport', () => {
@@ -89,6 +104,68 @@ it('should use TableViewportSubscription if viewport options are provided', () =
89104
expect(table.setViewport).not.toHaveBeenCalled();
90105
});
91106

107+
it('should fill missing rows and columns when creating a subscription', () => {
108+
jest.spyOn(TableUtils, 'isTreeTable').mockReturnValue(false);
109+
110+
const mockSubscription = {
111+
update: jest.fn(),
112+
close: jest.fn(),
113+
};
114+
115+
(table.createViewportSubscription as jest.Mock).mockReturnValue(
116+
mockSubscription
117+
);
118+
119+
const { result, rerender } = renderHook(
120+
options =>
121+
useSetPaddedViewportCallback(
122+
table,
123+
viewportSize,
124+
viewportPadding,
125+
options
126+
),
127+
{ initialProps: viewportOptionsMissingRows }
128+
);
129+
130+
const firstRow = 30;
131+
result.current(firstRow);
132+
133+
const expectedRows = {
134+
first: firstRow - viewportPadding,
135+
last: firstRow + viewportSize + viewportPadding - 1,
136+
};
137+
138+
expect(table.createViewportSubscription).toHaveBeenCalledWith({
139+
columns: viewportOptionsMissingRows.columns,
140+
rows: expectedRows,
141+
});
142+
143+
jest.clearAllMocks();
144+
145+
rerender(viewportOptionsMissingColumns);
146+
result.current(firstRow + 5);
147+
148+
expect(table.createViewportSubscription).toHaveBeenCalledWith({
149+
rows: viewportOptionsMissingColumns.rows,
150+
columns: table.columns,
151+
});
152+
153+
jest.clearAllMocks();
154+
155+
rerender(viewportOptionsMissingBoth);
156+
result.current(firstRow + 10);
157+
158+
const expectedRows2 = {
159+
first: firstRow + 10 - viewportPadding,
160+
last: firstRow + 10 + viewportSize + viewportPadding - 1,
161+
};
162+
163+
expect(table.createViewportSubscription).toHaveBeenCalledWith({
164+
rows: expectedRows2,
165+
columns: table.columns,
166+
});
167+
});
168+
92169
it('should use setViewport if provided a tree table', () => {
93170
jest.spyOn(TableUtils, 'isTreeTable').mockReturnValue(true);
94171

packages/jsapi-components/src/useSetPaddedViewportCallback.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@ import {
1616
* @param viewportPadding The padding to add before and after the viewport.
1717
* @param viewportSubscriptionOptions The viewport subscription options to use. If provided and
1818
* the table is not a `TreeTable`, the data will be requested using a `TableViewportSubscription`.
19+
* Rows and columns are filled in when the subscription is created if they are missing.
1920
* @returns A callback function for setting the viewport.
2021
*/
2122
export function useSetPaddedViewportCallback(
2223
table: dh.Table | dh.TreeTable | null,
2324
viewportSize: number,
2425
viewportPadding: number,
25-
viewportSubscriptionOptions: dh.ViewportSubscriptionOptions | null = null
26+
viewportSubscriptionOptions: Partial<dh.ViewportSubscriptionOptions> | null = null
2627
): (firstRow: number) => void {
2728
const subscriptionRef = useRef<dh.TableViewportSubscription | null>(null);
2829
const prevTableRef = useRef<dh.Table | dh.TreeTable | null>(null);
29-
const prevViewportOptionsRef = useRef<dh.ViewportSubscriptionOptions | null>(
30-
null
31-
);
30+
const prevViewportOptionsRef =
31+
useRef<Partial<dh.ViewportSubscriptionOptions> | null>(null);
3232

3333
const cleanupSubscription = () => {
3434
if (subscriptionRef.current) {
@@ -66,9 +66,14 @@ export function useSetPaddedViewportCallback(
6666
viewportSubscriptionOptions != null &&
6767
!TableUtils.isTreeTable(table)
6868
) {
69-
subscriptionRef.current = table.createViewportSubscription(
70-
viewportSubscriptionOptions
71-
);
69+
const subscriptionOptions: dh.ViewportSubscriptionOptions = {
70+
...viewportSubscriptionOptions,
71+
rows: viewportSubscriptionOptions.rows ?? { first, last },
72+
columns: viewportSubscriptionOptions.columns ?? table.columns,
73+
};
74+
75+
subscriptionRef.current =
76+
table.createViewportSubscription(subscriptionOptions);
7277
}
7378

7479
if (subscriptionRef.current == null) {

packages/jsapi-components/src/useViewportData.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export interface UseViewportDataProps<
3333
viewportPadding?: number;
3434
viewportSize?: number;
3535
deserializeRow?: RowDeserializer<TItem>;
36-
viewportSubscriptionOptions?: dh.ViewportSubscriptionOptions | null;
36+
viewportSubscriptionOptions?: Partial<dh.ViewportSubscriptionOptions> | null;
3737
}
3838

3939
export interface UseViewportDataResult<
@@ -71,6 +71,7 @@ export interface UseViewportDataResult<
7171
* @param reuseItemsOnTableResize If true, existing items will be re-used when
7272
* @param viewportSubscriptionOptions The viewport subscription options to use. If provided and
7373
* the table is not a `TreeTable`, the data will be requested using a `TableViewportSubscription`.
74+
* Rows and columns are filled in when the subscription is created if they are missing.
7475
* @returns An object for managing Table viewport state.
7576
*/
7677
export function useViewportData<TItem, TTable extends dh.Table | dh.TreeTable>({

0 commit comments

Comments
 (0)