From 2e3ecdb15ec9a98bdd93e3867763e31edca353ca Mon Sep 17 00:00:00 2001 From: liamcottle Date: Sat, 15 Feb 2025 22:41:58 +1300 Subject: [PATCH] add helper function for removing contact --- index.html | 2 +- src/connection/connection.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/index.html b/index.html index d570979..109db61 100644 --- a/index.html +++ b/index.html @@ -352,7 +352,7 @@ } // remove contact from device - await this.connection.sendCommandRemoveContact(contact.publicKey); + await this.connection.removeContact(contact.publicKey); // reload contacts await this.loadContacts(); diff --git a/src/connection/connection.js b/src/connection/connection.js index ccb9b58..3c9275c 100644 --- a/src/connection/connection.js +++ b/src/connection/connection.js @@ -788,6 +788,37 @@ class Connection extends EventEmitter { }); } + removeContact(pubKey) { + return new Promise(async (resolve, reject) => { + try { + + // resolve promise when we receive ok + const onOk = () => { + this.off(Constants.ResponseCodes.Ok, onOk); + this.off(Constants.ResponseCodes.Err, onErr); + resolve(); + } + + // reject promise when we receive err + const onErr = () => { + this.off(Constants.ResponseCodes.Ok, onOk); + this.off(Constants.ResponseCodes.Err, onErr); + reject(); + } + + // listen for events + this.once(Constants.ResponseCodes.Ok, onOk); + this.once(Constants.ResponseCodes.Err, onErr); + + // remove contact + await this.sendCommandRemoveContact(pubKey); + + } catch(e) { + reject(e); + } + }); + } + } export default Connection;