Bladeren bron

client: handle new query before old one is finished

master
Milan Pässler 1 jaar geleden
bovenliggende
commit
eded0b0bcd
1 gewijzigde bestanden met toevoegingen van 16 en 12 verwijderingen
  1. +16
    -12
      client/src/index.js

+ 16
- 12
client/src/index.js Bestand weergeven

@@ -22,6 +22,7 @@ const graph = {
links: []
};
let queued = 0;
let queryNum = 0;
let simulation = d3Force.forceSimulation();
let linkForce = d3Force.forceLink([]).id(d => String(d.hash));
const canvas = document.getElementById('canvas');
@@ -45,8 +46,8 @@ function drawGraph() {
ctx.strokeStyle = "rgba(0, 50, 255, .8)";
ctx.lineWidth = 5;
} else {
ctx.strokeStyle = "rgba(255, 255, 255, .3)";
ctx.lineWidth = 3;
ctx.strokeStyle = "rgba(255, 255, 255, .2)";
ctx.lineWidth = 2;
}
ctx.beginPath();
ctx.moveTo(x(link.source.x), y(link.source.y));
@@ -87,9 +88,8 @@ function addNode(node) {
node.references.forEach(async (link) => {
let neighbour;
try {
neighbour = await fetchHash(link.hash);
neighbour = await fetchNode("hash", link.hash);
} catch(err) {
console.log(node);
return;
}
neighbour.weight = 5;
@@ -110,15 +110,17 @@ function addNode(node) {
updateQueued(queued-1);
}

function fetchName(name) {
async function fetchNode(type, value) {
updateQueued(queued+1);
return fetch(`http://127.0.0.1:8080/name/${name}`)
.then(resp => resp.json());
}
function fetchHash(hash) {
updateQueued(queued+1);
return fetch(`http://127.0.0.1:8080/hash/${hash}`)
const queryNumAtStart = queryNum;

const node = await fetch(`http://127.0.0.1:8080/${type}/${value}`)
.then(resp => resp.json());

if (queryNum !== queryNumAtStart)
throw new Error("");

return node;
}

function init() {
@@ -130,10 +132,12 @@ function init() {

async function go() {
document.getElementById("derivation").blur();
queryNum++;
queued = 0;
graph.nodes = {};
graph.links = [];
const name = document.getElementById("derivation").value;
const node = await fetchName(name);
const node = await fetchNode("name", name);
node.weight = 10;
addNode(node);
view.selectedNode = node;


Laden…
Annuleren
Opslaan