Skip to content

Commit

Permalink
feat related to #38: It is now possible to get all Inputs from all ha…
Browse files Browse the repository at this point in the history
…ndles in an object with the handleIds as keys. This is useful for dynamic nodes where it is uncertain what handles exist
  • Loading branch information
MertenD committed Aug 27, 2023
1 parent f44e774 commit 4fc1423
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/engine/nodes/EngineDatabaseTableNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export class EngineDatabaseTableNode implements BasicNode {

async run() {

const inputs = usePlayStore.getState().getInputForAllHandles(this.id)

console.log("Inputs", inputs)

setTimeout(() => {
usePlayStore.getState().nextNode()
}, 2000);
Expand Down
34 changes: 33 additions & 1 deletion src/stores/editor/PlayStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export type PlayStoreState = {
addOutgoingPipelines: (from: string, value?: Output[] | Output | null) => void
deactivateIngoingPipelines: (to: string) => void
getInput: (nodeId: string, handleId: string, flattenInput?: boolean) => Output[][] | Output[] | undefined
getInputForAllHandles: (nodeId: string, flattenInput?: boolean) => Record<string, Output[][] | Output[]> | undefined
}

export const usePlayStore = create<PlayStoreState>((set, get) => ({
Expand Down Expand Up @@ -257,7 +258,10 @@ export const usePlayStore = create<PlayStoreState>((set, get) => ({
// Get amount of connected pipelines to the targetHandle
const ingoingConnections = Array.from(get().nodeMap.values()).filter((value: NodeMapValue) => {
if (value.next !== null) {
return Object.values(value.next).some(nextValue => nextValue.map(n => n.nodeId).includes(nodeId));
return Object.values(value.next).some(nextValue =>
nextValue.map(n => n.nodeId).includes(nodeId) &&
nextValue.map(n => n.targetHandleId).includes(handleId)
);
}
return false;
}).length;
Expand Down Expand Up @@ -298,5 +302,33 @@ export const usePlayStore = create<PlayStoreState>((set, get) => ({
get().backtrackToNextPossibleNode()
return undefined
}
},
getInputForAllHandles: (nodeId: string, flattenInput: boolean = true): Record<string, Output[][] | Output[]> | undefined => {
const handleIds = Array.from(new Set(useReactFlowStore.getState().edges
.filter(edge => edge.target === nodeId)
.map(edge => {
return edge.targetHandle
})
.filter(handleId => handleId)
)) as string[]

const inputs: Record<string, Output[][] | Output[]> = {};
let allInputsDefined = true;

for (const handleId of handleIds) {
const input = get().getInput(nodeId, handleId, flattenInput);
if (input) {
inputs[handleId] = input;
} else {
allInputsDefined = false;
break;
}
}

if (!allInputsDefined) {
return undefined;
}

return inputs
}
}))

0 comments on commit 4fc1423

Please sign in to comment.