11// assumption is that apis are giving json as output
22
3- import { cloneDeep } from 'lodash' ;
43import useCanvasStore from 'stores/CanvasStore' ;
5- import useCollectionStore from 'stores/CollectionStore' ;
6- import { useTabStore } from 'stores/TabStore' ;
74import { computeAuthNode } from './compute/authnode' ;
85import { computeEvaluateNode } from './compute/evaluatenode' ;
96import { computeRequestNode } from './compute/requestnode' ;
107
118class Graph {
12- constructor ( nodes , edges , collectionId , onGraphComplete ) {
13- const activeEnv = useCollectionStore
14- . getState ( )
15- . collections . find ( ( c ) => c . id === collectionId )
16- ?. environments . find ( ( e ) => e . name === useTabStore . getState ( ) . selectedEnv ) ;
9+ constructor ( nodes , edges , startTime , initialEnvVars , initialLogs ) {
1710 this . nodes = nodes ;
1811 this . edges = edges ;
19- this . onGraphComplete = onGraphComplete ;
20- this . logs = [ ] ;
21- this . timeout = 60000 ; // 1m timeout
22- this . startTime = Date . now ( ) ;
12+ this . logs = initialLogs ;
13+ this . timeout = 60000 ; //ms
14+ this . startTime = startTime ;
2315 this . graphRunNodeOutput = { } ;
2416 this . auth = undefined ;
25- this . envVariables = activeEnv ? cloneDeep ( activeEnv . variables ) : undefined ;
17+ this . envVariables = initialEnvVars ;
2618 }
2719
2820 #checkTimeout( ) {
@@ -33,13 +25,13 @@ class Graph {
3325 let connectingEdge = undefined ;
3426
3527 if ( node . type === 'evaluateNode' ) {
36- if ( result [ 3 ] === true ) {
28+ if ( result . output === true ) {
3729 connectingEdge = this . edges . find ( ( edge ) => edge . sourceHandle == 'true' && edge . source === node . id ) ;
3830 } else {
3931 connectingEdge = this . edges . find ( ( edge ) => edge . sourceHandle == 'false' && edge . source === node . id ) ;
4032 }
4133 } else {
42- if ( result [ 0 ] === 'Success' ) {
34+ if ( result . status === 'Success' ) {
4335 connectingEdge = this . edges . find ( ( edge ) => edge . source === node . id ) ;
4436 }
4537 }
@@ -63,7 +55,7 @@ class Graph {
6355 return prevNodesData ;
6456 }
6557
66- async #computeNode( node , prevNodeOutput ) {
58+ async #computeNode( node ) {
6759 let result = undefined ;
6860 const prevNodeOutputData = this . #computeDataFromPreviousNodes( node ) ;
6961
@@ -73,16 +65,29 @@ class Graph {
7365 if ( node . type === 'outputNode' ) {
7466 this . logs . push ( `Output: ${ JSON . stringify ( prevNodeOutputData ) } ` ) ;
7567 useCanvasStore . getState ( ) . setOutputNode ( node . id , prevNodeOutputData ) ;
76- result = [ 'Success' , node , prevNodeOutput ] ;
68+ result = {
69+ status : 'Success' ,
70+ node,
71+ } ;
7772 }
7873
7974 if ( node . type === 'evaluateNode' ) {
8075 if ( computeEvaluateNode ( node , prevNodeOutputData , this . logs ) ) {
8176 this . logs . push ( 'Result: true' ) ;
82- result = [ 'Success' , node , prevNodeOutput , true ] ;
77+ result = {
78+ status : 'Success' ,
79+ node,
80+ output : true ,
81+ } ;
82+ //result = ['Success', node, prevNodeOutput, true];
8383 } else {
8484 this . logs . push ( 'Result: false' ) ;
85- result = [ 'Success' , node , prevNodeOutput , false ] ;
85+ result = {
86+ status : 'Success' ,
87+ node,
88+ output : true ,
89+ } ;
90+ //result = ['Success', node, prevNodeOutput, false];
8691 }
8792 }
8893
@@ -93,21 +98,27 @@ class Graph {
9398 } ;
9499 await wait ( delay ) ;
95100 this . logs . push ( `Wait for: ${ delay } ms` ) ;
96- result = [ 'Success' , node , prevNodeOutput ] ;
101+ result = {
102+ status : 'Success' ,
103+ node,
104+ } ;
97105 }
98106
99107 if ( node . type === 'authNode' ) {
100108 this . auth = node . data . type ? computeAuthNode ( node . data , this . envVariables ) : undefined ;
101- result = [ 'Success' , node , prevNodeOutput ] ;
109+ result = {
110+ status : 'Success' ,
111+ node,
112+ } ;
102113 }
103114
104115 if ( node . type === 'requestNode' ) {
105116 result = await computeRequestNode ( node , prevNodeOutputData , this . envVariables , this . auth , this . logs ) ;
106117 // add post response variables if any
107- if ( result [ 3 ] ) {
118+ if ( result . postRespVars ) {
108119 this . envVariables = {
109120 ...this . envVariables ,
110- ...result [ 3 ] ,
121+ ...result . postRespVars ,
111122 } ;
112123 }
113124 }
@@ -117,12 +128,18 @@ class Graph {
117128 }
118129 } catch ( err ) {
119130 this . logs . push ( `Flow failed at: ${ JSON . stringify ( node ) } due to ${ err } ` ) ;
120- return [ 'Failed' , node ] ;
131+ return {
132+ status : 'Failed' ,
133+ node,
134+ } ;
121135 }
122136
123137 if ( result === undefined ) {
124138 this . logs . push ( `Flow failed at: ${ JSON . stringify ( node ) } ` ) ;
125- return [ 'Failed' , node ] ;
139+ return {
140+ status : 'Failed' ,
141+ node,
142+ } ;
126143 } else {
127144 const connectingEdge = this . #computeConnectingEdge( node , result ) ;
128145
@@ -132,15 +149,15 @@ class Graph {
132149 [ 'requestNode' , 'outputNode' , 'evaluateNode' , 'delayNode' , 'authNode' ] . includes ( node . type ) &&
133150 node . id === connectingEdge . target ,
134151 ) ;
135- this . graphRunNodeOutput [ node . id ] = result [ 2 ] && result [ 2 ] . data ? result [ 2 ] . data : { } ;
136- return this . #computeNode( nextNode , result [ 2 ] ) ;
152+ this . graphRunNodeOutput [ node . id ] = result . data ? result . data : { } ;
153+ return this . #computeNode( nextNode ) ;
137154 } else {
138155 return result ;
139156 }
140157 }
141158 }
142159
143- run ( ) {
160+ async run ( ) {
144161 // reset every output node for a fresh run
145162 this . nodes . forEach ( ( node ) => {
146163 if ( node . type === 'outputNode' ) {
@@ -154,26 +171,35 @@ class Graph {
154171 if ( startNode == undefined ) {
155172 this . logs . push ( 'No start node found' ) ;
156173 this . logs . push ( 'End Flowtest' ) ;
157- return ;
174+ return {
175+ status : 'Success' ,
176+ logs : this . logs ,
177+ envVars : this . envVariables ,
178+ } ;
158179 }
159180 const connectingEdge = this . edges . find ( ( edge ) => edge . source === startNode . id ) ;
160181
161182 // only start computing graph if initial node has the connecting edge
162183 if ( connectingEdge != undefined ) {
163184 const firstNode = this . nodes . find ( ( node ) => node . id === connectingEdge . target ) ;
164- this . #computeNode( firstNode , undefined ) . then ( ( result ) => {
165- if ( result [ 0 ] == 'Failed' ) {
166- console . debug ( 'Flow failed at: ' , result [ 1 ] ) ;
167- }
168- this . logs . push ( 'End Flowtest' ) ;
169- this . logs . push ( `Total time: ${ Date . now ( ) - this . startTime } ms` ) ;
170- console . log ( this . logs ) ;
171- this . onGraphComplete ( result , this . logs ) ;
172- } ) ;
185+ const result = await this . #computeNode( firstNode ) ;
186+ if ( result . status == 'Failed' ) {
187+ console . debug ( 'Flow failed at: ' , result . node ) ;
188+ }
189+ this . logs . push ( 'End Flowtest' ) ;
190+ this . logs . push ( `Total time: ${ Date . now ( ) - this . startTime } ms` ) ;
191+ return {
192+ status : result . status ,
193+ logs : this . logs ,
194+ envVars : this . envVariables ,
195+ } ;
173196 } else {
174- this . logs . push ( 'No connected request node to start node' ) ;
175197 this . logs . push ( 'End Flowtest' ) ;
176- this . onGraphComplete ( [ 'Success' ] , this . logs ) ;
198+ return {
199+ status : 'Success' ,
200+ logs : this . logs ,
201+ envVars : this . envVariables ,
202+ } ;
177203 }
178204 }
179205}
0 commit comments