Skip to content

Commit 38fefa4

Browse files
committed
using ObjPathLike instead of string | ObjPath
1 parent d7fe9c2 commit 38fefa4

File tree

10 files changed

+69
-61
lines changed

10 files changed

+69
-61
lines changed

dashi/src/lib/actions/handleComponentChange.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { type ComponentChangeEvent } from "@/lib/types/model/event";
55
import { getInputValues } from "@/lib/actions/helpers/getInputValues";
66
import { applyStateChangeRequests } from "@/lib/actions/helpers/applyStateChangeRequests";
77
import { invokeCallbacks } from "@/lib/actions/helpers/invokeCallbacks";
8+
import { equalObjPaths } from "@/lib/utils/objPath";
89

910
export function handleComponentChange(
1011
contribPoint: ContribPoint,
@@ -18,9 +19,9 @@ export function handleComponentChange(
1819
stateChanges: [
1920
{
2021
link: "component",
21-
id: changeEvent.componentId,
22-
property: changeEvent.propertyName,
23-
value: changeEvent.propertyValue,
22+
id: changeEvent.id,
23+
property: changeEvent.property,
24+
value: changeEvent.value,
2425
},
2526
],
2627
},
@@ -59,8 +60,8 @@ function getCallbackRequests(
5960
(input) =>
6061
!input.noTrigger &&
6162
(!input.link || input.link === "component") &&
62-
input.id === changeEvent.componentId &&
63-
input.property === changeEvent.propertyName,
63+
input.id === changeEvent.id &&
64+
equalObjPaths(input.property, changeEvent.property),
6465
);
6566
if (inputIndex >= 0) {
6667
callbackRequests.push({

dashi/src/lib/actions/helpers/getInputValues.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export function getInputValueFromComponent(
4747
input: Input,
4848
componentState: ComponentState,
4949
): unknown {
50-
if (componentState.id === input.id && input.property) {
50+
if (componentState.id === input.id) {
5151
return getValue(componentState, input.property);
5252
} else if (isContainerState(componentState)) {
5353
for (let i = 0; i < componentState.components.length; i++) {

dashi/src/lib/actions/helpers/invokeCallbacks.ts

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,36 @@ import { fetchStateChangeRequests } from "@/lib/api/fetchStateChangeRequests";
55
import { applyStateChangeRequests } from "@/lib/actions/helpers/applyStateChangeRequests";
66

77
export function invokeCallbacks(callbackRequests: CallbackRequest[]) {
8+
if (!callbackRequests.length) {
9+
return;
10+
}
811
const { configuration } = store.getState();
912
const invocationId = getInvocationId();
1013
if (import.meta.env.DEV) {
1114
console.debug(`invokeCallbacks (${invocationId})-->`, callbackRequests);
1215
}
13-
if (callbackRequests.length) {
14-
fetchApiResult(
15-
fetchStateChangeRequests,
16-
callbackRequests,
17-
configuration.api,
18-
).then((changeRequestsResult) => {
19-
if (changeRequestsResult.data) {
20-
if (import.meta.env.DEV) {
21-
console.debug(
22-
`invokeCallbacks <--(${invocationId})`,
23-
changeRequestsResult.data,
24-
);
25-
}
26-
applyStateChangeRequests(changeRequestsResult.data);
27-
} else {
28-
console.error(
29-
"callback failed:",
30-
changeRequestsResult.error,
31-
"for call requests:",
32-
callbackRequests,
16+
fetchApiResult(
17+
fetchStateChangeRequests,
18+
callbackRequests,
19+
configuration.api,
20+
).then((changeRequestsResult) => {
21+
if (changeRequestsResult.data) {
22+
if (import.meta.env.DEV) {
23+
console.debug(
24+
`invokeCallbacks <--(${invocationId})`,
25+
changeRequestsResult.data,
3326
);
3427
}
35-
});
36-
}
28+
applyStateChangeRequests(changeRequestsResult.data);
29+
} else {
30+
console.error(
31+
"callback failed:",
32+
changeRequestsResult.error,
33+
"for call requests:",
34+
callbackRequests,
35+
);
36+
}
37+
});
3738
}
3839

3940
let invocationCounter = 0;

dashi/src/lib/api/fetchContributions.ts

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,29 @@ export async function fetchContributions(
1414
return callApi<Contributions>(
1515
makeUrl("contributions", options),
1616
undefined,
17-
(contributions: Contributions) => ({
18-
...contributions,
19-
contributions: mapObject(
20-
contributions.contributions,
21-
(contributions: Contribution[]) =>
22-
contributions.map(
23-
(contribution): Contribution => ({
24-
...contribution,
25-
layout: contribution.layout
26-
? normalizeCallback(contribution.layout)
27-
: undefined,
28-
callbacks: normalizeCallbacks(contribution.callbacks),
29-
}),
30-
),
31-
),
32-
}),
17+
normalizeContributions,
3318
);
3419
}
3520

21+
function normalizeContributions(contributions: Contributions) {
22+
return {
23+
...contributions,
24+
contributions: mapObject(
25+
contributions.contributions,
26+
(contributions: Contribution[]) =>
27+
contributions.map(
28+
(contribution): Contribution => ({
29+
...contribution,
30+
layout: contribution.layout
31+
? normalizeCallback(contribution.layout)
32+
: undefined,
33+
callbacks: normalizeCallbacks(contribution.callbacks),
34+
}),
35+
),
36+
),
37+
};
38+
}
39+
3640
function normalizeCallbacks(callbacks: Callback[] | undefined): Callback[] {
3741
return callbacks ? callbacks.map(normalizeCallback) : [];
3842
}

dashi/src/lib/components/Button.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ export function Button({
2020
if (id) {
2121
onChange({
2222
componentType: "Button",
23-
componentId: id,
23+
id: id,
2424
// Compat with plotly/dash
25-
propertyName: "n_clicks",
25+
property: "n_clicks",
2626
// TODO: get number of mouse clicks
27-
propertyValue: event.buttons,
27+
value: event.buttons,
2828
});
2929
}
3030
};

dashi/src/lib/components/Checkbox.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ export function Checkbox({
2323
if (id) {
2424
return onChange({
2525
componentType: "Checkbox",
26-
componentId: id,
27-
propertyName: "value",
28-
propertyValue: event.currentTarget.checked,
26+
id: id,
27+
property: "value",
28+
value: event.currentTarget.checked,
2929
});
3030
}
3131
};

dashi/src/lib/components/Dropdown.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ export function Dropdown({
3131

3232
return onChange({
3333
componentType: "Dropdown",
34-
componentId: id,
35-
propertyName: "value",
36-
propertyValue: newValue,
34+
id: id,
35+
property: "value",
36+
value: newValue,
3737
});
3838
};
3939
return (

dashi/src/lib/components/Plot.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ export function Plot({ id, style, chart, onChange }: PlotProps) {
1616
if (id) {
1717
return onChange({
1818
componentType: "Plot",
19-
componentId: id,
20-
propertyName: "points",
21-
propertyValue: value,
19+
id: id,
20+
property: "points",
21+
value: value,
2222
});
2323
}
2424
};

dashi/src/lib/types/model/channel.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ObjPath } from "@/lib/utils/objPath";
1+
import type { ObjPathLike } from "@/lib/utils/objPath";
22

33
export type Link = "component" | "container" | "app";
44

@@ -20,7 +20,7 @@ export interface Channel {
2020
/**
2121
* The property of an object or array index.
2222
*/
23-
property: string | ObjPath;
23+
property: ObjPathLike;
2424
}
2525

2626
export interface Input extends Channel {

dashi/src/lib/types/model/event.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { type ComponentType } from "@/lib/types/state/component";
2+
import type { ObjPathLike } from "@/lib/utils/objPath";
23

34
export interface ComponentChangeEvent {
45
componentType: ComponentType;
5-
componentId: string;
6-
propertyName: string;
7-
propertyValue: unknown;
6+
// See commonality with StateChange
7+
id: string;
8+
property: ObjPathLike;
9+
value: unknown;
810
}
911

1012
export type ComponentChangeHandler = (event: ComponentChangeEvent) => void;

0 commit comments

Comments
 (0)