🐛 better running calc
This commit is contained in:
33
src/graph.js
33
src/graph.js
@@ -20,22 +20,26 @@ export default class Graph {
|
||||
|
||||
static async deserialize(nodes) {
|
||||
const graph = new Graph();
|
||||
graph.nodes = await createNodes(nodes);
|
||||
for (let {node} of graph.nodes) {
|
||||
const isRunningSet = new WeakSet();
|
||||
graph.nodes = await createUnconnectedNodes(nodes);
|
||||
for (const {node} of graph.nodes) {
|
||||
node.subject.on('running', (isRunning) => {
|
||||
graph.__nodesActive += isRunning ? 1 : -1;
|
||||
clearTimeout(graph.__nodesActiveDebounce);
|
||||
graph.__nodesActiveDebounce = setTimeout(() => {
|
||||
if (!isRunning && isRunningSet.has(node)) {
|
||||
isRunningSet.delete(node);
|
||||
graph.__nodesActive -= 1;
|
||||
} else if (isRunning && !isRunningSet.has(node)) {
|
||||
isRunningSet.add(node);
|
||||
graph.__nodesActive += 1;
|
||||
}
|
||||
if (graph.__nodesActive > 0) {
|
||||
console.log('running');
|
||||
graph.subject.next('running');
|
||||
} else {
|
||||
console.log('stopped');
|
||||
console.info('[Graphfx] graph stopped');
|
||||
graph.subject.next('stopped');
|
||||
}
|
||||
}, 16);
|
||||
});
|
||||
}
|
||||
graph.nodes = await reconnectNodes(graph.nodes);
|
||||
return graph;
|
||||
}
|
||||
|
||||
@@ -77,8 +81,8 @@ export const createNode = async ({name, options, id}) => {
|
||||
}
|
||||
}
|
||||
|
||||
export const createNodes = async (nodes) => {
|
||||
nodes = (await Promise.all(nodes.map(async ({node, x, y}) => {
|
||||
export const createUnconnectedNodes = async (nodes) => {
|
||||
return (await Promise.all(nodes.map(async ({node, x, y}) => {
|
||||
const nodeInstance = await createNode(node);
|
||||
return ({
|
||||
node: nodeInstance,
|
||||
@@ -92,7 +96,9 @@ export const createNode = async ({name, options, id}) => {
|
||||
}
|
||||
});
|
||||
}))).filter(({node}) => node);
|
||||
}
|
||||
|
||||
export const reconnectNodes = async (nodes) => {
|
||||
const outputsById = nodes
|
||||
.map(({node}) => Object.values(node.out.__values))
|
||||
.reduce((acc, nxt) => acc.concat(nxt), [])
|
||||
@@ -105,4 +111,9 @@ export const createNode = async ({name, options, id}) => {
|
||||
await connect(outputsById);
|
||||
return {node,x,y};
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
export const createNodes = async (nodes) => {
|
||||
nodes = await createUnconnectedNodes(nodes);
|
||||
return reconnectNodes(nodes);
|
||||
}
|
||||
|
||||
@@ -110,5 +110,9 @@ export default class Node {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
toString() {
|
||||
return `${this.name}<${this.id}>`;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user