fix: add timeout in publishing updated app data and sending notifications
This commit is contained in:
parent
6553ed89e0
commit
6b135ac54d
@ -29,6 +29,7 @@ import store from '../store/store'
|
|||||||
import { Meta, SignedEvent, UserAppData } from '../types'
|
import { Meta, SignedEvent, UserAppData } from '../types'
|
||||||
import { getHash } from './hash'
|
import { getHash } from './hash'
|
||||||
import { parseJson, removeLeadingSlash } from './string'
|
import { parseJson, removeLeadingSlash } from './string'
|
||||||
|
import { timeout } from './utils'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param hexKey hex private or public key
|
* @param hexKey hex private or public key
|
||||||
@ -555,20 +556,28 @@ export const updateUsersAppData = async (meta: Meta) => {
|
|||||||
const relayMap = (store.getState().relays as RelaysState).map!
|
const relayMap = (store.getState().relays as RelaysState).map!
|
||||||
const writeRelays = Object.keys(relayMap).filter((key) => relayMap[key].write)
|
const writeRelays = Object.keys(relayMap).filter((key) => relayMap[key].write)
|
||||||
|
|
||||||
const publishResult = await nostrController
|
console.log(`publishing event kind: ${kinds.Application}`)
|
||||||
.publishEvent(signedEvent, writeRelays)
|
const publishResult = await Promise.race([
|
||||||
.catch((errResults) => {
|
nostrController.publishEvent(signedEvent, writeRelays),
|
||||||
console.log('err :>> ', errResults)
|
timeout(1000 * 60 * 2)
|
||||||
toast.error('An error occurred while publishing App data')
|
]).catch((err) => {
|
||||||
|
console.log('err :>> ', err)
|
||||||
errResults.forEach((errResult: any) => {
|
if (err.message === 'Timeout') {
|
||||||
|
toast.error('Timeout occurred in publishing updated app data')
|
||||||
|
} else if (Array.isArray(err)) {
|
||||||
|
err.forEach((errResult) => {
|
||||||
toast.error(
|
toast.error(
|
||||||
`Publishing to ${errResult.relay} caused the following error: ${errResult.error}`
|
`Publishing to ${errResult.relay} caused the following error: ${errResult.error}`
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
} else {
|
||||||
|
toast.error(
|
||||||
|
'An unexpected error occurred in publishing updated app data '
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
return null
|
return null
|
||||||
})
|
})
|
||||||
|
|
||||||
if (!publishResult) return null
|
if (!publishResult) return null
|
||||||
|
|
||||||
@ -666,6 +675,8 @@ const uploadUserAppDataToBlossom = async (
|
|||||||
}
|
}
|
||||||
|
|
||||||
const getUserAppDataFromBlossom = async (url: string, privateKey: string) => {
|
const getUserAppDataFromBlossom = async (url: string, privateKey: string) => {
|
||||||
|
let errorCode = 0
|
||||||
|
|
||||||
const encrypted = await axios
|
const encrypted = await axios
|
||||||
.get(url, {
|
.get(url, {
|
||||||
responseType: 'blob'
|
responseType: 'blob'
|
||||||
@ -679,9 +690,21 @@ const getUserAppDataFromBlossom = async (url: string, privateKey: string) => {
|
|||||||
console.error(`error occurred in getting file from ${url}`, err)
|
console.error(`error occurred in getting file from ${url}`, err)
|
||||||
toast.error(err.message || `error occurred in getting file from ${url}`)
|
toast.error(err.message || `error occurred in getting file from ${url}`)
|
||||||
|
|
||||||
|
if (err.request) {
|
||||||
|
const { status } = err.request
|
||||||
|
errorCode = status
|
||||||
|
}
|
||||||
|
|
||||||
return null
|
return null
|
||||||
})
|
})
|
||||||
|
|
||||||
|
if (errorCode === 404) {
|
||||||
|
return {
|
||||||
|
sigits: {},
|
||||||
|
processedGiftWraps: []
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!encrypted) return null
|
if (!encrypted) return null
|
||||||
|
|
||||||
const secret = hexToBytes(privateKey)
|
const secret = hexToBytes(privateKey)
|
||||||
@ -818,15 +841,18 @@ export const sendNotification = async (receiver: string, meta: Meta) => {
|
|||||||
// Ensure relay list is not empty
|
// Ensure relay list is not empty
|
||||||
if (relaySet.read.length === 0) return
|
if (relaySet.read.length === 0) return
|
||||||
|
|
||||||
|
console.log('Publishing notifications')
|
||||||
// Publish the notification event to the recipient's read relays
|
// Publish the notification event to the recipient's read relays
|
||||||
const nostrController = NostrController.getInstance()
|
const nostrController = NostrController.getInstance()
|
||||||
await nostrController
|
|
||||||
.publishEvent(wrappedEvent, relaySet.read)
|
await Promise.race([
|
||||||
.catch((errResults) => {
|
nostrController.publishEvent(wrappedEvent, relaySet.read),
|
||||||
console.log(
|
timeout(1000 * 60 * 2)
|
||||||
`An error occurred while publishing notification event for ${hexToNpub(receiver)}`,
|
]).catch((err) => {
|
||||||
errResults
|
console.log(
|
||||||
)
|
`An error occurred while publishing notification event for ${hexToNpub(receiver)}`,
|
||||||
throw errResults
|
err
|
||||||
})
|
)
|
||||||
|
throw err
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
@ -49,3 +49,18 @@ export const isOnline = async () => {
|
|||||||
return false // If an error occurs, return false
|
return false // If an error occurs, return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a promise that rejects with a timeout error after a specified duration.
|
||||||
|
* @param ms The duration in milliseconds after which the promise should reject. Defaults to 60000 milliseconds (1 minute).
|
||||||
|
* @returns A promise that rejects with an Error('Timeout') after the specified duration.
|
||||||
|
*/
|
||||||
|
export const timeout = (ms: number = 60000) => {
|
||||||
|
return new Promise<never>((_, reject) => {
|
||||||
|
// Set a timeout using setTimeout
|
||||||
|
setTimeout(() => {
|
||||||
|
// Reject the promise with an Error indicating a timeout
|
||||||
|
reject(new Error('Timeout'))
|
||||||
|
}, ms) // Timeout duration in milliseconds
|
||||||
|
})
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user