tweak: Asynchronous refactoring
This commit is contained in:
parent
b99aa05318
commit
ed59faafe1
|
@ -1,5 +1,5 @@
|
||||||
/* Imports*/
|
/* Imports*/
|
||||||
import React from "react";
|
import React, { useState, useEffect } from 'react';
|
||||||
import { toLocale, truncateValueForDisplay, numToAlphabeticalString, formatLargeOrSmall } from "../lib/utils.js"
|
import { toLocale, truncateValueForDisplay, numToAlphabeticalString, formatLargeOrSmall } from "../lib/utils.js"
|
||||||
|
|
||||||
/* Utilities */
|
/* Utilities */
|
||||||
|
@ -7,14 +7,13 @@ let avg = arr => arr.reduce((a, b) => (a + b), 0) / arr.length
|
||||||
|
|
||||||
/* Main function */
|
/* Main function */
|
||||||
|
|
||||||
function findPathsInner({
|
async function findPathsInner({
|
||||||
sourceElementId, sourceElementPosition,
|
sourceElementId, sourceElementPosition,
|
||||||
targetElementId, targetElementPosition,
|
targetElementId, targetElementPosition,
|
||||||
links, nodes,
|
links, nodes,
|
||||||
maxLengthOfPath, pathSoFar
|
maxLengthOfPath, pathSoFar
|
||||||
}) {
|
}) {
|
||||||
let paths = []
|
let paths = []
|
||||||
|
|
||||||
let minPos = Math.min(sourceElementPosition, targetElementPosition)
|
let minPos = Math.min(sourceElementPosition, targetElementPosition)
|
||||||
let maxPos = Math.max(sourceElementPosition, targetElementPosition)
|
let maxPos = Math.max(sourceElementPosition, targetElementPosition)
|
||||||
let linksInner = links.filter(link =>
|
let linksInner = links.filter(link =>
|
||||||
|
@ -30,7 +29,7 @@ function findPathsInner({
|
||||||
) {
|
) {
|
||||||
paths.push(pathSoFar.concat(link).flat())
|
paths.push(pathSoFar.concat(link).flat())
|
||||||
} else if ((link.source == sourceElementId)) {
|
} else if ((link.source == sourceElementId)) {
|
||||||
let newPaths = findPathsInner({
|
let newPaths = await findPathsInner({
|
||||||
sourceElementId: link.target, sourceElementPosition: link.sourceElementPosition,
|
sourceElementId: link.target, sourceElementPosition: link.sourceElementPosition,
|
||||||
targetElementId, targetElementPosition,
|
targetElementId, targetElementPosition,
|
||||||
pathSoFar: pathSoFar.concat(link).flat(),
|
pathSoFar: pathSoFar.concat(link).flat(),
|
||||||
|
@ -40,7 +39,7 @@ function findPathsInner({
|
||||||
paths.push(...newPaths)
|
paths.push(...newPaths)
|
||||||
}
|
}
|
||||||
} else if ((link.target == sourceElementId)) {
|
} else if ((link.target == sourceElementId)) {
|
||||||
let newPaths = findPathsInner({
|
let newPaths = await findPathsInner({
|
||||||
sourceElementId: link.source, sourceElementPosition: link.sourceElementPosition,
|
sourceElementId: link.source, sourceElementPosition: link.sourceElementPosition,
|
||||||
targetElementId, targetElementPosition,
|
targetElementId, targetElementPosition,
|
||||||
pathSoFar: pathSoFar.concat(link).flat(),
|
pathSoFar: pathSoFar.concat(link).flat(),
|
||||||
|
@ -72,13 +71,13 @@ function findPaths({
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function findDistance({
|
async function findDistance({
|
||||||
sourceElementId, sourceElementPosition,
|
sourceElementId, sourceElementPosition,
|
||||||
targetElementId, targetElementPosition,
|
targetElementId, targetElementPosition,
|
||||||
nodes, links, direction
|
nodes, links, direction
|
||||||
}) {
|
}) {
|
||||||
let maxLengthOfPath = Math.abs(sourceElementPosition - targetElementPosition)
|
let maxLengthOfPath = Math.abs(sourceElementPosition - targetElementPosition)
|
||||||
let paths = findPathsInner({
|
let paths = await findPathsInner({
|
||||||
sourceElementId, sourceElementPosition,
|
sourceElementId, sourceElementPosition,
|
||||||
targetElementId, targetElementPosition,
|
targetElementId, targetElementPosition,
|
||||||
links, nodes, direction,
|
links, nodes, direction,
|
||||||
|
@ -112,36 +111,7 @@ function findDistance({
|
||||||
//return weights.map(weight => Math.round(weight*100)/100)
|
//return weights.map(weight => Math.round(weight*100)/100)
|
||||||
}
|
}
|
||||||
|
|
||||||
function getDirectionalLinks({ nodes, links }) {
|
async function findDistancesForAllElements({ nodes, links }) {
|
||||||
console.log("getDirectionalLinks")
|
|
||||||
// direction: 1 for upwards, -1 for downwards
|
|
||||||
let upwardsLinks = []
|
|
||||||
let downwardsLinks = []
|
|
||||||
links.forEach(link => {
|
|
||||||
console.log(link)
|
|
||||||
let sourceElementId = link.source
|
|
||||||
let targetElementId = link.target
|
|
||||||
if (link.distance < 1) {
|
|
||||||
// We already deal with this case upstream, but whatever.
|
|
||||||
[sourceElementId, targetElementId] = [targetElementId, sourceElementId]
|
|
||||||
}
|
|
||||||
let sourceElementPosition = nodes.find(element => element.id == sourceElementId).position
|
|
||||||
let targetElementPosition = nodes.find(element => element.id == targetElementId).position
|
|
||||||
if (link.distance == 1) {
|
|
||||||
// If two elements are the same, then they belong to both upwards and downwards paths!!
|
|
||||||
upwardsLinks.push(link)
|
|
||||||
downwardsLinks.push(link)
|
|
||||||
} else if (sourceElementPosition < targetElementPosition) {
|
|
||||||
upwardsLinks.push(link)
|
|
||||||
} else {
|
|
||||||
downwardsLinks.push(link)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
console.log([upwardsLinks, downwardsLinks])
|
|
||||||
return [upwardsLinks, downwardsLinks]
|
|
||||||
}
|
|
||||||
|
|
||||||
function findDistancesForAllElements({ nodes, links }) {
|
|
||||||
let referenceElements = nodes.filter(x => x.isReferenceValue)
|
let referenceElements = nodes.filter(x => x.isReferenceValue)
|
||||||
let midpoint = Math.round(nodes.length / 2)
|
let midpoint = Math.round(nodes.length / 2)
|
||||||
let referenceElement = referenceElements.length > 0 ? referenceElements[0] : nodes[midpoint]
|
let referenceElement = referenceElements.length > 0 ? referenceElements[0] : nodes[midpoint]
|
||||||
|
@ -166,6 +136,7 @@ function findDistancesForAllElements({ nodes, links }) {
|
||||||
return distance
|
return distance
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
distances = await Promise.all(distances)
|
||||||
return distances
|
return distances
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -185,9 +156,13 @@ function abridgeArrayAndDisplay(array) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function CreateTableWithDistances({ isListOrdered, orderedList, listOfElements, links }) {
|
export function CreateTableWithDistances({ isListOrdered, orderedList, listOfElements, links }) {
|
||||||
if (!isListOrdered || orderedList.length < listOfElements.length) {
|
const [rows, setRows] = useState([])
|
||||||
return (<div>{""}</div>)
|
|
||||||
} else {
|
useEffect(async () => {
|
||||||
|
// https://stackoverflow.com/questions/57847626/using-async-await-inside-a-react-functional-component
|
||||||
|
// https://stackoverflow.com/questions/54936559/using-async-await-in-react-component
|
||||||
|
// https://reactjs.org/docs/hooks-effect.html#tip-optimizing-performance-by-skipping-effects
|
||||||
|
if (isListOrdered && ! (orderedList.length < listOfElements.length) && rows.length == 0) {
|
||||||
let nodes = []
|
let nodes = []
|
||||||
let positionDictionary=({})
|
let positionDictionary=({})
|
||||||
orderedList.forEach((id, pos) => {
|
orderedList.forEach((id, pos) => {
|
||||||
|
@ -200,15 +175,23 @@ export function CreateTableWithDistances({ isListOrdered, orderedList, listOfEle
|
||||||
sourceElementPosition: positionDictionary[link.source],
|
sourceElementPosition: positionDictionary[link.source],
|
||||||
targetElementPosition: positionDictionary[link.target]
|
targetElementPosition: positionDictionary[link.target]
|
||||||
}))
|
}))
|
||||||
/* Continue */
|
|
||||||
let distances = findDistancesForAllElements({ nodes, links })
|
let distances = await findDistancesForAllElements({ nodes, links })
|
||||||
let rows = nodes.map((element, i) => ({ id: numToAlphabeticalString(element.position), position: element.position, name: element.name, distances: distances[i] }))
|
setRows(nodes.map((element, i) => ({
|
||||||
|
id: numToAlphabeticalString(element.position),
|
||||||
|
position: element.position,
|
||||||
|
name: element.name,
|
||||||
|
distances: distances[i]
|
||||||
|
})))
|
||||||
console.log("rows@CreateTableWithDistances")
|
console.log("rows@CreateTableWithDistances")
|
||||||
console.log(rows)
|
console.log(rows)
|
||||||
|
}
|
||||||
|
}); // this useEffect should ideally only work when isListOrdered changes, but I haven't bothered to program that.
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<table className="">
|
<table className="">
|
||||||
<thead >
|
<thead>
|
||||||
<tr >
|
<tr >
|
||||||
<th >Id</th>
|
<th >Id</th>
|
||||||
<th> </th>
|
<th> </th>
|
||||||
|
@ -238,7 +221,6 @@ export function CreateTableWithDistances({ isListOrdered, orderedList, listOfEle
|
||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user