Skip to content

Commit cfeddbd

Browse files
committed
callback mechanism works!
1 parent 3bbe5dd commit cfeddbd

File tree

4 files changed

+100
-31
lines changed

4 files changed

+100
-31
lines changed

dashi/src/actions/handleComponentPropertyChange.ts

Lines changed: 83 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
1-
import appStore, { ContribPoint } from "../store/appStore";
2-
import { PropertyChangeEvent } from "../model/component";
3-
import { CallbackCallRequest, CallbackCallResult } from "../model/callback";
1+
import appStore, { ContribPoint, ContributionState } from "../store/appStore";
2+
import {
3+
ComponentModel,
4+
ContainerModel,
5+
isContainerModel,
6+
PropertyChangeEvent,
7+
} from "../model/component";
8+
import {
9+
CallbackCallRequest,
10+
CallbackCallResult,
11+
ComputedOutput,
12+
} from "../model/callback";
413
import fetchApiResult from "../utils/fetchApiResult";
514
import { fetchCallbackCallResults } from "../api";
15+
import { updateArray } from "../utils/updateArray";
616

717
export default function handleComponentPropertyChange(
818
contribPoint: ContribPoint,
@@ -68,24 +78,75 @@ export default function handleComponentPropertyChange(
6878
}
6979

7080
function applyCallbackCallResults(callResults: CallbackCallResult[]) {
71-
// TODO: process call result --> modify any targets in appState.
72-
console.warn(
73-
"processing of callback results not implemented yet:",
74-
callResults,
75-
);
76-
//const contributionPoints = appStore.getState().contributionPointsResult.data!;
77-
callResults.forEach(
78-
({ contribPoint: _1, contribIndex: _2, computedOutputs }) => {
79-
//const contributions = contributionPoints[contribPoint];
80-
//const contributionModel = contributions[contribIndex];
81-
//const contributionState = { ...appStore.getState().panelStates[c] };
82-
computedOutputs.forEach((output) => {
83-
if (!output.kind || output.kind === "Component") {
84-
//const componentId = output.id;
85-
//const propertyName = output.property;
86-
//const propertyValue = output.value;
87-
}
81+
console.log("processing call results", callResults);
82+
callResults.forEach(({ contribPoint, contribIndex, computedOutputs }) => {
83+
console.log(
84+
"processing output of",
85+
contribPoint,
86+
contribIndex,
87+
computedOutputs,
88+
);
89+
const contributionStatesRecord =
90+
appStore.getState().contributionStatesRecord;
91+
const contributionStates = contributionStatesRecord[contribPoint];
92+
const contributionState = contributionStates[contribIndex];
93+
const componentModelOld = contributionState.componentModelResult.data;
94+
let componentModel = componentModelOld;
95+
computedOutputs.forEach((output) => {
96+
if (componentModel && (!output.kind || output.kind === "Component")) {
97+
componentModel = updateComponentState(componentModel, output);
98+
} else {
99+
// TODO: process other output kinds which may not require componentModel.
100+
console.warn(
101+
"processing of this kind of output not supported yet:",
102+
output,
103+
);
104+
}
105+
});
106+
if (componentModel && componentModel !== componentModelOld) {
107+
appStore.setState({
108+
contributionStatesRecord: {
109+
...contributionStatesRecord,
110+
[contribPoint]: updateArray<ContributionState>(
111+
contributionStates,
112+
contribIndex,
113+
{
114+
...contributionState,
115+
componentModelResult: {
116+
...contributionState.componentModelResult,
117+
data: componentModel,
118+
},
119+
},
120+
),
121+
},
88122
});
89-
},
90-
);
123+
}
124+
});
125+
}
126+
127+
function updateComponentState(
128+
componentModel: ComponentModel,
129+
output: ComputedOutput,
130+
): ComponentModel {
131+
if (componentModel.id === output.id) {
132+
return { ...componentModel, [output.property]: output.value };
133+
} else if (isContainerModel(componentModel)) {
134+
const containerModelOld: ContainerModel = componentModel;
135+
let containerModelNew: ContainerModel = containerModelOld;
136+
for (let i = 0; i < containerModelOld.components.length; i++) {
137+
const itemOld = containerModelOld.components[i];
138+
const itemNew = updateComponentState(itemOld, output);
139+
if (itemNew !== itemOld) {
140+
if (containerModelNew === containerModelOld) {
141+
containerModelNew = {
142+
...containerModelOld,
143+
components: [...containerModelOld.components],
144+
};
145+
}
146+
containerModelNew.components[i] = itemNew;
147+
}
148+
}
149+
return containerModelNew;
150+
}
151+
return componentModel;
91152
}

dashi/src/actions/updateContributionState.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import appStore, { ContribPoint, ContributionState } from "../store/appStore";
2+
import { updateArray } from "../utils/updateArray";
23

34
export function updateContributionState(
45
contribPoint: ContribPoint,
@@ -18,11 +19,3 @@ export function updateContributionState(
1819
},
1920
});
2021
}
21-
22-
function updateArray<T>(array: T[], index: number, item: Partial<T>): T[] {
23-
return [
24-
...array.slice(0, index),
25-
{ ...array[index], ...item },
26-
...array.slice(index + 1),
27-
];
28-
}

dashi/src/model/component.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CSSProperties } from "react";
1+
import { Component, CSSProperties } from "react";
22
import { Config, Layout, PlotData } from "plotly.js";
33

44
export type ComponentType = "Button" | "Dropdown" | "Plot" | "Box";
@@ -16,6 +16,12 @@ export interface ContainerModel extends ComponentModel {
1616
components: ComponentModel[];
1717
}
1818

19+
export function isContainerModel(
20+
componentModel: ComponentModel,
21+
): componentModel is ContainerModel {
22+
return !!(componentModel as ContainerModel).components;
23+
}
24+
1925
export interface DropdownModel extends ComponentModel {
2026
type: "Dropdown";
2127
options: Array<[string, string | number]>;

dashi/src/utils/updateArray.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export function updateArray<T>(
2+
array: T[],
3+
index: number,
4+
item: Partial<T>,
5+
): T[] {
6+
array = [...array];
7+
array[index] = { ...array[index], ...item };
8+
return array;
9+
}

0 commit comments

Comments
 (0)