From 6469b815b119277bf21cb549d8b587626596808c Mon Sep 17 00:00:00 2001 From: liamcottle Date: Sat, 15 Feb 2025 22:11:22 +1300 Subject: [PATCH] allow providing a timeout in milliseconds when fetching get self info --- index.html | 10 +++++++++- src/connection/connection.js | 7 +++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/index.html b/index.html index c7e5c82..dc9883a 100644 --- a/index.html +++ b/index.html @@ -177,7 +177,15 @@ async onConnected() { console.log("connected"); - await this.loadSelfInfo(); + + try { + await this.loadSelfInfo(); + } catch(e) { + alert("Disconnected: timed out waiting for self info"); + await this.disconnect(); + return; + } + await this.loadContacts(); this.connection.on(Constants.PushCodes.Advert, async () => { diff --git a/src/connection/connection.js b/src/connection/connection.js index 00db325..c9ce694 100644 --- a/src/connection/connection.js +++ b/src/connection/connection.js @@ -345,7 +345,7 @@ class Connection extends EventEmitter { }); } - getSelfInfo() { + getSelfInfo(timeoutMillis = null) { return new Promise(async (resolve, reject) => { // listen for response @@ -353,7 +353,10 @@ class Connection extends EventEmitter { resolve(selfInfo); }); - // todo add timeout in case user connected to unsupported device + // timeout after provided milliseconds if device did not respond + if(timeoutMillis != null){ + setTimeout(reject, timeoutMillis); + } // request self info await this.sendCommandAppStart();