Skip to content

Commit

Permalink
timer, gate, and held valiue should be properties of node instance, n…
Browse files Browse the repository at this point in the history
…ot of factory
  • Loading branch information
bacalj committed Mar 25, 2024
1 parent 40c8fe7 commit 764d7e7
Showing 1 changed file with 29 additions and 21 deletions.
50 changes: 29 additions & 21 deletions src/plugins/dataflow/nodes/factories/control-rete-node-factory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,21 @@ import { HoldFunctionOptions } from "../../model/utilities/node";
import { PlotButtonControl } from "../controls/plot-button-control";
import { determineGateAndTimerStates, getHoldNodeResultString } from "../utilities/view-utilities";

interface HoldNodeData {
gateActive: boolean;
heldValue: number | null;
timerRunning: boolean;
}
export class ControlReteNodeFactory extends DataflowReteNodeFactory {
constructor(numSocket: Socket) {
super("Control", numSocket);
}

private heldValue: number | null = null;
private timerRunning: boolean = false;

private startTimer(duration: number) {
if (this.timerRunning) return;
this.timerRunning = true;
private startTimer(node: NodeData, duration: number) {

Check warning on line 21 in src/plugins/dataflow/nodes/factories/control-rete-node-factory.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/dataflow/nodes/factories/control-rete-node-factory.tsx#L21

Added line #L21 was not covered by tests
if (node.data.timerRunning) return;
node.data.timerRunning = true;
setTimeout(() => {
this.timerRunning = false;
node.data.timerRunning = false;

Check warning on line 25 in src/plugins/dataflow/nodes/factories/control-rete-node-factory.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/dataflow/nodes/factories/control-rete-node-factory.tsx#L23-L25

Added lines #L23 - L25 were not covered by tests
}, duration * 1000);
}

Expand All @@ -31,7 +33,12 @@ export class ControlReteNodeFactory extends DataflowReteNodeFactory {
const valueInput = new Rete.Input("num2", "Number2", this.numSocket);
const out = new Rete.Output("num", "Number", this.numSocket);

node.data.gateActive = false;
node.data = {
...node.data,
gateActive: false,
heldValue: null,
timerRunning: false,
} as NodeData['data'] & HoldNodeData;

const dropdownOptions = HoldFunctionOptions
.map((nodeOp) => {
Expand All @@ -54,38 +61,39 @@ export class ControlReteNodeFactory extends DataflowReteNodeFactory {
const recents: number[] | undefined = (node.data.recentValues as any)?.nodeValue;
const lastRecentValue = recents?.[recents.length - 1];
const priorValue = lastRecentValue == null ? null : lastRecentValue;
const isTimerRunning = node.data.timerRunning as boolean;

let result = 0;
let cResult = 0;

const { activateGate, startTimer } = determineGateAndTimerStates(node, inputs, this.timerRunning);
startTimer && this.startTimer(node.data.waitDuration as number);
const { activateGate, startTimer } = determineGateAndTimerStates(node, inputs, isTimerRunning);
startTimer && this.startTimer(node, node.data.waitDuration as number);
node.data.gateActive = activateGate;

// requires value in signalValue (except for case of Output Zero)
if (isNaN(signalValue)) {
this.heldValue = null;
node.data.heldValue = null;
result = NaN;
cResult = NaN;
}

// For each function, evaluate given inputs and node state
// TODO - check and see if this gets serialized, and if so, how to handle legacy funcNames on load
if (funcName === "Hold 0" || funcName === "Output Zero"){
this.heldValue = null;
node.data.heldValue = null;
result = node.data.gateActive ? 0 : signalValue;
cResult = 0;
}

else if (funcName === "Hold Current"){
if (node.data.gateActive){
// Already a number here? Maintain. Otherwise set the new held value;
this.heldValue = typeof this.heldValue === "number" ? this.heldValue : signalValue;
result = this.heldValue;
cResult = this.heldValue;
node.data.heldValue = typeof node.data.heldValue === "number" ? node.data.heldValue : signalValue;
result = node.data.heldValue as number;
cResult = node.data.heldValue as number;

Check warning on line 93 in src/plugins/dataflow/nodes/factories/control-rete-node-factory.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/dataflow/nodes/factories/control-rete-node-factory.tsx#L92-L93

Added lines #L92 - L93 were not covered by tests
}
else {
this.heldValue = null;
node.data.heldValue = null;
result = signalValue;
cResult = signalValue; // still signalValue, since the value to be held would be the current
}
Expand All @@ -94,18 +102,18 @@ export class ControlReteNodeFactory extends DataflowReteNodeFactory {
else if (funcName === "Hold Prior"){
if (node.data.gateActive){
// Already a number here? Maintain. Otherwise set the new held value;
this.heldValue = typeof this.heldValue === "number" ? this.heldValue : priorValue;
result = this.heldValue || 0;
cResult = this.heldValue || 0;
node.data.heldValue = typeof node.data.heldValue === "number" ? node.data.heldValue : priorValue;
result = node.data.heldValue as number || 0;
cResult = node.data.heldValue as number || 0;
}
else {
this.heldValue = null;
node.data.heldValue = null;
result = signalValue;

Check warning on line 111 in src/plugins/dataflow/nodes/factories/control-rete-node-factory.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/dataflow/nodes/factories/control-rete-node-factory.tsx#L110-L111

Added lines #L110 - L111 were not covered by tests
cResult = priorValue || 0;
}
}

const resultSentence = getHoldNodeResultString(node, result, cResult, this.timerRunning) || "";
const resultSentence = getHoldNodeResultString(node, result, cResult, isTimerRunning) || "";

// operate rete
if (this.editor) {
Expand Down

0 comments on commit 764d7e7

Please sign in to comment.