chore: use query to manage last run to decrease the code complex
parent
02110b4323
commit
82aca36fc1
@ -1,67 +0,0 @@
|
|||||||
import produce from 'immer'
|
|
||||||
import type { StateCreator } from 'zustand'
|
|
||||||
import type { NodeRunResult } from '@/types/workflow'
|
|
||||||
|
|
||||||
type LastRunState = {
|
|
||||||
nodes: NodeRunResult[]
|
|
||||||
}
|
|
||||||
|
|
||||||
type LastRunActions = {
|
|
||||||
setLastRunInfos: (vars: NodeRunResult[]) => void
|
|
||||||
getLastRunInfos: () => NodeRunResult[]
|
|
||||||
setLastRunNodeInfo: (nodeId: string, payload: NodeRunResult) => void
|
|
||||||
getLastRunNodeInfo: (nodeId: string) => NodeRunResult | undefined
|
|
||||||
getLastRunVar: (nodeId: string, key: string) => any
|
|
||||||
}
|
|
||||||
|
|
||||||
export type LastRunSliceShape = LastRunState & LastRunActions
|
|
||||||
|
|
||||||
export const createLastRunSlice: StateCreator<LastRunSliceShape> = (set, get) => {
|
|
||||||
return ({
|
|
||||||
nodes: [],
|
|
||||||
setLastRunInfos: (vars) => {
|
|
||||||
set(() => ({
|
|
||||||
nodes: vars,
|
|
||||||
}))
|
|
||||||
},
|
|
||||||
getLastRunInfos: () => {
|
|
||||||
return get().nodes
|
|
||||||
},
|
|
||||||
clearVars: () => {
|
|
||||||
set(() => ({
|
|
||||||
nodes: [],
|
|
||||||
}))
|
|
||||||
},
|
|
||||||
setLastRunNodeInfo: (nodeId, payload) => {
|
|
||||||
set((state) => {
|
|
||||||
const prevNodes = state.nodes
|
|
||||||
const nodes = produce(prevNodes, (draft) => {
|
|
||||||
const index = prevNodes.findIndex(node => node.id === nodeId)
|
|
||||||
if (index === -1)
|
|
||||||
draft.push(payload)
|
|
||||||
else
|
|
||||||
draft[index] = payload
|
|
||||||
})
|
|
||||||
|
|
||||||
return {
|
|
||||||
nodes,
|
|
||||||
}
|
|
||||||
})
|
|
||||||
},
|
|
||||||
getLastRunNodeInfo: (nodeId) => {
|
|
||||||
const nodes = get().nodes
|
|
||||||
return nodes.find(node => node.node_id === nodeId)
|
|
||||||
},
|
|
||||||
getLastRunVar: (nodeId, key) => {
|
|
||||||
const node = get().getLastRunNodeInfo(nodeId)
|
|
||||||
if (!node)
|
|
||||||
return undefined
|
|
||||||
|
|
||||||
const varItem = node
|
|
||||||
if (!varItem)
|
|
||||||
return undefined
|
|
||||||
|
|
||||||
return varItem.outputs?.[key]
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue