Skip to content

Commit aaf3b21

Browse files
authored
Merge pull request #15 from bcdev/forman-refactorings_3
Another renaming round
2 parents 98bc27e + 9a26146 commit aaf3b21

File tree

5 files changed

+123
-87
lines changed

5 files changed

+123
-87
lines changed

dashi/src/actions/applyPropertyChange.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { describe, it, expect } from "vitest";
33
import { ComponentState, PlotState } from "../state/component";
44
import { ContribPoint } from "../model/extension";
55
import { ContributionState } from "../state/contribution";
6-
import { ChangeRequest } from "../model/callback";
6+
import { StateChangeRequest } from "../model/callback";
77
import {
88
applyComponentStateChange,
99
applyContributionChangeRequests,
@@ -33,10 +33,10 @@ describe("Test that applyContributionChangeRequests()", () => {
3333
],
3434
};
3535

36-
const changeRequest1: ChangeRequest = {
36+
const stateChangeRequest1: StateChangeRequest = {
3737
contribPoint: "panels",
3838
contribIndex: 0,
39-
changes: [
39+
stateChanges: [
4040
{
4141
kind: "Component",
4242
id: "dd1",
@@ -46,10 +46,10 @@ describe("Test that applyContributionChangeRequests()", () => {
4646
],
4747
};
4848

49-
const changeRequest2: ChangeRequest = {
49+
const stateChangeRequest2: StateChangeRequest = {
5050
contribPoint: "panels",
5151
contribIndex: 0,
52-
changes: [
52+
stateChanges: [
5353
{
5454
kind: "Component",
5555
id: "dd1",
@@ -61,7 +61,7 @@ describe("Test that applyContributionChangeRequests()", () => {
6161

6262
it("changes state if values are different", () => {
6363
const newState = applyContributionChangeRequests(contributionStatesRecord, [
64-
changeRequest1,
64+
stateChangeRequest1,
6565
]);
6666
expect(newState).not.toBe(contributionStatesRecord);
6767
expect(
@@ -71,7 +71,7 @@ describe("Test that applyContributionChangeRequests()", () => {
7171

7272
it("doesn't change the state if value stays the same", () => {
7373
const newState = applyContributionChangeRequests(contributionStatesRecord, [
74-
changeRequest2,
74+
stateChangeRequest2,
7575
]);
7676
expect(newState).toBe(contributionStatesRecord);
7777
});

dashi/src/actions/applyPropertyChange.ts

Lines changed: 69 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import appStore from "../store/appStore";
2-
import { fetchChangeRequests } from "../api";
2+
import { fetchStateChangeRequests } from "../api";
33
import fetchApiResult from "../utils/fetchApiResult";
44
import {
55
ComponentState,
@@ -8,12 +8,12 @@ import {
88
} from "../state/component";
99
import { ContribPoint } from "../model/extension";
1010
import {
11-
CallbackCallRequest,
12-
ChangeRequest,
13-
Change,
14-
CallbackCall,
1511
Callback,
12+
CallbackRef,
13+
CallbackRequest,
1614
Input,
15+
StateChange,
16+
StateChangeRequest,
1717
} from "../model/callback";
1818
import { PropertyChangeEvent } from "../model/event";
1919
import { Contribution } from "../model/contribution";
@@ -31,15 +31,17 @@ export default function applyPropertyChange(
3131
const contributionStates = contributionStatesRecord[contribPoint];
3232
const contributionModel = contributionModels[contribIndex];
3333
const contributionState = contributionStates[contribIndex];
34-
const callbackCalls = generateCallbackCalls(
34+
const callbackRefs = generateCallbackRefs(
3535
contributionModel,
3636
contributionState,
3737
contribEvent,
3838
);
39-
const triggerChangeRequest: ChangeRequest = {
39+
// The primary state change request corresponds
40+
// to the original property change event.
41+
const primaryChangeRequest: StateChangeRequest = {
4042
contribPoint,
4143
contribIndex,
42-
changes: [
44+
stateChanges: [
4345
{
4446
kind: "Component",
4547
id: contribEvent.componentId,
@@ -49,41 +51,44 @@ export default function applyPropertyChange(
4951
],
5052
};
5153
// console.debug("callRequests", callRequests);
52-
if (callbackCalls.length == 0) {
53-
applyChangeRequests([triggerChangeRequest]);
54+
if (callbackRefs.length == 0) {
55+
applyStateChangeRequests([primaryChangeRequest]);
5456
} else {
55-
const callbackCallRequests: CallbackCallRequest[] = callbackCalls.map(
56-
(cbc) => ({
57+
const callbackRequests: CallbackRequest[] = callbackRefs.map(
58+
(callbackRef) => ({
5759
contribPoint,
5860
contribIndex,
59-
...cbc,
61+
...callbackRef,
6062
}),
6163
);
62-
fetchApiResult(fetchChangeRequests, callbackCallRequests).then(
64+
fetchApiResult(fetchStateChangeRequests, callbackRequests).then(
6365
(changeRequestsResult) => {
64-
if (changeRequestsResult.data) {
65-
applyChangeRequests(
66-
[triggerChangeRequest].concat(changeRequestsResult.data),
66+
const secondaryChangeRequests = changeRequestsResult.data;
67+
if (secondaryChangeRequests) {
68+
applyStateChangeRequests(
69+
[primaryChangeRequest].concat(secondaryChangeRequests),
6770
);
6871
} else {
72+
// Note, we do not even apply the primaryChangeRequest
73+
// in order to avoid an inconsistent state.
6974
console.error(
7075
"callback failed:",
7176
changeRequestsResult.error,
7277
"for call requests:",
73-
callbackCalls,
78+
callbackRefs,
7479
);
7580
}
7681
},
7782
);
7883
}
7984
}
8085

81-
function generateCallbackCalls(
86+
function generateCallbackRefs(
8287
contributionModel: Contribution,
8388
contributionState: ContributionState,
8489
contribEvent: PropertyChangeEvent,
85-
): CallbackCall[] {
86-
const callbackCalls: CallbackCall[] = [];
90+
): CallbackRef[] {
91+
const callbackRefs: CallbackRef[] = [];
8792
// Prepare calling all callbacks of the contribution
8893
// that are triggered by the property change
8994
(contributionModel.callbacks || []).forEach((callback, callbackIndex) => {
@@ -93,10 +98,10 @@ function generateCallbackCalls(
9398
callback,
9499
);
95100
if (inputValues) {
96-
callbackCalls.push({ callbackIndex, inputValues });
101+
callbackRefs.push({ callbackIndex, inputValues });
97102
}
98103
});
99-
return callbackCalls;
104+
return callbackRefs;
100105
}
101106

102107
export function getInputValues(
@@ -148,11 +153,13 @@ export function getInputValues(
148153
});
149154
}
150155

151-
export function applyChangeRequests(changeRequests: ChangeRequest[]) {
156+
export function applyStateChangeRequests(
157+
stateChangeRequests: StateChangeRequest[],
158+
) {
152159
const { contributionStatesRecord } = appStore.getState();
153160
const contributionStatesRecordNew = applyContributionChangeRequests(
154161
contributionStatesRecord,
155-
changeRequests,
162+
stateChangeRequests,
156163
);
157164
if (contributionStatesRecordNew !== contributionStatesRecord) {
158165
appStore.setState({
@@ -164,51 +171,59 @@ export function applyChangeRequests(changeRequests: ChangeRequest[]) {
164171

165172
export function applyContributionChangeRequests(
166173
contributionStatesRecord: Record<ContribPoint, ContributionState[]>,
167-
changeRequests: ChangeRequest[],
174+
stateChangeRequests: StateChangeRequest[],
168175
): Record<ContribPoint, ContributionState[]> {
169-
changeRequests.forEach(({ contribPoint, contribIndex, changes }) => {
170-
const contributionState =
171-
contributionStatesRecord[contribPoint][contribIndex];
172-
const componentStateOld = contributionState.componentState;
173-
let componentState = componentStateOld;
174-
if (componentState) {
175-
changes
176-
.filter((change) => !change.kind || change.kind === "Component")
177-
.forEach((change) => {
178-
componentState = applyComponentStateChange(componentState!, change);
179-
});
180-
if (componentState !== componentStateOld) {
181-
contributionStatesRecord = {
182-
...contributionStatesRecord,
183-
[contribPoint]: updateArray<ContributionState>(
184-
contributionStatesRecord[contribPoint],
185-
contribIndex,
186-
{ ...contributionState, componentState },
187-
),
188-
};
176+
stateChangeRequests.forEach(
177+
({ contribPoint, contribIndex, stateChanges }) => {
178+
const contributionState =
179+
contributionStatesRecord[contribPoint][contribIndex];
180+
const componentStateOld = contributionState.componentState;
181+
let componentState = componentStateOld;
182+
if (componentState) {
183+
stateChanges
184+
.filter(
185+
(stateChange) =>
186+
!stateChange.kind || stateChange.kind === "Component",
187+
)
188+
.forEach((stateChange) => {
189+
componentState = applyComponentStateChange(
190+
componentState!,
191+
stateChange,
192+
);
193+
});
194+
if (componentState !== componentStateOld) {
195+
contributionStatesRecord = {
196+
...contributionStatesRecord,
197+
[contribPoint]: updateArray<ContributionState>(
198+
contributionStatesRecord[contribPoint],
199+
contribIndex,
200+
{ ...contributionState, componentState },
201+
),
202+
};
203+
}
189204
}
190-
}
191-
});
205+
},
206+
);
192207
return contributionStatesRecord;
193208
}
194209

195210
export function applyComponentStateChange(
196211
componentState: ComponentState,
197-
change: Change,
212+
stateChange: StateChange,
198213
): ComponentState {
199-
if (componentState.id === change.id) {
214+
if (componentState.id === stateChange.id) {
200215
const oldValue = (componentState as unknown as Record<string, unknown>)[
201-
change.property
216+
stateChange.property
202217
];
203-
if (oldValue !== change.value) {
204-
return { ...componentState, [change.property]: change.value };
218+
if (oldValue !== stateChange.value) {
219+
return { ...componentState, [stateChange.property]: stateChange.value };
205220
}
206221
} else if (isContainerState(componentState)) {
207222
const containerStateOld: ContainerState = componentState;
208223
let containerStateNew: ContainerState = containerStateOld;
209224
for (let i = 0; i < containerStateOld.components.length; i++) {
210225
const itemOld = containerStateOld.components[i];
211-
const itemNew = applyComponentStateChange(itemOld, change);
226+
const itemNew = applyComponentStateChange(itemOld, stateChange);
212227
if (itemNew !== itemOld) {
213228
if (containerStateNew === containerStateOld) {
214229
containerStateNew = {

dashi/src/api.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ComponentState } from "./state/component";
22
import { ContribPoint, Contributions } from "./model/extension";
3-
import { CallbackCallRequest, ChangeRequest } from "./model/callback";
3+
import { CallbackRequest, StateChangeRequest } from "./model/callback";
44
import { callApi } from "./utils/fetchApiResult";
55

66
const serverUrl = "http://localhost:8888";
@@ -20,11 +20,11 @@ export async function fetchInitialComponentState(
2020
});
2121
}
2222

23-
export async function fetchChangeRequests(
24-
callRequests: CallbackCallRequest[],
25-
): Promise<ChangeRequest[]> {
23+
export async function fetchStateChangeRequests(
24+
callbackRequests: CallbackRequest[],
25+
): Promise<StateChangeRequest[]> {
2626
return callApi(`${serverUrl}/dashi/callback`, {
27-
body: JSON.stringify({ callRequests }),
27+
body: JSON.stringify({ callbackRequests: callbackRequests }),
2828
method: "post",
2929
});
3030
}

dashi/src/model/callback.ts

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,47 @@ export interface Input extends InputOutput {}
2828

2929
export interface Output extends InputOutput {}
3030

31+
/**
32+
* A reference to a specific contribution.
33+
*/
3134
export interface ContribRef {
32-
// Identifies a contribution
35+
// Contribution point name, e.g., "panels"
3336
contribPoint: string;
37+
// Contribution index
3438
contribIndex: number;
3539
}
3640

37-
export interface CallbackCall {
38-
// The callback of the contribution
41+
/**
42+
* A reference to a specific callback of a contribution.
43+
*/
44+
export interface CallbackRef {
45+
// The callback index of the contribution
3946
callbackIndex: number;
40-
// The input values for the callback
47+
// The input values for the callback that will become server-side
48+
// function call arguments. They have the same size and order
49+
// as the callback's inputs.
4150
inputValues: unknown[];
4251
}
4352

44-
export interface CallbackCallRequest extends ContribRef, CallbackCall {}
53+
/**
54+
* A `CallbackRequest` is a request to invoke a server side-side callback.
55+
* The result from invoking server-side callbacks is a list of `StateChangeRequest`
56+
* instances.
57+
*/
58+
export interface CallbackRequest extends ContribRef, CallbackRef {}
4559

46-
export interface ChangeRequest extends ContribRef {
47-
// The changes requested by the contribution
48-
changes: Change[];
60+
/**
61+
* A `StateChangeRequest` is a request to change the application state.
62+
* Instances of this interface are returned from invoking a server-side callback.
63+
*/
64+
export interface StateChangeRequest extends ContribRef {
65+
// The stateChanges requested by the contribution
66+
stateChanges: StateChange[];
4967
}
5068

51-
export interface Change extends Output {
69+
/**
70+
* A single state change.
71+
*/
72+
export interface StateChange extends Output {
5273
value: unknown;
5374
}

0 commit comments

Comments
 (0)