add helper function for adding or updating contact

This commit is contained in:
liamcottle 2025-02-15 22:53:10 +13:00
parent 3040bb54a9
commit 2aeb37ffa8
2 changed files with 48 additions and 17 deletions

View file

@ -850,6 +850,37 @@ class Connection extends EventEmitter {
});
}
addOrUpdateContact(publicKey, type, flags, outPathLen, outPath, advName, lastAdvert, advLat, advLon) {
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);
// add or update contact
await this.sendCommandAddUpdateContact(publicKey, type, flags, outPathLen, outPath, advName, lastAdvert, advLat, advLon);
} catch(e) {
reject(e);
}
});
}
resetPath(pubKey) {
return new Promise(async (resolve, reject) => {
try {