Skip to content

Commit 75dadf8

Browse files
committed
Renamed InputOutput into Channel with common property "link"
1 parent 9492676 commit 75dadf8

File tree

9 files changed

+72
-67
lines changed

9 files changed

+72
-67
lines changed

dashi/src/lib/actions/handleComponentChange.ts

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

dashi/src/lib/actions/handleHostStoreChange.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import type {
55
CallbackRef,
66
CallbackRequest,
77
ContribRef,
8-
Input,
98
InputRef,
109
} from "@/lib/types/model/callback";
10+
import type { Input } from "@/lib/types/model/channel";
1111
import { getInputValues } from "@/lib/actions/helpers/getInputValues";
1212
import { getValue, type PropertyPath } from "@/lib/utils/getValue";
1313
import { invokeCallbacks } from "@/lib/actions/helpers/invokeCallbacks";
@@ -59,6 +59,9 @@ function getCallbackRequests<S extends object = object>(
5959

6060
const getHostStorePropertyRefs = memoizeOne(_getHostStorePropertyRefs);
6161

62+
/**
63+
* Get the static list of host state property references for all contributions.
64+
*/
6265
function _getHostStorePropertyRefs(): PropertyRef[] {
6366
const { contributionsRecord } = store.getState();
6467
const propertyRefs: PropertyRef[] = [];
@@ -68,13 +71,13 @@ function _getHostStorePropertyRefs(): PropertyRef[] {
6871
(contribution.callbacks || []).forEach(
6972
(callback, callbackIndex) =>
7073
(callback.inputs || []).forEach((input, inputIndex) => {
71-
if (input.type === "AppInput") {
74+
if (!input.noTrigger && input.link === "app") {
7275
propertyRefs.push({
7376
contribPoint,
7477
contribIndex,
7578
callbackIndex,
7679
inputIndex,
77-
propertyPath: input.property.split("."),
80+
propertyPath: input.property!.split("."),
7881
});
7982
}
8083
}),

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-
type: "Output",
49+
link: "component",
5050
id: "dd1",
5151
property: "value",
5252
value: 14,
@@ -59,7 +59,7 @@ describe("Test that applyContributionChangeRequests()", () => {
5959
contribIndex: 0,
6060
stateChanges: [
6161
{
62-
type: "Output",
62+
link: "component",
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-
type: "Output",
91+
link: "component",
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-
type: "Output",
102+
link: "component",
103103
id: "cb1",
104104
property: "value",
105105
value: true,

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function applyHostStateChanges(stateChangeRequests: StateChangeRequest[]) {
3838
hostState = applyStateChanges(
3939
hostState,
4040
stateChangeRequest.stateChanges.filter(
41-
(stateChange) => stateChange.type === "AppOutput",
41+
(stateChange) => stateChange.link === "app",
4242
),
4343
);
4444
});
@@ -72,16 +72,14 @@ export function applyContributionChangeRequests(
7272
const state = applyStateChanges(
7373
contribution.state,
7474
stateChanges.filter(
75-
(stateChange) =>
76-
(!stateChange.type || stateChange.type === "Output") &&
77-
!stateChange.id,
75+
(stateChange) => stateChange.link === "container" && !stateChange.id,
7876
),
7977
);
8078
const component = applyComponentStateChanges(
8179
contribution.component,
8280
stateChanges.filter(
8381
(stateChange) =>
84-
(!stateChange.type || stateChange.type === "Output") &&
82+
(!stateChange.link || stateChange.link === "component") &&
8583
stateChange.id,
8684
),
8785
);

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

Lines changed: 13 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-
type: "Input",
30+
link: "component",
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-
type: "Input",
40+
link: "component",
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-
type: "Input",
50+
link: "component",
5151
id: "cb1",
5252
property: "value",
5353
}),
5454
).toEqual(true);
5555

5656
expect(
5757
getComponentStateValue(componentState, {
58-
type: "Input",
58+
link: "component",
5959
id: "dd1",
6060
property: "value",
6161
}),
@@ -67,21 +67,27 @@ describe("Test that getInputValueFromState()", () => {
6767
it("works with input.id and input.property", () => {
6868
const state = { x: { y: 26 } };
6969
expect(
70-
getInputValueFromState({ type: "State", id: "x", property: "y" }, state),
70+
getInputValueFromState(
71+
{ link: "component", id: "x", property: "y" },
72+
state,
73+
),
7174
).toEqual(26);
7275
});
7376

7477
it("works with arrays indexes", () => {
7578
const state = { x: [4, 5, 6] };
7679
expect(
77-
getInputValueFromState({ type: "State", id: "x", property: "1" }, state),
80+
getInputValueFromState(
81+
{ link: "component", id: "x", property: "1" },
82+
state,
83+
),
7884
).toEqual(5);
7985
});
8086

8187
it("works without input.id", () => {
8288
const state = { x: [4, 5, 6] };
8389
expect(
84-
getInputValueFromState({ type: "State", property: "x" }, state),
90+
getInputValueFromState({ link: "container", property: "x" }, state),
8591
).toEqual([4, 5, 6]);
8692
});
8793
});

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Input } from "@/lib/types/model/callback";
1+
import type { Input } from "@/lib/types/model/channel";
22
import type { ContributionState } from "@/lib/types/state/contribution";
33
import type { ComponentState } from "@/lib/types/state/component";
44
import { isSubscriptable } from "@/lib/utils/isSubscriptable";
@@ -22,17 +22,16 @@ export function getInputValue<S extends object = object>(
2222
hostState?: S | undefined,
2323
): unknown {
2424
let inputValue: unknown = undefined;
25-
const inputKind = input.type || "Input";
26-
if (
27-
(inputKind === "Input" || inputKind === "State") &&
28-
contributionState.component
29-
) {
25+
const dataSource = input.link || "component";
26+
if (dataSource === "component" && contributionState.component) {
3027
// Return value of a property of some component in the tree
3128
inputValue = getComponentStateValue(contributionState.component, input);
32-
} else if (inputKind === "AppInput" && hostState) {
29+
} else if (dataSource === "container" && contributionState.state) {
30+
inputValue = getInputValueFromState(input, hostState);
31+
} else if (dataSource === "app" && hostState) {
3332
inputValue = getInputValueFromState(input, hostState);
3433
} else {
35-
console.warn(`input of unknown kind:`, input);
34+
console.warn(`input with unknown data source:`, input);
3635
}
3736
if (inputValue === undefined || inputValue === noValue) {
3837
// We use null, because undefined is not JSON-serializable.

dashi/src/lib/components/Component.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export function Component({ type, ...props }: ComponentProps) {
1515
// TODO: allow for registering components via their types
1616
// and make following code generic.
1717
//
18-
// const DashiComp = Registry.getComponent(type);
18+
// const DashiComp = Registry.getComponent(link);
1919
// return return <DashiComp {...props} />;
2020
//
2121
if (type === "Plot") {

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

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { Input, Output } from "@/lib/types/model/channel";
2+
13
export interface Callback {
24
function: CbFunction;
35
inputs?: Input[];
@@ -30,43 +32,6 @@ export interface CbParameter {
3032
default?: unknown;
3133
}
3234

33-
export type InputType = "AppInput" | "Input" | "State";
34-
35-
export type OutputType = "AppOutput" | "Output";
36-
37-
export type InputOutputType = InputType | OutputType;
38-
39-
export interface InputOutput {
40-
/**
41-
* The type of input or output.
42-
*/
43-
type: InputOutputType;
44-
45-
/**
46-
* The identifier of a subcomponent.
47-
* `id` is not needed if type == "AppInput" | "AppOutput".
48-
*/
49-
id?: string;
50-
51-
// TODO: we must allow `property` to be a constant
52-
// expression of the form: name {"." name | index}.
53-
// Then we get the normalized form
54-
// property: string[];
55-
56-
/**
57-
* The property of an object or array index.
58-
*/
59-
property: string;
60-
}
61-
62-
export interface Input extends InputOutput {
63-
type: InputType;
64-
}
65-
66-
export interface Output extends InputOutput {
67-
type: OutputType;
68-
}
69-
7035
/**
7136
* A reference to a specific contribution.
7237
*/
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
export type Link = "component" | "container" | "app";
2+
3+
/**
4+
* Base for `Input` and `Output`.
5+
*/
6+
export interface Channel {
7+
/**
8+
* The link provides the source for inputs and that target for outputs.
9+
*/
10+
link: Link;
11+
12+
/**
13+
* The identifier of a subcomponent.
14+
* `id` is not needed if link == "AppInput" | "AppOutput".
15+
*/
16+
id?: string;
17+
18+
// TODO: we must allow `property` to be a constant
19+
// expression of the form: name {"." name | index}.
20+
// Then we get the normalized form
21+
// property: string[];
22+
23+
/**
24+
* The property of an object or array index.
25+
*/
26+
property: string;
27+
}
28+
29+
export interface Input extends Channel {
30+
noTrigger?: boolean;
31+
}
32+
33+
export interface Output extends Channel {}

0 commit comments

Comments
 (0)