sachaw
sachaw
Explore posts from servers
SSolidJS
Created by sachaw on 4/29/2024 in #support
Force effect computation, or alternate approach?
I'm trying to write my own force directed graph, I am able to display nodes and links just fine with a random initial coordinate. I also have a simple function that calculates the next positions for the nodes. However I need to call that function at a rather high rate. When I put it inside of an interval, nothing gets updated. I'm pobbably missing something here. Any assistance would be much appreciated.
11 replies
SSolidJS
Created by sachaw on 11/21/2023 in #support
Most concise upsert for store?
I'm wondering if there was a more efficient way of doing an upsert operation? This is what I'm doing currently:
const updateNodes = (node: Node) => {
setLocalState(
"nodes",
produce((nodes) => {
const nodeIndex = nodes.findIndex((n) => n.id === node.id);

if (nodeIndex !== -1) {
nodes[nodeIndex] = node;
} else {
nodes.push(node);
}
}),
);
};
const updateNodes = (node: Node) => {
setLocalState(
"nodes",
produce((nodes) => {
const nodeIndex = nodes.findIndex((n) => n.id === node.id);

if (nodeIndex !== -1) {
nodes[nodeIndex] = node;
} else {
nodes.push(node);
}
}),
);
};
3 replies
SSolidJS
Created by sachaw on 11/19/2023 in #support
Nicest way of having computed properties in a store?
I'm trying to model my store in a concise way, I want to have some base properties, and some computed properties, I think the cleanest way is to use getter/setters but currently they are not reactive as I'm clearly doing something wrong. This is what I have currently:
interface LocalState {
currentOperatorId: string;
currentOperator?: Operator;
operators: Operator[];
nodes: Node[];
}

const [localState, setLocalState] = createStore<LocalState>({
currentOperatorId: "",
// Inline getter, not currently reactive.
get currentOperator(): Operator | undefined {
return this.operators.find((op) => op.id === this.currentOperatorId);
},
nodes: [],
operators: [],
});

// External setter method, more code
const setCurrentOperator = (operatorId: string) =>
setLocalState("currentOperatorId", operatorId);
interface LocalState {
currentOperatorId: string;
currentOperator?: Operator;
operators: Operator[];
nodes: Node[];
}

const [localState, setLocalState] = createStore<LocalState>({
currentOperatorId: "",
// Inline getter, not currently reactive.
get currentOperator(): Operator | undefined {
return this.operators.find((op) => op.id === this.currentOperatorId);
},
nodes: [],
operators: [],
});

// External setter method, more code
const setCurrentOperator = (operatorId: string) =>
setLocalState("currentOperatorId", operatorId);
13 replies