Skip to content

Commit 24d853c

Browse files
committed
observe variables now returns a promise and set-variables waits for it before continueing
1 parent 9b2c3d1 commit 24d853c

File tree

3 files changed

+109
-80
lines changed

3 files changed

+109
-80
lines changed

libs/visual-programming-system/src/canvas-app/flow-core.ts

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export interface IFlowCore {
7171
scopeId?: string,
7272
runCounter?: any,
7373
isInitializing?: boolean
74-
) => boolean;
74+
) => Promise<boolean>;
7575

7676
getVariables: (scopeId?: string) => Record<string, any>;
7777

@@ -88,7 +88,7 @@ export interface IFlowCore {
8888
observeVariable: (
8989
nodeId: string,
9090
variableName: string,
91-
updated: (data: any, runCounter?: any) => void
91+
updated: (data: any, runCounter?: any) => Promise<void>
9292
) => void;
9393

9494
removeObserveVariable: (nodeId: string, variableName: string) => void;
@@ -138,7 +138,7 @@ export class FlowCore implements IFlowCore {
138138
> = {};
139139
protected variableObservers: Map<
140140
string,
141-
Map<string, (data: any, runCounter?: any) => void>
141+
Map<string, (data: any, runCounter?: any) => Promise<void>>
142142
> = new Map();
143143
protected commandHandlers: Record<string, ICommandHandler> = {};
144144
protected nodeSetStateHandlers: Record<string, SetNodeStatedHandler> = {};
@@ -262,28 +262,40 @@ export class FlowCore implements IFlowCore {
262262
runCounter?: any,
263263
isInitializing?: boolean
264264
) => {
265-
if (scopeId && this.tempVariables[scopeId][variableName]) {
266-
this.tempVariables[scopeId][variableName] = data;
267-
} else if (variableName && this.variables[variableName]) {
268-
const result = this.variables[variableName].setData(data, scopeId);
269-
if (!result) {
270-
return false;
271-
}
272-
if (isInitializing) {
273-
return true;
274-
}
275-
const dataToObserver = this.variables[variableName].getData(
276-
undefined,
277-
scopeId
278-
);
279-
const map = this.variableObservers.get(`${variableName}`);
280-
if (map) {
281-
map.forEach((observer) => {
282-
observer(dataToObserver, runCounter);
283-
});
265+
return new Promise<boolean>((resolve) => {
266+
if (scopeId && this.tempVariables[scopeId][variableName]) {
267+
this.tempVariables[scopeId][variableName] = data;
268+
} else if (variableName && this.variables[variableName]) {
269+
const result = this.variables[variableName].setData(data, scopeId);
270+
if (!result) {
271+
resolve(false);
272+
return;
273+
}
274+
if (isInitializing) {
275+
resolve(true);
276+
return;
277+
}
278+
const dataToObserver = this.variables[variableName].getData(
279+
undefined,
280+
scopeId
281+
);
282+
const map = this.variableObservers.get(`${variableName}`);
283+
if (map) {
284+
const observePromises: Promise<void>[] = [];
285+
map.forEach((observer) => {
286+
observePromises.push(
287+
observer(dataToObserver, runCounter) as unknown as Promise<void>
288+
);
289+
});
290+
Promise.all(observePromises).then(() => {
291+
resolve(true);
292+
});
293+
294+
return;
295+
}
284296
}
285-
}
286-
return true;
297+
resolve(true);
298+
});
287299
};
288300

289301
getVariables = (scopeId?: string) => {
@@ -326,7 +338,7 @@ export class FlowCore implements IFlowCore {
326338
observeVariable = (
327339
nodeId: string,
328340
variableName: string,
329-
updated: (data: any, runCounter?: any) => void
341+
updated: (data: any, runCounter?: any) => Promise<void>
330342
) => {
331343
let map = this.variableObservers.get(`${variableName}`);
332344
if (!map) {

libs/web-flow-executor/src/nodes/observe-variable.ts

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -79,47 +79,49 @@ export const observeVariable = (updated: () => void): NodeTask<NodeInfo> => {
7979
node.id,
8080
variableName,
8181
(value, runCounter?: any) => {
82-
if (timeout) {
83-
clearTimeout(timeout);
84-
timeout = undefined;
85-
}
86-
if (componentWrapper && componentWrapper?.domElement) {
87-
(componentWrapper.domElement as HTMLElement).classList.remove(
88-
'border-transparent'
89-
);
90-
}
91-
if (componentWrapper && componentWrapper?.domElement) {
92-
(componentWrapper.domElement as HTMLElement).classList.add(
93-
'border-white'
94-
);
95-
}
96-
timeout = setTimeout(() => {
82+
return new Promise<void>((resolve) => {
83+
if (timeout) {
84+
clearTimeout(timeout);
85+
timeout = undefined;
86+
}
9787
if (componentWrapper && componentWrapper?.domElement) {
9888
(componentWrapper.domElement as HTMLElement).classList.remove(
99-
'border-white'
89+
'border-transparent'
10090
);
91+
}
92+
if (componentWrapper && componentWrapper?.domElement) {
10193
(componentWrapper.domElement as HTMLElement).classList.add(
102-
'border-transparent'
94+
'border-white'
10395
);
10496
}
105-
}, 250);
97+
timeout = setTimeout(() => {
98+
if (componentWrapper && componentWrapper?.domElement) {
99+
(componentWrapper.domElement as HTMLElement).classList.remove(
100+
'border-white'
101+
);
102+
(componentWrapper.domElement as HTMLElement).classList.add(
103+
'border-transparent'
104+
);
105+
}
106+
}, 250);
106107

107-
if (canvasAppInstance) {
108-
runNode(
109-
node,
110-
canvasAppInstance,
111-
(_input) => {
112-
//
113-
},
114-
value,
115-
undefined,
116-
undefined,
117-
undefined,
118-
undefined,
119-
undefined,
120-
runCounter
121-
);
122-
}
108+
if (canvasAppInstance) {
109+
runNode(
110+
node,
111+
canvasAppInstance,
112+
(_input) => {
113+
resolve();
114+
},
115+
value,
116+
undefined,
117+
undefined,
118+
undefined,
119+
undefined,
120+
undefined,
121+
runCounter
122+
);
123+
}
124+
});
123125
}
124126
);
125127
}

libs/web-flow-executor/src/nodes/set-variable.ts

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
ThumbConnectionType,
1010
ThumbType,
1111
visualNodeFactory,
12+
IComputeResult,
1213
} from '@devhelpr/visual-programming-system';
1314
import { NodeInfo } from '../types/node-info';
1415
import { RunCounter } from '../follow-path/run-counter';
@@ -25,34 +26,46 @@ export const setVariable: NodeTaskFactory<NodeInfo> = (
2526
const initializeCompute = () => {
2627
return;
2728
};
28-
const compute = (
29+
const computeAsync = (
2930
input: string,
3031
_loopIndex?: number,
3132
_payload?: any,
3233
_thumbName?: string,
3334
scopeId?: string,
3435
runCounter?: RunCounter
3536
) => {
36-
if (contextInstance) {
37-
const variableName = node?.nodeInfo?.formValues?.[fieldName] ?? '';
38-
if (variableName) {
39-
if (
40-
!contextInstance.setVariable(variableName, input, scopeId, runCounter)
41-
) {
42-
return {
43-
result: input,
44-
output: input,
45-
followPath: undefined,
46-
stop: true,
47-
};
37+
return new Promise<IComputeResult>((resolve) => {
38+
if (contextInstance) {
39+
const variableName = node?.nodeInfo?.formValues?.[fieldName] ?? '';
40+
if (variableName) {
41+
contextInstance
42+
.setVariable(variableName, input, scopeId, runCounter)
43+
.then((result) => {
44+
if (result) {
45+
resolve({
46+
result: input,
47+
output: input,
48+
followPath: undefined,
49+
});
50+
} else {
51+
resolve({
52+
result: input,
53+
output: input,
54+
followPath: undefined,
55+
stop: true,
56+
});
57+
}
58+
});
59+
60+
return;
4861
}
4962
}
50-
}
51-
return {
52-
result: input,
53-
output: input,
54-
followPath: undefined,
55-
};
63+
resolve({
64+
result: input,
65+
output: input,
66+
followPath: undefined,
67+
});
68+
});
5669
};
5770

5871
const getDependencies = (): { startNodeId: string; endNodeId: string }[] => {
@@ -75,7 +88,7 @@ export const setVariable: NodeTaskFactory<NodeInfo> = (
7588
'Set variable',
7689
'flow-canvas',
7790
'variableName',
78-
compute,
91+
computeAsync,
7992
initializeCompute,
8093
false,
8194
200,
@@ -133,6 +146,8 @@ export const setVariable: NodeTaskFactory<NodeInfo> = (
133146
category: 'variables',
134147
backgroundColorClassName:
135148
theme?.referenceVariableBackgroundColorClassName,
136-
}
149+
},
150+
undefined,
151+
true
137152
);
138153
};

0 commit comments

Comments
 (0)