import { Box } from '../../classes/display/box.js'; import { Scene } from '../../classes/display/scene.js'; import { Actor } from '../../classes/display/actor.js'; const rootElement = document.getElementById('basic'); const rootBox = new Box('rootBox', rootElement).flex(); function delay(min, max = min) { const delayMs = min + Math.random() * (max - min); return new Promise((resolve) => { setTimeout(resolve, delayMs); }); } (async function run() { const scene = new Scene('Scene 2', rootBox).withSequenceDiagram(); const webClient = new Actor('webClient', scene); const nodes = []; const memories = []; const storages = []; async function addNode() { const idx = nodes.length; const node = new Actor(`node${idx}`, scene); const memory = new Actor(`memory${idx}`, scene); const storage = new Actor(`storage${idx}`, scene); node.memory = memory; node.storage = storage; nodes.push(node); memories.push(memory); storages.push(storage); return node; } function getPeer(node) { const peers = nodes.filter((peer) => peer !== node); const idx = Math.floor(Math.random() * peers.length); return peers[idx]; } await addNode(); await addNode(); const [ seekTruth, considerInfo, evaluateConfidence, chooseResponse, qualifiedOpinions, requestMemoryData, memoryData, requestStorageData, storageData, ] = [ 'seek truth', 'consider available information', 'evaluate confidence', 'choose response', 'qualified opinions', 'request in-memory data', 'in-memory data', 'request storage data', 'storage data', ].map((name) => scene.addAction(name)); memories.forEach((memory) => { memory.setStatus('Idle'); memory.on(requestMemoryData, async (src, _detail) => { memory.setStatus('Retrieving data'); await delay(1000); memory.send(src, memoryData, {}); memory.setStatus('Idle'); }); }); storages.forEach((storage) => { storage.setStatus('Idle'); storage.on(requestStorageData, async (src, _detail) => { storage.setStatus('Retrieving data'); await delay(1000); storage.send(src, storageData, {}); storage.setStatus('Idle'); }); }); nodes.forEach((node) => { node.setStatus('Idle'); node.on(seekTruth, async (seeker, detail) => { node.setStatus('Processing request'); node.on(chooseResponse, async (_src, _info) => { node.setStatus('Choosing response'); await delay(1000); node.send(seeker, qualifiedOpinions, {}); node.setStatus('Idle'); }); node.on(evaluateConfidence, async (_src, _info) => { node.setStatus('Evaluating confidence'); await delay(1000); node.send(node, chooseResponse); }); node.on(considerInfo, async (_src, _info) => { node.setStatus('Considering info'); await delay(1000); node.send(node, evaluateConfidence); }); node.on(memoryData, (_src, _data) => { node.on(storageData, (__src, __data) => { if (detail?.readConcern === 'single') { node.send(node, considerInfo, {}); } else { const peer = getPeer(node); node.on(qualifiedOpinions, (___src, info) => { node.send(node, considerInfo, info); }); node.send(peer, seekTruth, { readConcern: 'single' }); } }); node.send(node.storage, requestStorageData); }); await delay(1000); node.send(node.memory, requestMemoryData); }); }); webClient.on(qualifiedOpinions, (_src, _detail) => { webClient.setStatus('Received opinions and qualifications'); }); await delay(1000); webClient.setStatus('Seek truth'); webClient.send(nodes[0], seekTruth); }());