-
-
Notifications
You must be signed in to change notification settings - Fork 345
Expand file tree
/
Copy pathuseAutomaticFeatures.ts
More file actions
32 lines (27 loc) · 1.4 KB
/
useAutomaticFeatures.ts
File metadata and controls
32 lines (27 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { getIncomers, useReactFlow } from 'reactflow';
import { useContext } from 'use-context-selector';
import { EdgeData, NodeData, SchemaId } from '../../common/common-types';
import { BackendContext } from '../contexts/BackendContext';
/**
* Determines whether a node should use automatic ahead-of-time features, such as individually running the node or determining certain type features automatically.
*/
export const useAutomaticFeatures = (id: string, schemaId: SchemaId) => {
const { schemata } = useContext(BackendContext);
const schema = schemata.get(schemaId);
const { getEdges, getNodes, getNode } = useReactFlow<NodeData, EdgeData>();
const thisNode = getNode(id);
// A node should not use automatic features if it has incoming connections
const hasIncomingConnections =
thisNode && getIncomers(thisNode, getNodes(), getEdges()).length > 0;
// If the node is a generator, it should not use automatic features
const isGenerator = schema.kind === 'generator';
// Same if it has any static input values
const hasStaticValueInput = schema.inputs.some((i) => i.kind === 'static');
// We should only use automatic features if the node has side effects
const { hasSideEffects } = schema;
return {
isAutomatic:
hasSideEffects && !hasIncomingConnections && !isGenerator && !hasStaticValueInput,
hasIncomingConnections,
};
};