Skip to content

Commit 775b3c1

Browse files
committed
Memoized getHostStorePropertyRefs()
1 parent ba1eead commit 775b3c1

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

dashi/package-lock.json

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dashi/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"@emotion/styled": "^11.13.0",
5050
"@fontsource/roboto": "^5.1.0",
5151
"@mui/material": "^6.1.5",
52+
"memoize-one": "^6.0.0",
5253
"microdiff": "^1.4.0",
5354
"react": "^18.3.1",
5455
"react-dom": "^18.3.1",

dashi/src/lib/actions/handleHostStoreChange.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import memoizeOne from "memoize-one";
2+
13
import { store } from "@/lib/store";
24
import type {
35
CallbackRef,
@@ -7,7 +9,7 @@ import type {
79
InputRef,
810
} from "@/lib/types/model/callback";
911
import { getInputValues } from "@/lib/actions/helpers/getInputValues";
10-
import { getValue } from "@/lib/utils/getValue";
12+
import { getValue, type PropertyPath } from "@/lib/utils/getValue";
1113
import { invokeCallbacks } from "@/lib/actions/helpers/invokeCallbacks";
1214
import type { ContributionState } from "@/lib";
1315

@@ -16,14 +18,13 @@ import type { ContributionState } from "@/lib";
1618
*/
1719
export interface PropertyRef extends ContribRef, CallbackRef, InputRef {
1820
/** The property name as path. */
19-
propertyPath: string[];
21+
propertyPath: PropertyPath;
2022
}
2123

2224
export function handleHostStoreChange<S extends object = object>(
2325
currState: S,
2426
prevState: S,
2527
) {
26-
console.log("Aaaaaaaarghhh!");
2728
const { contributionsRecord } = store.getState();
2829
const callbackRequests = getCallbackRequests(
2930
contributionsRecord,
@@ -56,9 +57,9 @@ function getCallbackRequests<S extends object = object>(
5657
return callbackRequest;
5758
}
5859

59-
// TODO: memoize this function,
60-
// its return value should stay constant.
61-
function getHostStorePropertyRefs(): PropertyRef[] {
60+
const getHostStorePropertyRefs = memoizeOne(_getHostStorePropertyRefs);
61+
62+
function _getHostStorePropertyRefs(): PropertyRef[] {
6263
const { contributionsRecord } = store.getState();
6364
const propertyRefs: PropertyRef[] = [];
6465
Object.getOwnPropertyNames(contributionsRecord).forEach((contribPoint) => {
@@ -85,7 +86,7 @@ function getHostStorePropertyRefs(): PropertyRef[] {
8586
}
8687

8788
function hasPropertyChanged<S extends object = object>(
88-
propertyPath: string[],
89+
propertyPath: PropertyPath,
8990
currState: S,
9091
prevState: S,
9192
): boolean {

0 commit comments

Comments
 (0)