fix: cleanup paymentEvents, simplify websockets (#3570)

This commit is contained in:
dni ⚡ 2025-11-26 13:11:27 +01:00 committed by GitHub
parent 7f114ddcc0
commit baa9a35773
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 50 additions and 57 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -2,18 +2,6 @@ window.LNbits = {
g: window.g, g: window.g,
utils: window._lnbitsUtils, utils: window._lnbitsUtils,
api: window._lnbitsApi, api: window._lnbitsApi,
events: {
onInvoicePaid(wallet, cb) {
ws = new WebSocket(`${websocketUrl}/${wallet.inkey}`)
ws.onmessage = ev => {
const data = JSON.parse(ev.data)
if (data.payment) {
cb(data)
}
}
return ws.onclose
}
},
map: { map: {
user(data) { user(data) {
const obj = { const obj = {

View file

@ -18,13 +18,11 @@ window.g = Vue.reactive({
fiatBalance: 0, fiatBalance: 0,
exchangeRate: 0, exchangeRate: 0,
fiatTracking: false, fiatTracking: false,
wallets: [],
payments: [], payments: [],
walletEventListeners: [], walletEventListeners: [],
showNewWalletDialog: false, showNewWalletDialog: false,
newWalletType: 'lightning', newWalletType: 'lightning',
updatePayments: false, updatePayments: false,
updatePaymentsHash: '',
currencies: WINDOW_SETTINGS.LNBITS_CURRENCIES ?? [], currencies: WINDOW_SETTINGS.LNBITS_CURRENCIES ?? [],
allowedCurrencies: WINDOW_SETTINGS.LNBITS_ALLOWED_CURRENCIES ?? [], allowedCurrencies: WINDOW_SETTINGS.LNBITS_ALLOWED_CURRENCIES ?? [],
locale: localStore('lnbits.lang', navigator.languages[1] ?? 'en'), locale: localStore('lnbits.lang', navigator.languages[1] ?? 'en'),

View file

@ -661,10 +661,6 @@ window.PageWallet = {
}, },
'g.updatePayments'() { 'g.updatePayments'() {
this.parse.show = false this.parse.show = false
if (this.receive.paymentHash === this.g.updatePaymentsHash) {
this.receive.show = false
this.receive.paymentHash = null
}
if ( if (
this.g.wallet.currency && this.g.wallet.currency &&
this.$q.localStorage.getItem( this.$q.localStorage.getItem(

View file

@ -12,43 +12,58 @@ window.windowMixin = {
this.g.newWalletType = walletType this.g.newWalletType = walletType
this.g.showNewWalletDialog = true this.g.showNewWalletDialog = true
}, },
onWebsocketMessage(ev) {
const data = JSON.parse(ev.data)
if (!data.payment) {
console.error('ws message no payment', data)
return
}
// update sidebar wallet balances
this.g.user.wallets.forEach(w => {
if (w.id === data.payment.wallet_id) {
w.sat = data.wallet_balance
}
})
// if current wallet, update balance and payments
if (this.g.wallet.id === data.payment.wallet_id) {
this.g.wallet.sat = data.wallet_balance
// lnbits-payment-list is watching
this.g.updatePayments = !this.g.updatePayments
}
// NOTE: react only on incoming payments for now
if (data.payment.amount > 0) {
eventReaction(data.wallet_balance * 1000)
}
},
paymentEvents() { paymentEvents() {
this.g.walletEventListeners = this.g.walletEventListeners || [] let timeout
this.g.user.wallets.forEach(wallet => { this.g.user.wallets.forEach(wallet => {
if (!this.g.walletEventListeners.includes(wallet.id)) { if (!this.g.walletEventListeners.includes(wallet.id)) {
this.g.walletEventListeners.push(wallet.id) this.g.walletEventListeners.push(wallet.id)
LNbits.events.onInvoicePaid(wallet, data => { const ws = new WebSocket(`${websocketUrl}/${wallet.inkey}`)
const walletIndex = this.g.user.wallets.findIndex( ws.onmessage = this.onWebsocketMessage
w => w.id === wallet.id ws.onopen = () => console.log('ws connected for wallet', wallet.id)
// onclose and onerror can both happen on their own or together,
// so we add a clearTimeout to avoid multiple reconnections
ws.onclose = () => {
console.log('ws closed, reconnecting...', wallet.id)
this.g.walletEventListeners = this.g.walletEventListeners.filter(
id => id !== wallet.id
) )
if (walletIndex !== -1) { clearTimeout(timeout)
//needed for balance being deducted timeout = setTimeout(this.paymentEvents, 5000)
let satBalance = data.wallet_balance }
if (data.payment.amount < 0) { ws.onerror = () => {
satBalance = data.wallet_balance += data.payment.amount / 1000 console.warn('ws error, reconnecting...', wallet.id)
} this.g.walletEventListeners = this.g.walletEventListeners.filter(
//update the wallet id => id !== wallet.id
Object.assign(this.g.user.wallets[walletIndex], { )
sat: satBalance, clearTimeout(timeout)
msat: data.wallet_balance * 1000, timeout = setTimeout(this.paymentEvents, 5000)
fsat: data.wallet_balance.toLocaleString() }
})
//update the current wallet
if (this.g.wallet.id === data.payment.wallet_id) {
Object.assign(this.g.wallet, this.g.user.wallets[walletIndex])
//if on the wallet page and payment is incoming trigger the eventReaction
if (
data.payment.amount > 0 &&
window.location.pathname === '/wallet'
) {
eventReaction(data.wallet_balance * 1000)
}
}
}
this.g.updatePaymentsHash = data.payment.payment_hash
this.g.updatePayments = !this.g.updatePayments
})
} }
}) })
}, },
@ -103,6 +118,7 @@ window.windowMixin = {
async created() { async created() {
if (window.user) { if (window.user) {
this.g.user = Vue.reactive(window.LNbits.map.user(window.user)) this.g.user = Vue.reactive(window.LNbits.map.user(window.user))
this.paymentEvents()
} }
if (window.wallet) { if (window.wallet) {
this.g.wallet = Vue.reactive(window.LNbits.map.wallet(window.wallet)) this.g.wallet = Vue.reactive(window.LNbits.map.wallet(window.wallet))
@ -110,10 +126,5 @@ window.windowMixin = {
if (window.extensions) { if (window.extensions) {
this.g.extensions = Vue.reactive(window.extensions) this.g.extensions = Vue.reactive(window.extensions)
} }
},
mounted() {
if (this.g.user) {
this.paymentEvents()
}
} }
} }