Skip to content

Commit

Permalink
Initialize Nimiq libary on page load
Browse files Browse the repository at this point in the history
Instead of only when selecting a client type. This way one can already use the global `Nimiq` object without connecting to the network.
  • Loading branch information
sisou committed Jul 25, 2024
1 parent ba08270 commit 44d70ef
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,23 @@
document.getElementById('peers').innerText = peerCount;
}

const nimiqInitPromise = new Promise((resolve, reject) => Nimiq.init(resolve, (code) => {
switch (code) {
case Nimiq.ERR_WAIT:
alert('Error: Already open in another tab or window.');
break;
case Nimiq.ERR_UNSUPPORTED:
alert('Error: Browser not supported');
break;
default:
alert('Error: Nimiq initialization error');
break;
}
reject(code);
}));

function init(clientType = 'pico') {
Nimiq.init(async function() {
nimiqInitPromise.then(async () => {
document.getElementById('message').innerText = 'Nimiq loaded. Connecting and establishing consensus.';

// Connect to the testnet.
Expand Down Expand Up @@ -83,19 +98,6 @@

// Update the peer count every 1 second
setInterval(_updatePeerCount, 1000);

}, function(code) {
switch (code) {
case Nimiq.ERR_WAIT:
alert('Error: Already open in another tab or window.');
break;
case Nimiq.ERR_UNSUPPORTED:
alert('Error: Browser not supported');
break;
default:
alert('Error: Nimiq initialization error');
break;
}
});
}

Expand Down

0 comments on commit 44d70ef

Please sign in to comment.