From 3040bb54a9a7cb5a8089f4562fa2b1787bc1f8ec Mon Sep 17 00:00:00 2001 From: liamcottle Date: Sat, 15 Feb 2025 22:44:54 +1300 Subject: [PATCH] add helper function for sharing 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 1f6c739..f6260e8 100644 --- a/index.html +++ b/index.html @@ -359,7 +359,7 @@ }, async shareContact(contact) { - await this.connection.sendCommandShareContact(contact.publicKey); + await this.connection.shareContact(contact.publicKey); }, async exportContact(contact) { try { diff --git a/src/connection/connection.js b/src/connection/connection.js index 0342037..46aaabf 100644 --- a/src/connection/connection.js +++ b/src/connection/connection.js @@ -788,6 +788,37 @@ class Connection extends EventEmitter { }); } + shareContact(pubKey) { + return new Promise(async (resolve, reject) => { + try { + + // resolve promise when we receive ok + const onOk = (response) => { + this.off(Constants.ResponseCodes.Ok, onOk); + this.off(Constants.ResponseCodes.Err, onErr); + resolve(response); + } + + // 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); + + // share contact + await this.sendCommandShareContact(pubKey); + + } catch(e) { + reject(e); + } + }); + } + removeContact(pubKey) { return new Promise(async (resolve, reject) => { try {