Merge pull request #33 from shocknet/events

Events
This commit is contained in:
Daniel Lugo 2020-02-21 11:07:07 -04:00 committed by GitHub
commit 76b8ed83c4

View file

@ -77,31 +77,54 @@ const __onUserToIncoming = (cb, user, SEA) => {
})
}
/** @type {Set<(av: string|null) => void>} */
const avatarListeners = new Set()
/** @type {string|null} */
let currentAvatar = null
const getAvatar = () => currentAvatar
/** @param {string|null} av */
const setAvatar = av => {
currentAvatar = av
avatarListeners.forEach(l => l(currentAvatar))
}
let avatarSubbed = false
/**
* @param {(avatar: string|null) => void} cb
* @param {UserGUNNode} user Pass only for testing purposes.
* @throws {Error} If user hasn't been auth.
* @returns {void}
* @returns {() => void}
*/
const onAvatar = (cb, user) => {
if (!user.is) {
throw new Error(ErrorCode.NOT_AUTH)
}
const callb = debounce(cb, DEBOUNCE_WAIT_TIME)
// Initial value if avvatar is undefined in gun
callb(null)
avatarListeners.add(cb)
cb(currentAvatar)
if (!avatarSubbed) {
avatarSubbed = true
user
.get(Key.PROFILE)
.get(Key.AVATAR)
.on(avatar => {
if (typeof avatar === 'string' || avatar === null) {
callb(avatar)
setAvatar(avatar)
}
})
}
return () => {
avatarListeners.delete(cb)
}
}
/**
* @param {(blacklist: string[]) => void} cb
* @param {UserGUNNode} user
@ -133,61 +156,105 @@ const onBlacklist = (cb, user) => {
})
}
/** @type {Set<(addr: string|null) => void>} */
const addressListeners = new Set()
/** @type {string|null} */
let currentAddress = null
const getHandshakeAddress = () => currentAddress
/** @param {string|null} addr */
const setAddress = addr => {
currentAddress = addr
addressListeners.forEach(l => l(currentAddress))
}
let addrSubbed = false
/**
* @param {(currentHandshakeAddress: string|null) => void} cb
* @param {UserGUNNode} user
* @returns {void}
* @returns {() => void}
*/
const onCurrentHandshakeAddress = (cb, user) => {
if (!user.is) {
throw new Error(ErrorCode.NOT_AUTH)
}
const callb = debounce(cb, DEBOUNCE_WAIT_TIME)
addressListeners.add(cb)
// If undefined, callback below wont be called. Let's supply null as the
// initial value.
callb(null)
cb(currentAddress)
if (!addrSubbed) {
addrSubbed = true
user.get(Key.CURRENT_HANDSHAKE_ADDRESS).on(addr => {
if (typeof addr !== 'string') {
console.error('expected handshake address to be an string')
callb(null)
setAddress(null)
return
}
callb(addr)
setAddress(addr)
})
}
return () => {
addressListeners.delete(cb)
}
}
/** @type {Set<(dn: string|null) => void>} */
const dnListeners = new Set()
/** @type {string|null} */
let currentDn = null
const getDisplayName = () => currentDn
/** @param {string|null} dn */
const setDn = dn => {
currentDn = dn
dnListeners.forEach(l => l(currentDn))
}
let dnSubbed = false
/**
* @param {(displayName: string|null) => void} cb
* @param {UserGUNNode} user Pass only for testing purposes.
* @throws {Error} If user hasn't been auth.
* @returns {void}
* @returns {() => void}
*/
const onDisplayName = (cb, user) => {
if (!user.is) {
throw new Error(ErrorCode.NOT_AUTH)
}
const callb = debounce(cb, DEBOUNCE_WAIT_TIME)
cb(currentDn)
// Initial value if display name is undefined in gun
callb(null)
dnListeners.add(cb)
if (!dnSubbed) {
dnSubbed = true
user
.get(Key.PROFILE)
.get(Key.DISPLAY_NAME)
.on(displayName => {
if (typeof displayName === 'string' || displayName === null) {
callb(displayName)
setDn(displayName)
}
})
}
return () => {
dnListeners.delete(cb)
}
}
/**
* @param {(messages: Record<string, Message>) => void} cb
* @param {string} userPK Public key of the user from whom the incoming
@ -531,5 +598,8 @@ module.exports = {
getCurrentSentReqs: require('./onSentReqs').getCurrentSentReqs,
onBio,
onSeedBackup,
onChats
onChats,
getAvatar,
getDisplayName,
getHandshakeAddress
}