Line data Source code
1 : import { storeToRefs } from 'pinia'
2 : import { computed, toValue } from 'vue'
3 : import type { MaybeRefOrGetter } from 'vue'
4 :
5 : import { useExecutionStore } from '@/stores/executionStore'
6 : import type { NodeLocatorId } from '@comfyorg/graph-model/nodeIdentification'
7 :
8 : /**
9 : * Composable for managing execution state of Vue-based nodes
10 : *
11 : * Provides reactive access to execution state and progress for a specific node
12 : * by injecting execution data from the parent GraphCanvas provider.
13 : *
14 : * @param nodeLocatorIdMaybe - Locator ID (root or subgraph scoped) of the node to track
15 : * @returns Object containing reactive execution state and progress
16 : */
17 2355 : export const useNodeExecutionState = (
18 : nodeLocatorIdMaybe: MaybeRefOrGetter<NodeLocatorId | undefined>
19 : ) => {
20 8015 : const locatorId = computed(() => toValue(nodeLocatorIdMaybe) ?? '')
21 8015 : const { nodeLocationProgressStates, isIdle } =
22 : storeToRefs(useExecutionStore())
23 :
24 8015 : const progressState = computed(() => {
25 379 : const id = locatorId.value
26 379 : return id ? nodeLocationProgressStates.value[id] : undefined
27 : })
28 :
29 8015 : const executing = computed(
30 : () => !isIdle.value && progressState.value?.state === 'running'
31 : )
32 :
33 8015 : const progress = computed(() => {
34 46 : const state = progressState.value
35 46 : return state && state.max > 0 ? state.value / state.max : undefined
36 : })
37 :
38 : const progressPercentage = computed(() => {
39 0 : const prog = progress.value
40 0 : return prog !== undefined ? Math.round(prog * 100) : undefined
41 : })
42 :
43 : const executionState = computed(() => {
44 0 : const state = progressState.value
45 0 : if (!state) return 'idle'
46 0 : return state.state
47 : })
48 :
49 8015 : return {
50 : executing,
51 : progress,
52 : progressPercentage,
53 : progressState,
54 : executionState
55 : }
56 : }
|