Use instantiateGun instead of logoff and catch listener errors

This commit is contained in:
emad-salah 2021-11-04 13:31:58 +01:00
parent 8146bf6d7c
commit 53a8d44f69

View file

@ -506,7 +506,7 @@ module.exports = async (
await recreateLnServices() await recreateLnServices()
if (GunDB.isAuthenticated()) { if (GunDB.isAuthenticated()) {
GunDB.logoff() GunDB.instantiateGun()
} }
const publicKey = await GunDB.authenticate(alias, password) const publicKey = await GunDB.authenticate(alias, password)
@ -2296,27 +2296,39 @@ module.exports = async (
: publicKey : publicKey
? gun.user(publicKey) ? gun.user(publicKey)
: gun : gun
keys.forEach(key => (node = node.get(key)))
logger.info(`fetching: ${keys}`) logger.info(`fetching: ${keys}`)
return new Promise((res, rej) => { keys.forEach(key => (node = node.get(key)))
const listener = data => {
logger.info(`got res for: ${keys}`)
logger.info(data || 'falsey data (does not get logged)')
if (publicKeyForDecryption) {
GunWriteRPC.deepDecryptIfNeeded(
data,
publicKeyForDecryption,
epubForDecryption
)
.then(res)
.catch(rej)
} else {
res(data)
}
}
if (type === 'once') node.once(listener) if (!publicKeyForDecryption || !epubForDecryption) {
if (type === 'load') node.load(listener) logger.warn('[GUN] Missing public key for decryption!', {
publicKeyForDecryption,
epubForDecryption
})
}
return new Promise((res, rej) => {
try {
const listener = data => {
logger.info(`got res for: ${keys}`)
logger.info(data || 'falsey data (does not get logged)')
if (publicKeyForDecryption) {
GunWriteRPC.deepDecryptIfNeeded(
data,
publicKeyForDecryption,
epubForDecryption
)
.then(res)
.catch(rej)
} else {
res(data)
}
}
if (type === 'once') node.once(listener)
if (type === 'load') node.load(listener)
} catch (err) {
logger.error('Gun Fetch Error:', err)
}
}) })
}) })
} }