Skip to content

Commit 096521c

Browse files
committed
update NodePin and component pin handling to use multiplicity directly
1 parent 1615ce3 commit 096521c

File tree

6 files changed

+60
-44
lines changed

6 files changed

+60
-44
lines changed

src/pages/edit/Editor/renderer/NodePin.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export default function CCComponentEditorRendererNodePin({
2929
const componentEditorState = useComponentEditorStore()();
3030
const nodePin = nullthrows(store.nodePins.get(nodePinId));
3131
const node = nullthrows(store.nodes.get(nodePin.nodeId));
32+
const nodePins = store.nodePins.getManyByNodeId(node.id);
3233
const componentPin = nullthrows(
3334
store.componentPins.get(nodePin.componentPinId),
3435
);
@@ -152,7 +153,10 @@ export default function CCComponentEditorRendererNodePin({
152153
implementationComponentPin.type === "input"
153154
) {
154155
nodePinValue = nullthrows(
155-
componentEditorState.getInputValue(implementationComponentPin.id),
156+
componentEditorState.getInputValue(
157+
implementationComponentPin.id,
158+
nodePins,
159+
),
156160
);
157161
} else {
158162
nodePinValue = nullthrows(

src/pages/edit/Editor/store/slices/core/index.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import simulateComponent from "../../../../../../store/componentEvaluator";
55
import type { CCComponentPinId } from "../../../../../../store/componentPin";
66
import type { CCConnectionId } from "../../../../../../store/connection";
77
import type { CCNodeId } from "../../../../../../store/node";
8-
import type { CCNodePinId } from "../../../../../../store/nodePin";
8+
import type { CCNodePin, CCNodePinId } from "../../../../../../store/nodePin";
99
import type { ComponentEditorSliceCreator } from "../../types";
1010
import type { EditorStoreCoreSlice } from "./types";
1111

@@ -46,12 +46,13 @@ export const createComponentEditorStoreCoreSlice: ComponentEditorSliceCreator<
4646
},
4747
/** @private */
4848
inputValues: new Map(),
49-
getInputValue(componentPinId: CCComponentPinId) {
49+
getInputValue(componentPinId: CCComponentPinId, nodePins: CCNodePin[]) {
5050
const value = get().inputValues.get(componentPinId);
5151
if (!value) {
5252
const multiplexability =
5353
store.componentPins.getComponentPinMultiplexability(
5454
componentPinId,
55+
nodePins,
5556
);
5657
if (multiplexability === "undecidable") {
5758
throw new Error("Cannot determine multiplexability");
@@ -174,8 +175,17 @@ export const createComponentEditorStoreCoreSlice: ComponentEditorSliceCreator<
174175
const inputValues = new Map<CCComponentPinId, SimulationValue>();
175176
const pins = store.componentPins.getManyByComponentId(componentId);
176177
for (const pin of pins) {
178+
invariant(pin.implementation);
177179
if (pin.type === "input") {
178-
inputValues.set(pin.id, editorState.getInputValue(pin.id));
180+
const nodePin = store.nodePins.get(pin.implementation);
181+
invariant(nodePin);
182+
const node = store.nodes.get(nodePin.nodeId);
183+
invariant(node);
184+
const nodePins = store.nodePins.getManyByNodeId(node.id);
185+
inputValues.set(
186+
pin.id,
187+
editorState.getInputValue(pin.id, nodePins),
188+
);
179189
}
180190
}
181191
simulationCachedFrames.push(

src/pages/edit/Editor/store/slices/core/types.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { Vector2 } from "../../../../../../common/vector2";
33
import type { CCComponentPinId } from "../../../../../../store/componentPin";
44
import type { CCConnectionId } from "../../../../../../store/connection";
55
import type { CCNodeId } from "../../../../../../store/node";
6-
import type { CCNodePinId } from "../../../../../../store/nodePin";
6+
import type { CCNodePin, CCNodePinId } from "../../../../../../store/nodePin";
77

88
export type EditorMode = EditorModeEdit | EditorModePlay;
99
export type EditorModeEdit = "edit";
@@ -30,7 +30,10 @@ export type EditorStoreCoreSlice = {
3030
target: NodePinPropertyEditorTarget | null,
3131
): void;
3232
inputValues: Map<InputValueKey, SimulationValue>;
33-
getInputValue(componentPinId: CCComponentPinId): SimulationValue;
33+
getInputValue(
34+
componentPinId: CCComponentPinId,
35+
nodePins: CCNodePin[],
36+
): SimulationValue;
3437
setInputValue(componentPinId: CCComponentPinId, value: SimulationValue): void;
3538
setEditorMode(mode: EditorMode): void;
3639
resetTimeStep(): void;

src/store/componentEvaluator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function createInput(
3434
);
3535
const bitWidth = multiplexability.isMultiplexable
3636
? 1
37-
: multiplexability.getMultiplicity(nodePins);
37+
: multiplexability.multiplicity;
3838
return Array<boolean>(bitWidth).fill(false);
3939
});
4040
input[key] = values;
@@ -58,7 +58,7 @@ function createOutputShape(
5858
if (multiplexability.isMultiplexable) {
5959
return 1;
6060
}
61-
return multiplexability.getMultiplicity(nodePins);
61+
return multiplexability.multiplicity;
6262
});
6363
const outputShape = multiplicity.map((multiplicity) => ({ multiplicity }));
6464
return outputShape;

src/store/componentPin.ts

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export type CCPinMultiplexability =
4949
| { isMultiplexable: true }
5050
| {
5151
isMultiplexable: false;
52-
getMultiplicity: (nodePins: CCNodePin[]) => number;
52+
multiplicity: number;
5353
};
5454
// | { isMultiplexable: false; multiplicity: number };
5555

@@ -243,6 +243,7 @@ export class CCComponentPinStore extends EventEmitter<CCComponentPinStoreEvents>
243243
*/
244244
getComponentPinMultiplexability(
245245
pinId: CCComponentPinId,
246+
nodePins: CCNodePin[],
246247
): CCComponentPinMultiplexability {
247248
const pin = this.#pins.get(pinId);
248249
invariant(pin);
@@ -268,52 +269,46 @@ export class CCComponentPinStore extends EventEmitter<CCComponentPinStoreEvents>
268269
return "undecidable";
269270
}
270271
case nullthrows(aggregate.outputPin.id): {
271-
const getMultiplicity = (nodePins: CCNodePin[]): number => {
272-
const multiplicity = nodePins
273-
.filter((pin) => {
274-
const componentPin = this.#store.componentPins.get(
275-
pin.componentPinId,
276-
);
277-
invariant(componentPin);
278-
return componentPin.type === "input";
279-
})
280-
.reduce((acc, pin) => {
281-
invariant(pin.userSpecifiedBitWidth);
282-
return acc + pin.userSpecifiedBitWidth;
283-
}, 0);
284-
return multiplicity;
285-
};
272+
const multiplicity = nodePins
273+
.filter((pin) => {
274+
const componentPin = this.#store.componentPins.get(
275+
pin.componentPinId,
276+
);
277+
invariant(componentPin);
278+
return componentPin.type === "input";
279+
})
280+
.reduce((acc, pin) => {
281+
invariant(pin.userSpecifiedBitWidth);
282+
return acc + pin.userSpecifiedBitWidth;
283+
}, 0);
286284
return {
287285
isMultiplexable: false,
288-
getMultiplicity,
286+
multiplicity,
289287
};
290288
}
291289
case nullthrows(decompose.outputPin.id): {
292290
return "undecidable";
293291
}
294292
case nullthrows(decompose.inputPin.In.id): {
295-
const getMultiplicity = (nodePins: CCNodePin[]): number => {
296-
const multiplicity = nodePins
297-
.filter((pin) => {
298-
const componentPin = this.#store.componentPins.get(
299-
pin.componentPinId,
300-
);
301-
invariant(componentPin);
302-
return componentPin.type === "output";
303-
})
304-
.reduce((acc, pin) => {
305-
invariant(pin.userSpecifiedBitWidth);
306-
return acc + pin.userSpecifiedBitWidth;
307-
}, 0);
308-
return multiplicity;
309-
};
293+
const multiplicity = nodePins
294+
.filter((pin) => {
295+
const componentPin = this.#store.componentPins.get(
296+
pin.componentPinId,
297+
);
298+
invariant(componentPin);
299+
return componentPin.type === "output";
300+
})
301+
.reduce((acc, pin) => {
302+
invariant(pin.userSpecifiedBitWidth);
303+
return acc + pin.userSpecifiedBitWidth;
304+
}, 0);
310305
return {
311306
isMultiplexable: false,
312-
getMultiplicity,
307+
multiplicity,
313308
};
314309
}
315310
case nullthrows(broadcast.inputPin.In.id): {
316-
return { isMultiplexable: false, getMultiplicity: () => 1 };
311+
return { isMultiplexable: false, multiplicity: 1 };
317312
}
318313
case nullthrows(broadcast.outputPin.id): {
319314
return "undecidable";

src/store/nodePin.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,15 +188,18 @@ export class CCNodePinStore extends EventEmitter<CCNodePinStoreEvents> {
188188
const node = nullthrows(this.#store.nodes.get(nodeId));
189189
const nodePins = this.getManyByNodeId(node.id);
190190
const givenPinMultiplexability =
191-
this.#store.componentPins.getComponentPinMultiplexability(pinId);
191+
this.#store.componentPins.getComponentPinMultiplexability(
192+
pinId,
193+
nodePins,
194+
);
192195
if (givenPinMultiplexability === "undecidable") {
193196
invariant(
194197
userSpecifiedBitWidth,
195198
"Multiplexability of undecidable pin must be contained in multiplexabilityEnv",
196199
);
197200
return {
198201
isMultiplexable: false,
199-
getMultiplicity: () => userSpecifiedBitWidth,
202+
multiplicity: userSpecifiedBitWidth,
200203
};
201204
}
202205
if (!givenPinMultiplexability.isMultiplexable) {
@@ -206,6 +209,7 @@ export class CCNodePinStore extends EventEmitter<CCNodePinStoreEvents> {
206209
const pinMultiplexability =
207210
this.#store.componentPins.getComponentPinMultiplexability(
208211
nodePin.componentPinId,
212+
nodePins,
209213
);
210214
if (pinMultiplexability === "undecidable") {
211215
throw new Error("unreachable");

0 commit comments

Comments
 (0)