Skip to content

Commit a9ff949

Browse files
committed
Memoize computation of property references
1 parent 05a9741 commit a9ff949

File tree

2 files changed

+76
-23
lines changed

2 files changed

+76
-23
lines changed

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

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import type {
77
ContribRef,
88
InputRef,
99
} from "@/types/model/callback";
10-
import type { Input } from "@/types/model/channel";
1110
import { getInputValues } from "@/actions/helpers/getInputValues";
1211
import { formatObjPath } from "@/utils/objPath";
1312
import { invokeCallbacks } from "@/actions/helpers/invokeCallbacks";
@@ -37,7 +36,7 @@ export function handleHostStoreChange() {
3736
// Exit if there are no extensions (yet)
3837
return;
3938
}
40-
const propertyRefs = getHostStorePropertyRefs(contributionsRecord);
39+
const propertyRefs = getPropertyRefsForContribPoints(contributionsRecord);
4140
if (!propertyRefs || propertyRefs.length === 0) {
4241
// Exit if there are is nothing to be changed
4342
return;
@@ -71,18 +70,64 @@ function getCallbackRequests(
7170
}
7271

7372
/**
74-
* Get the static list of host state property references for all contributions.
73+
* Get the static list of host state property references
74+
* for given contribution points.
7575
*/
76-
const getHostStorePropertyRefs = memoize(_getHostStorePropertyRefs);
76+
const getPropertyRefsForContribPoints = memoize(
77+
_getPropertyRefsForContribPoints,
78+
);
7779

78-
function getCallbackfn(
80+
function _getPropertyRefsForContribPoints(
81+
contributionsRecord: Record<ContribPoint, ContributionState[]>,
82+
): PropertyRef[] {
83+
const propertyRefs: PropertyRef[] = [];
84+
Object.getOwnPropertyNames(contributionsRecord).forEach((contribPoint) => {
85+
const contributions = contributionsRecord[contribPoint];
86+
propertyRefs.push(
87+
...getPropertyRefsForContributions(contribPoint, contributions),
88+
);
89+
});
90+
return propertyRefs;
91+
}
92+
93+
/**
94+
* Get the static list of host state property references
95+
* for given contributions.
96+
*/
97+
const getPropertyRefsForContributions = memoize(
98+
_getPropertyRefsForContributions,
99+
);
100+
101+
function _getPropertyRefsForContributions(
102+
contribPoint: string,
103+
contributions: ContributionState[],
104+
): PropertyRef[] {
105+
const propertyRefs: PropertyRef[] = [];
106+
contributions.forEach((contribution, contribIndex) => {
107+
propertyRefs.push(
108+
...getPropertyRefsForCallbacks(
109+
contribPoint,
110+
contribIndex,
111+
contribution.callbacks,
112+
),
113+
);
114+
});
115+
return propertyRefs;
116+
}
117+
118+
/**
119+
* Get the static list of host state property references
120+
* for given callbacks.
121+
*/
122+
const getPropertyRefsForCallbacks = memoize(_getPropertyRefsForCallbacks);
123+
124+
function _getPropertyRefsForCallbacks(
79125
contribPoint: string,
80-
contribution: ContributionState,
81126
contribIndex: number,
127+
callbacks: Callback[] | undefined,
82128
) {
83129
const propertyRefs: PropertyRef[] = [];
84-
const callbacks: Callback[] = contribution.callbacks || [];
85-
callbacks.forEach((callback, callbackIndex) => {
130+
(callbacks || []).forEach((callback, callbackIndex) => {
86131
const inputs = callback.inputs || [];
87132
inputs.forEach((input, inputIndex) => {
88133
if (!input.noTrigger && input.id === "@app" && input.property) {
@@ -99,17 +144,6 @@ function getCallbackfn(
99144
return propertyRefs;
100145
}
101146

102-
function _getHostStorePropertyRefs(
103-
contributionsRecord: Record<ContribPoint, ContributionState[]>,
104-
): PropertyRef[] {
105-
const propertyRefs: PropertyRef[] = [];
106-
Object.getOwnPropertyNames(contributionsRecord).forEach((contribPoint) => {
107-
const contributions = contributionsRecord[contribPoint];
108-
contributions.forEach(getCallbackfn(propertyRefs, contribPoint));
109-
});
110-
return propertyRefs;
111-
}
112-
113147
function synchronizeThemeMode(hostStore: HostStore) {
114148
const newThemeMode = hostStore.get("themeMode");
115149
const oldThemeMode = store.getState().themeMode;

chartlets.js/packages/lib/src/types/state/store.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,33 @@ import type { FrameworkOptions } from "./options";
99

1010
export type ThemeMode = "dark" | "light" | "system";
1111

12+
// TODO: Split contributionsRecord into two fields comprising static
13+
// contribution data and dynamic contribution states.
14+
// This will allow memoizing the computation of property references
15+
// (PropertyRef[]) on the level of the StoreState from static data only.
16+
// The property references would then be just computed once.
17+
// See function getPropertyRefsForContribPoints()
18+
// in actions/handleHostStoreChange.ts
19+
20+
/**
21+
* The state of the Chartlets main store.
22+
*/
1223
export interface StoreState {
13-
/** Framework configuration */
24+
/**
25+
* Framework configuration.
26+
*/
1427
configuration: FrameworkOptions;
15-
/** All extensions */
28+
/**
29+
* All extensions.
30+
*/
1631
extensions: Extension[];
17-
/** API call result from `GET /contributions`. */
32+
/**
33+
* API call result from `GET /contributions`.
34+
*/
1835
contributionsResult: ApiResult<Contributions>;
19-
/** A record that maps contribPoint --> ContributionState[].*/
36+
/**
37+
* A record that maps contribPoint --> ContributionState[].
38+
*/
2039
contributionsRecord: Record<ContribPoint, ContributionState[]>;
2140
/**
2241
* The app's current theme mode.

0 commit comments

Comments
 (0)