better displayName Event
This commit is contained in:
parent
fba877e814
commit
4f81b8f6b8
1 changed files with 36 additions and 13 deletions
|
|
@ -185,32 +185,54 @@ const onCurrentHandshakeAddress = (cb, user) => {
|
|||
})
|
||||
}
|
||||
|
||||
/** @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
|
||||
|
|
@ -555,5 +577,6 @@ module.exports = {
|
|||
onBio,
|
||||
onSeedBackup,
|
||||
onChats,
|
||||
getAvatar
|
||||
getAvatar,
|
||||
getDisplayName
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue