Skip to content

Commit 54d474c

Browse files
committed
Add memoization for invoking callbacks
1 parent 64990f7 commit 54d474c

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

chartlets.js/packages/lib/src/actions/handleHostStoreChange.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ export interface PropertyRef extends ContribRef, CallbackRef, InputRef {
2020
property: string;
2121
}
2222

23+
const processedinputValuess = new Map<string, boolean>();
24+
2325
export function handleHostStoreChange() {
2426
const { extensions, configuration, contributionsRecord } = store.getState();
2527
const { hostStore } = configuration;
@@ -43,16 +45,21 @@ export function handleHostStoreChange() {
4345
contributionsRecord,
4446
hostStore,
4547
);
46-
if (callbackRequests && callbackRequests.length > 0) {
47-
invokeCallbacks(callbackRequests);
48+
const filtered_callbackRequests = callbackRequests.filter(
49+
(req): req is CallbackRequest => req !== undefined,
50+
);
51+
if (filtered_callbackRequests && filtered_callbackRequests.length > 0) {
52+
invokeCallbacks(filtered_callbackRequests);
4853
}
4954
}
5055

5156
function getCallbackRequests(
5257
propertyRefs: PropertyRef[],
5358
contributionsRecord: Record<string, ContributionState[]>,
5459
hostStore: HostStore,
55-
): CallbackRequest[] {
60+
): (CallbackRequest | undefined)[] {
61+
const { configuration } = store.getState();
62+
const loggingEnabled = configuration.logging?.enabled;
5663
return propertyRefs.map((propertyRef) => {
5764
const contributions = contributionsRecord[propertyRef.contribPoint];
5865
const contribution = contributions[propertyRef.contribIndex];
@@ -62,6 +69,20 @@ function getCallbackRequests(
6269
contribution,
6370
hostStore,
6471
);
72+
const serializedInputValues = JSON.stringify(inputValues);
73+
if (processedinputValuess.has(serializedInputValues)) {
74+
if (loggingEnabled) {
75+
console.groupCollapsed(
76+
"Skipping this callback as no state has changed!",
77+
);
78+
console.log("propertyRef", propertyRef);
79+
console.log("inputValues", inputValues);
80+
console.groupEnd();
81+
}
82+
return;
83+
} else {
84+
processedinputValuess.set(serializedInputValues, true);
85+
}
6586
return { ...propertyRef, inputValues };
6687
});
6788
}

0 commit comments

Comments
 (0)