Skip to content

Commit a28ac03

Browse files
committed
Introduced new callback I/O types AppInput, AppOutput, and State
1 parent 4955d32 commit a28ac03

File tree

7 files changed

+56
-42
lines changed

7 files changed

+56
-42
lines changed

dashi/src/lib/actions/handleComponentChange.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export function handleComponentChange(
1717
contribIndex,
1818
stateChanges: [
1919
{
20-
kind: "Component",
20+
type: "Output",
2121
id: changeEvent.componentId,
2222
property: changeEvent.propertyName,
2323
value: changeEvent.propertyValue,
@@ -57,7 +57,7 @@ function getCallbackRequests(
5757
const inputs = callback.inputs;
5858
const inputIndex = inputs.findIndex(
5959
(input) =>
60-
(!input.kind || input.kind === "Component") &&
60+
(!input.type || input.type === "Input") &&
6161
input.id === changeEvent.componentId &&
6262
input.property === changeEvent.propertyName,
6363
);

dashi/src/lib/actions/handleHostStoreChange.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ function _getHostStorePropertyRefs(): PropertyRef[] {
6868
(contribution.callbacks || []).forEach(
6969
(callback, callbackIndex) =>
7070
(callback.inputs || []).forEach((input, inputIndex) => {
71-
if (input.kind === "AppState") {
71+
if (input.type === "AppInput") {
7272
propertyRefs.push({
7373
contribPoint,
7474
contribIndex,

dashi/src/lib/actions/helpers/applyStateChangeRequests.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ describe("Test that applyContributionChangeRequests()", () => {
4646
contribIndex: 0,
4747
stateChanges: [
4848
{
49-
kind: "Component",
49+
type: "Output",
5050
id: "dd1",
5151
property: "value",
5252
value: 14,
@@ -59,7 +59,7 @@ describe("Test that applyContributionChangeRequests()", () => {
5959
contribIndex: 0,
6060
stateChanges: [
6161
{
62-
kind: "Component",
62+
type: "Output",
6363
id: "dd1",
6464
property: "value",
6565
value: 13,
@@ -88,7 +88,7 @@ describe("Test that applyContributionChangeRequests()", () => {
8888
describe("Test that applyComponentStateChange()", () => {
8989
it("changes state if values are different", () => {
9090
const newState = applyComponentStateChange(componentTree, {
91-
kind: "Component",
91+
type: "Output",
9292
id: "cb1",
9393
property: "value",
9494
value: false,
@@ -99,7 +99,7 @@ describe("Test that applyComponentStateChange()", () => {
9999

100100
it("doesn't change the state if value stays the same", () => {
101101
const newState = applyComponentStateChange(componentTree, {
102-
kind: "Component",
102+
type: "Output",
103103
id: "cb1",
104104
property: "value",
105105
value: true,

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

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ function applyHostStateChanges(stateChangeRequests: StateChangeRequest[]) {
3737
stateChangeRequests.forEach((stateChangeRequest) => {
3838
hostState = applyStateChanges(
3939
hostState,
40-
stateChangeRequest.stateChanges,
41-
"AppState",
40+
stateChangeRequest.stateChanges.filter(
41+
(stateChange) => stateChange.type === "AppOutput",
42+
),
4243
);
4344
});
4445
if (hostState !== hostStateOld) {
@@ -53,13 +54,9 @@ function applyComponentStateChanges(
5354
) {
5455
let component = componentOld;
5556
if (component) {
56-
stateChanges
57-
.filter(
58-
(stateChange) => !stateChange.kind || stateChange.kind === "Component",
59-
)
60-
.forEach((stateChange) => {
61-
component = applyComponentStateChange(component!, stateChange);
62-
});
57+
stateChanges.forEach((stateChange) => {
58+
component = applyComponentStateChange(component!, stateChange);
59+
});
6360
}
6461
return component;
6562
}
@@ -74,12 +71,19 @@ export function applyContributionChangeRequests(
7471
const contribution = contributionsRecord[contribPoint][contribIndex];
7572
const state = applyStateChanges(
7673
contribution.state,
77-
stateChanges,
78-
"State",
74+
stateChanges.filter(
75+
(stateChange) =>
76+
(!stateChange.type || stateChange.type === "Output") &&
77+
!stateChange.id,
78+
),
7979
);
8080
const component = applyComponentStateChanges(
8181
contribution.component,
82-
stateChanges,
82+
stateChanges.filter(
83+
(stateChange) =>
84+
(!stateChange.type || stateChange.type === "Output") &&
85+
stateChange.id,
86+
),
8387
);
8488
if (
8589
state !== contribution.state ||
@@ -139,14 +143,12 @@ export function applyComponentStateChange(
139143
export function applyStateChanges<S extends object>(
140144
state: S | undefined,
141145
stateChanges: StateChange[],
142-
kind: "State" | "AppState",
143146
): S | undefined {
144147
stateChanges.forEach((stateChange) => {
145148
if (
146-
stateChange.kind === kind &&
147-
(!state ||
148-
(state as unknown as Record<string, unknown>)[stateChange.property] !==
149-
stateChange.value)
149+
!state ||
150+
(state as unknown as Record<string, unknown>)[stateChange.property] !==
151+
stateChange.value
150152
) {
151153
state = { ...state, [stateChange.property]: stateChange.value } as S;
152154
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ describe("Test that getComponentStateValue()", () => {
2727
it("works on 1st level", () => {
2828
expect(
2929
getComponentStateValue(componentState, {
30-
kind: "Component",
30+
type: "Input",
3131
id: "b1",
3232
property: "value",
3333
}),
@@ -37,7 +37,7 @@ describe("Test that getComponentStateValue()", () => {
3737
it("works on 2nd level", () => {
3838
expect(
3939
getComponentStateValue(componentState, {
40-
kind: "Component",
40+
type: "Input",
4141
id: "p1",
4242
property: "chart",
4343
}),
@@ -47,15 +47,15 @@ describe("Test that getComponentStateValue()", () => {
4747
it("works on 3rd level", () => {
4848
expect(
4949
getComponentStateValue(componentState, {
50-
kind: "Component",
50+
type: "Input",
5151
id: "cb1",
5252
property: "value",
5353
}),
5454
).toEqual(true);
5555

5656
expect(
5757
getComponentStateValue(componentState, {
58-
kind: "Component",
58+
type: "Input",
5959
id: "dd1",
6060
property: "value",
6161
}),
@@ -67,21 +67,21 @@ describe("Test that getInputValueFromState()", () => {
6767
it("works with input.id and input.property", () => {
6868
const state = { x: { y: 26 } };
6969
expect(
70-
getInputValueFromState({ kind: "State", id: "x", property: "y" }, state),
70+
getInputValueFromState({ type: "State", id: "x", property: "y" }, state),
7171
).toEqual(26);
7272
});
7373

7474
it("works with arrays indexes", () => {
7575
const state = { x: [4, 5, 6] };
7676
expect(
77-
getInputValueFromState({ kind: "State", id: "x", property: "1" }, state),
77+
getInputValueFromState({ type: "State", id: "x", property: "1" }, state),
7878
).toEqual(5);
7979
});
8080

8181
it("works without input.id", () => {
8282
const state = { x: [4, 5, 6] };
8383
expect(
84-
getInputValueFromState({ kind: "State", property: "x" }, state),
84+
getInputValueFromState({ type: "State", property: "x" }, state),
8585
).toEqual([4, 5, 6]);
8686
});
8787
});

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@ export function getInputValue<S extends object = object>(
2222
hostState?: S | undefined,
2323
): unknown {
2424
let inputValue: unknown = undefined;
25-
const inputKind = input.kind || "Component";
26-
if (inputKind === "Component" && contributionState.component) {
25+
const inputKind = input.type || "Input";
26+
if (
27+
(inputKind === "Input" || inputKind === "State") &&
28+
contributionState.component
29+
) {
2730
// Return value of a property of some component in the tree
2831
inputValue = getComponentStateValue(contributionState.component, input);
29-
} else if (inputKind === "State" && contributionState.state) {
30-
inputValue = getInputValueFromState(input, contributionState.state);
31-
} else if (inputKind === "AppState" && hostState) {
32+
} else if (inputKind === "AppInput" && hostState) {
3233
inputValue = getInputValueFromState(input, hostState);
3334
} else {
3435
console.warn(`input of unknown kind:`, input);

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,21 @@ export interface CbParameter {
3030
default?: unknown;
3131
}
3232

33-
export type InputOutputKind = "AppState" | "State" | "Component";
33+
export type InputType = "AppInput" | "Input" | "State";
34+
35+
export type OutputType = "AppOutput" | "Output";
36+
37+
export type InputOutputType = InputType | OutputType;
3438

3539
export interface InputOutput {
36-
/** The kind of input or output. */
37-
kind: InputOutputKind;
40+
/**
41+
* The type of input or output.
42+
*/
43+
type: InputOutputType;
44+
3845
/**
3946
* The identifier of a subcomponent.
40-
* `id` is not needed if kind == "AppState" | "State".
47+
* `id` is not needed if type == "AppInput" | "AppOutput".
4148
*/
4249
id?: string;
4350

@@ -52,9 +59,13 @@ export interface InputOutput {
5259
property: string;
5360
}
5461

55-
export interface Input extends InputOutput {}
62+
export interface Input extends InputOutput {
63+
type: InputType;
64+
}
5665

57-
export interface Output extends InputOutput {}
66+
export interface Output extends InputOutput {
67+
type: OutputType;
68+
}
5869

5970
/**
6071
* A reference to a specific contribution.

0 commit comments

Comments
 (0)