🐛 better running calc

This commit is contained in:
2019-11-14 13:35:40 +01:00
parent cc9252aba3
commit a8d76d43c8
2 changed files with 54 additions and 39 deletions

View File

@@ -20,22 +20,26 @@ export default class Graph {
static async deserialize(nodes) { static async deserialize(nodes) {
const graph = new Graph(); const graph = new Graph();
graph.nodes = await createNodes(nodes); const isRunningSet = new WeakSet();
for (let {node} of graph.nodes) { graph.nodes = await createUnconnectedNodes(nodes);
for (const {node} of graph.nodes) {
node.subject.on('running', (isRunning) => { node.subject.on('running', (isRunning) => {
graph.__nodesActive += isRunning ? 1 : -1; if (!isRunning && isRunningSet.has(node)) {
clearTimeout(graph.__nodesActiveDebounce); isRunningSet.delete(node);
graph.__nodesActiveDebounce = setTimeout(() => { graph.__nodesActive -= 1;
if (graph.__nodesActive > 0) { } else if (isRunning && !isRunningSet.has(node)) {
console.log('running'); isRunningSet.add(node);
graph.subject.next('running'); graph.__nodesActive += 1;
} else { }
console.log('stopped'); if (graph.__nodesActive > 0) {
graph.subject.next('stopped'); graph.subject.next('running');
} } else {
}, 16); console.info('[Graphfx] graph stopped');
graph.subject.next('stopped');
}
}); });
} }
graph.nodes = await reconnectNodes(graph.nodes);
return graph; return graph;
} }
@@ -77,32 +81,39 @@ export const createNode = async ({name, options, id}) => {
} }
} }
export const createNodes = async (nodes) => { export const createUnconnectedNodes = async (nodes) => {
nodes = (await Promise.all(nodes.map(async ({node, x, y}) => { return (await Promise.all(nodes.map(async ({node, x, y}) => {
const nodeInstance = await createNode(node); const nodeInstance = await createNode(node);
return ({ return ({
node: nodeInstance, node: nodeInstance,
x, y, x, y,
async connect(outputs) { async connect(outputs) {
try { try {
await nodeInstance.reconnect(node, outputs); await nodeInstance.reconnect(node, outputs);
} catch(err) { } catch(err) {
console.error('Can\'t recoonect', nodeInstance, err); console.error('Can\'t recoonect', nodeInstance, err);
}
} }
}); }
}))).filter(({node}) => node); });
}))).filter(({node}) => node);
}
const outputsById = nodes export const reconnectNodes = async (nodes) => {
.map(({node}) => Object.values(node.out.__values)) const outputsById = nodes
.reduce((acc, nxt) => acc.concat(nxt), []) .map(({node}) => Object.values(node.out.__values))
.reduce((acc, nxt) => { .reduce((acc, nxt) => acc.concat(nxt), [])
acc[nxt.id] = nxt; .reduce((acc, nxt) => {
return acc; acc[nxt.id] = nxt;
}, {}); return acc;
}, {});
return await Promise.all(nodes.map(async ({node, x, y, connect}) => { return await Promise.all(nodes.map(async ({node, x, y, connect}) => {
await connect(outputsById); await connect(outputsById);
return {node,x,y}; return {node,x,y};
})) }))
} }
export const createNodes = async (nodes) => {
nodes = await createUnconnectedNodes(nodes);
return reconnectNodes(nodes);
}

View File

@@ -110,5 +110,9 @@ export default class Node {
} }
}) })
} }
toString() {
return `${this.name}<${this.id}>`;
}
}; };