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