Skip to content

Commit 29da0fd

Browse files
authored
fix: debounce sendHeight (#426)
* fix: debounce sendHeight * feat: allow autoresize debounce time to be set through config * docs: add TS docs for
1 parent 801b7d8 commit 29da0fd

File tree

6 files changed

+50
-4
lines changed

6 files changed

+50
-4
lines changed

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"axios": "1.7.7",
5353
"dexie": "4.0.8",
5454
"http-status-codes": "2.3.0",
55+
"lodash.debounce": "4.0.8",
5556
"miragejs": "0.1.48",
5657
"msw": "2.4.11",
5758
"uuid": "10.0.0"
@@ -68,6 +69,7 @@
6869
"@testing-library/dom": "10.4.0",
6970
"@testing-library/react": "16",
7071
"@trivago/prettier-plugin-sort-imports": "4.3.0",
72+
"@types/lodash.debounce": "^4",
7173
"@types/node": "20.16.5",
7274
"@types/react": "18.3.8",
7375
"@types/react-dom": "18.3.0",

src/config/constants.ts

+2
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@ export const MOCK_TOKEN = 'mock-token';
1717
export const APP_ACTIONS_TOPIC = 'app-actions';
1818
export const APP_DATA_TOPIC = 'app-data';
1919
export const APP_SETTINGS_TOPIC = 'app-settings';
20+
21+
export const DEBOUNCE_TIME_AUTORESIZE = 150; // Unit: ms

src/hooks/postMessage.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { useEffect } from 'react';
77
import { LocalContext } from '@graasp/sdk';
88

99
import { useQuery, useQueryClient } from '@tanstack/react-query';
10+
import debounce from 'lodash.debounce';
1011

1112
import * as Api from '../api/index.js';
1213
import {
@@ -314,14 +315,14 @@ const configurePostMessageHooks = (queryConfig: QueryClientConfig) => {
314315

315316
useEffect(() => {
316317
if (!queryConfig.isStandalone) {
317-
const sendHeight = (height: number): void => {
318+
const sendHeight = debounce((height: number): void => {
318319
console.debug('[app-postMessage] Sending height', height);
319320
console.debug('communication channel is', communicationChannel);
320321
communicationChannel?.postMessage({
321322
type: POST_MESSAGE_KEYS.POST_AUTO_RESIZE,
322323
payload: height,
323324
});
324-
};
325+
}, queryConfig.debounceTimeAutoResize);
325326
if (!communicationChannel) {
326327
const error = new MissingMessageChannelPortError();
327328
console.error(error);
@@ -340,7 +341,10 @@ const configurePostMessageHooks = (queryConfig: QueryClientConfig) => {
340341
});
341342
resizeObserver.observe(document.body);
342343

343-
return () => resizeObserver.disconnect();
344+
return () => {
345+
sendHeight.cancel();
346+
resizeObserver.disconnect();
347+
};
344348
}
345349
return () => {};
346350
// eslint-disable-next-line react-hooks/exhaustive-deps

src/queryClient.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ import { ReactQueryDevtools } from '@tanstack/react-query-devtools';
1010
import { StatusCodes, getReasonPhrase } from 'http-status-codes';
1111

1212
import { API_ROUTES } from './api/routes.js';
13-
import { CACHE_TIME_MILLISECONDS, STALE_TIME_MILLISECONDS } from './config/constants.js';
13+
import {
14+
CACHE_TIME_MILLISECONDS,
15+
DEBOUNCE_TIME_AUTORESIZE,
16+
STALE_TIME_MILLISECONDS,
17+
} from './config/constants.js';
1418
import { QUERY_KEYS, buildPostMessageKeys } from './config/keys.js';
1519
import configureHooks from './hooks/index.js';
1620
import configureMutations from './mutations/index.js';
@@ -78,6 +82,7 @@ const configure = (
7882
enableWebsocket: Boolean(config.enableWebsocket),
7983
refetchOnWindowFocus: config.refetchOnWindowFocus ?? false,
8084
isStandalone: config.isStandalone ?? false,
85+
debounceTimeAutoResize: config.debounceTimeAutoResize ?? DEBOUNCE_TIME_AUTORESIZE,
8186
};
8287

8388
// create queryclient with given config

src/types.ts

+8
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ export type OptionalQueryClientConfig = {
3434
WS_HOST?: string;
3535
enableWebsocket: boolean;
3636
isStandalone: boolean;
37+
/**
38+
* Time in milliseconds for debouncing the autoresize hook defined in {@link postMessage}.
39+
* Sending the height from the app to the parent window will be delayed until after
40+
* `debounceTimeAutoResize` milliseconds have elapsed since the last time the height was
41+
* sent.
42+
* @see {@link https://lodash.com/docs/#debounce}
43+
*/
44+
debounceTimeAutoResize: number;
3745

3846
/**
3947
* @deprecated Use GRAASP_APP_KEY instead

yarn.lock

+25
Original file line numberDiff line numberDiff line change
@@ -977,6 +977,7 @@ __metadata:
977977
"@testing-library/dom": "npm:10.4.0"
978978
"@testing-library/react": "npm:16"
979979
"@trivago/prettier-plugin-sort-imports": "npm:4.3.0"
980+
"@types/lodash.debounce": "npm:^4"
980981
"@types/node": "npm:20.16.5"
981982
"@types/react": "npm:18.3.8"
982983
"@types/react-dom": "npm:18.3.0"
@@ -1001,6 +1002,7 @@ __metadata:
10011002
happy-dom: "npm:15.7.4"
10021003
http-status-codes: "npm:2.3.0"
10031004
husky: "npm:9.1.6"
1005+
lodash.debounce: "npm:4.0.8"
10041006
miragejs: "npm:0.1.48"
10051007
msw: "npm:2.4.11"
10061008
nock: "npm:13.5.5"
@@ -1961,6 +1963,22 @@ __metadata:
19611963
languageName: node
19621964
linkType: hard
19631965

1966+
"@types/lodash.debounce@npm:^4":
1967+
version: 4.0.9
1968+
resolution: "@types/lodash.debounce@npm:4.0.9"
1969+
dependencies:
1970+
"@types/lodash": "npm:*"
1971+
checksum: 10/8183a152e01928e3b97ca773f6ae6038b8695e76493ba8bf6b743ec143948a62294fbc9d49fa4a78b52265b3ba4892ef57534e0c13d04aa0f111671b5a944feb
1972+
languageName: node
1973+
linkType: hard
1974+
1975+
"@types/lodash@npm:*":
1976+
version: 4.17.12
1977+
resolution: "@types/lodash@npm:4.17.12"
1978+
checksum: 10/3d829f97c0ea713135e6eb48e983feffb315f4cf228d03654ca8a328e3fecd03005bf7dd30bf411f4306967c610d96738d441588283a41456815f2ce1fd7044c
1979+
languageName: node
1980+
linkType: hard
1981+
19641982
"@types/mute-stream@npm:^0.0.4":
19651983
version: 0.0.4
19661984
resolution: "@types/mute-stream@npm:0.0.4"
@@ -5396,6 +5414,13 @@ __metadata:
53965414
languageName: node
53975415
linkType: hard
53985416

5417+
"lodash.debounce@npm:4.0.8":
5418+
version: 4.0.8
5419+
resolution: "lodash.debounce@npm:4.0.8"
5420+
checksum: 10/cd0b2819786e6e80cb9f5cda26b1a8fc073daaf04e48d4cb462fa4663ec9adb3a5387aa22d7129e48eed1afa05b482e2a6b79bfc99b86886364449500cbb00fd
5421+
languageName: node
5422+
linkType: hard
5423+
53995424
"lodash.isplainobject@npm:^4.0.6":
54005425
version: 4.0.6
54015426
resolution: "lodash.isplainobject@npm:4.0.6"

0 commit comments

Comments
 (0)