Skip to content

Commit 05a9741

Browse files
committed
save work
1 parent e18dc5c commit 05a9741

File tree

3 files changed

+44
-27
lines changed

3 files changed

+44
-27
lines changed

chartlets.js/package-lock.json

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

chartlets.js/packages/lib/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"preview": "vite preview"
5656
},
5757
"dependencies": {
58+
"micro-memoize": "^4.1.3",
5859
"microdiff": "^1.4",
5960
"zustand": "^5.0"
6061
},

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

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import memoize from "micro-memoize";
2+
13
import type {
4+
Callback,
25
CallbackRef,
36
CallbackRequest,
47
ContribRef,
@@ -11,6 +14,7 @@ import { invokeCallbacks } from "@/actions/helpers/invokeCallbacks";
1114
import type { ContributionState } from "@/types/state/contribution";
1215
import type { HostStore } from "@/types/state/host";
1316
import { store } from "@/store";
17+
import type { ContribPoint } from "@/types/model/extension";
1418

1519
/**
1620
* A reference to a property of an input of a callback of a contribution.
@@ -33,7 +37,7 @@ export function handleHostStoreChange() {
3337
// Exit if there are no extensions (yet)
3438
return;
3539
}
36-
const propertyRefs = getHostStorePropertyRefs();
40+
const propertyRefs = getHostStorePropertyRefs(contributionsRecord);
3741
if (!propertyRefs || propertyRefs.length === 0) {
3842
// Exit if there are is nothing to be changed
3943
return;
@@ -66,37 +70,42 @@ function getCallbackRequests(
6670
});
6771
}
6872

69-
// TODO: use a memoized selector to get hostStorePropertyRefs
70-
// Note that this will only be effective and once we split the
71-
// static contribution infos and dynamic contribution states.
72-
// The hostStorePropertyRefs only depend on the static
73-
// contribution infos.
74-
7573
/**
7674
* Get the static list of host state property references for all contributions.
7775
*/
78-
function getHostStorePropertyRefs(): PropertyRef[] {
79-
const { contributionsRecord } = store.getState();
76+
const getHostStorePropertyRefs = memoize(_getHostStorePropertyRefs);
77+
78+
function getCallbackfn(
79+
contribPoint: string,
80+
contribution: ContributionState,
81+
contribIndex: number,
82+
) {
83+
const propertyRefs: PropertyRef[] = [];
84+
const callbacks: Callback[] = contribution.callbacks || [];
85+
callbacks.forEach((callback, callbackIndex) => {
86+
const inputs = callback.inputs || [];
87+
inputs.forEach((input, inputIndex) => {
88+
if (!input.noTrigger && input.id === "@app" && input.property) {
89+
propertyRefs.push({
90+
contribPoint,
91+
contribIndex,
92+
callbackIndex,
93+
inputIndex,
94+
property: formatObjPath(input.property),
95+
});
96+
}
97+
});
98+
});
99+
return propertyRefs;
100+
}
101+
102+
function _getHostStorePropertyRefs(
103+
contributionsRecord: Record<ContribPoint, ContributionState[]>,
104+
): PropertyRef[] {
80105
const propertyRefs: PropertyRef[] = [];
81106
Object.getOwnPropertyNames(contributionsRecord).forEach((contribPoint) => {
82107
const contributions = contributionsRecord[contribPoint];
83-
contributions.forEach((contribution, contribIndex) => {
84-
(contribution.callbacks || []).forEach(
85-
(callback, callbackIndex) =>
86-
(callback.inputs || []).forEach((input, inputIndex) => {
87-
if (!input.noTrigger && input.id === "@app" && input.property) {
88-
propertyRefs.push({
89-
contribPoint,
90-
contribIndex,
91-
callbackIndex,
92-
inputIndex,
93-
property: formatObjPath(input.property),
94-
});
95-
}
96-
}),
97-
[] as Input[],
98-
);
99-
});
108+
contributions.forEach(getCallbackfn(propertyRefs, contribPoint));
100109
});
101110
return propertyRefs;
102111
}

0 commit comments

Comments
 (0)