refined outgoing handling

This commit is contained in:
Daniel Lugo 2020-02-04 00:09:09 -04:00
parent 1f0ca74779
commit 92a7ef0894

View file

@ -323,8 +323,7 @@ const notifyOutgoingsListeners = () => {
outgoingsListeners.forEach(l => l(currentOutgoings)) outgoingsListeners.forEach(l => l(currentOutgoings))
} }
/** @type {UserGUNNode|null} */ let outSubbed = false
let lastUserWithListener = null
/** /**
* @param {OutgoingsListener} cb * @param {OutgoingsListener} cb
@ -334,26 +333,18 @@ const onOutgoing = cb => {
outgoingsListeners.add(cb) outgoingsListeners.add(cb)
cb(currentOutgoings) cb(currentOutgoings)
const currentUser = require('../../Mediator').getUser() if (!outSubbed) {
const user = require('../../Mediator').getUser()
if (lastUserWithListener !== currentUser) { user.get(Key.OUTGOINGS).open(
// in case user changed gun alias debounce(async data => {
currentOutgoings = {} try {
lastUserWithListener = currentUser
currentUser.get(Key.OUTGOINGS).open(async data => {
// deactivate this listener when user changes
if (lastUserWithListener !== require('../../Mediator').getUser()) {
return
}
if (typeof data !== 'object' || data === null) { if (typeof data !== 'object' || data === null) {
currentOutgoings = {} currentOutgoings = {}
notifyOutgoingsListeners() notifyOutgoingsListeners()
return return
} }
/** @type {Record<string, Outgoing>} */ /** @type {Record<string, Outgoing|null>} */
const newOuts = {} const newOuts = {}
const SEA = require('../../Mediator').mySEA const SEA = require('../../Mediator').mySEA
@ -365,7 +356,6 @@ const onOutgoing = cb => {
} }
if (out === null) { if (out === null) {
// @ts-ignore
newOuts[id] = null newOuts[id] = null
return return
} }
@ -387,7 +377,7 @@ const onOutgoing = cb => {
const ourSec = await SEA.secret( const ourSec = await SEA.secret(
await Utils.pubToEpub(pub), await Utils.pubToEpub(pub),
require('../../Mediator').getUser()._.sea user._.sea
) )
if (typeof messages === 'object' && messages !== null) { if (typeof messages === 'object' && messages !== null) {
@ -399,7 +389,11 @@ const onOutgoing = cb => {
typeof msg.body === 'string' && typeof msg.body === 'string' &&
typeof msg.timestamp === 'number' typeof msg.timestamp === 'number'
) { ) {
newOuts[id].messages[mid] = { const newOut = newOuts[id]
if (!newOut) {
return
}
newOut.messages[mid] = {
body: body:
msg.body === Actions.INITIAL_MSG msg.body === Actions.INITIAL_MSG
? Actions.INITIAL_MSG ? Actions.INITIAL_MSG
@ -415,7 +409,16 @@ const onOutgoing = cb => {
currentOutgoings = newOuts currentOutgoings = newOuts
notifyOutgoingsListeners() notifyOutgoingsListeners()
}) } catch (e) {
console.log('--------------------------')
console.log('Events -> onOutgoing')
console.log(e)
console.log('--------------------------')
}
}, 400)
)
outSubbed = true
} }
return () => { return () => {