diff --git a/web-client/example/index.html b/web-client/example/index.html index 57869d79d1..9b20e2ddea 100644 --- a/web-client/example/index.html +++ b/web-client/example/index.html @@ -7,8 +7,24 @@ - -
+
+ Consensus: connecting +
+ +
+ Peers: 0 +
+ +
+ Block: - +
+ +
+
+ Main-thread unblocked: + +
+
diff --git a/web-client/example/module.js b/web-client/example/module.js index febb736579..e6731ce212 100644 --- a/web-client/example/module.js +++ b/web-client/example/module.js @@ -9,27 +9,29 @@ init().then(async () => { const client = await Nimiq.Client.create(config.build()); window.client = client; // Prevent garbage collection and for playing around - client.addConsensusChangedListener( - (state) => { - console.log(`Consensus ${state.toUpperCase()}`); - }, - ); - - client.addHeadChangedListener( - async (hash, reason, revertedBlocks, adoptedBlocks) => { - const block = await client.getBlock(hash); - const rebranchLength = revertedBlocks.length; - - console.log([ - 'Blockchain:', - reason, - ...(rebranchLength ? [rebranchLength] : []), - 'at', - block.height, - `(${new Date(block.timestamp).toISOString().substring(0, 19).replace('T', ' ')} UTC)` - ].join(' ')); - }, - ); + client.addConsensusChangedListener((state) => { + console.log(`Consensus ${state.toUpperCase()}`); + document.querySelector('#consensus').textContent = state; + document.querySelector('#consensus').classList.toggle('connecting', state === 'connecting'); + document.querySelector('#consensus').classList.toggle('syncing', state === 'syncing'); + document.querySelector('#consensus').classList.toggle('established', state === 'established'); + }); + + client.addHeadChangedListener(async (hash, reason, revertedBlocks, adoptedBlocks) => { + const block = await client.getBlock(hash); + const rebranchLength = revertedBlocks.length; + + console.log([ + 'Blockchain:', + reason, + ...(rebranchLength ? [rebranchLength] : []), + 'at', + block.height, + `(${new Date(block.timestamp).toISOString().substring(0, 19).replace('T', ' ')} UTC)` + ].join(' ')); + + document.querySelector('#block').textContent = block.height; + }); client.addPeerChangedListener((peerId, reason, numPeers, peerInfo) => { if (peerInfo) { @@ -38,6 +40,8 @@ init().then(async () => { } else { console.log(`Peer ${reason}: ${peerId} - now ${numPeers} peers connected`); } + + document.querySelector('#peers').textContent = numPeers; }); /**