From 44d70ef9f912fd8af57f71d1a168a85ebe1cd0b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren?= Date: Thu, 25 Jul 2024 08:48:16 +0200 Subject: [PATCH] Initialize Nimiq libary on page load Instead of only when selecting a client type. This way one can already use the global `Nimiq` object without connecting to the network. --- index.html | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/index.html b/index.html index 679e1d4..b1d3bf3 100644 --- a/index.html +++ b/index.html @@ -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. @@ -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; - } }); }