Skip to content

Commit

Permalink
Merge pull request #414 from poap-xyz/release/v1.17.3
Browse files Browse the repository at this point in the history
Release v1.17.3
  • Loading branch information
jm42 authored Nov 26, 2024
2 parents e65186f + 2c8e82d commit 8e6740a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@poap-xyz/poap-family",
"version": "1.17.2",
"version": "1.17.3",
"author": {
"name": "POAP",
"url": "https://poap.xyz"
Expand Down
54 changes: 48 additions & 6 deletions src/loaders/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,8 @@ export async function getInCommonEventsWithEvents(
onEventsTotal?: (eventsTotal: number) => void,
onEvents?: (events: Drop[]) => void,
): Promise<EventsInCommon | null> {
let resolved = false

let totalInCommon: number | null = null
let totalEvents: number | null = null
let receivedEventIds: number | null = null
Expand All @@ -333,24 +335,49 @@ export async function getInCommonEventsWithEvents(

return new Promise((resolve, reject) => {
inCommonStream.addEventListener('error', (ev) => {
if (resolved) {
return
}

resolved = true
inCommonStream.close()
reject(new Error(
`Connection lost to event '${eventId}' in common streaming`
))

if (
inCommon.ts != null &&
totalInCommon != null &&
receivedEventIds != null &&
receivedOwners != null &&
receivedOwners === receivedEventIds &&
receivedEventIds === totalInCommon &&
receivedEvents != null &&
totalEvents != null &&
receivedEvents === totalEvents &&
Object.keys(inCommon.events).length === totalEvents
) {
resolve(inCommon)
} else {
reject(new Error(
`Connection lost to event '${eventId}' in common streaming`
))
}
})

inCommonStream.addEventListener('message', (ev) => {
let data: unknown | undefined
try {
data = JSON.parse(ev.data)
} catch {
resolved = true
inCommonStream.close()
reject(new Error(
`Cannot parse data '${ev.data}' when streaming ` +
`event '${eventId}' in common`
))
return
}
if (!data || typeof data !== 'object') {
resolved = true
inCommonStream.close()
reject(new Error(
`Malformed data '${data}' when streaming event '${eventId}' in common`
))
Expand Down Expand Up @@ -394,10 +421,13 @@ export async function getInCommonEventsWithEvents(
}
if ('total' in data && data.total && typeof data.total === 'number') {
if (totalInCommon != null) {
resolved = true
inCommonStream.close()
reject(new Error(
`Received total (with value ${data.total} was ${totalInCommon}) ` +
`two times when streaming event '${eventId}' in common`
))
return
}
totalInCommon = data.total
onTotal?.(totalInCommon)
Expand Down Expand Up @@ -426,10 +456,13 @@ export async function getInCommonEventsWithEvents(
receivedOwners++
}
if (!receivedEventIds || !totalInCommon) {
resolved = true
inCommonStream.close()
reject(new Error(
`Received the first in common event before the total ` +
`when streaming event '${eventId}' in common`
))
return
}
inCommon.inCommon[data.eventId] = data.owners
onInCommon?.(data.eventId, data.owners)
Expand Down Expand Up @@ -477,7 +510,13 @@ export async function getInCommonEventsWithEvents(
totalEvents
)
} else {
console.warn('No new events?')
resolved = true
inCommonStream.close()
reject(new Error(
`Received empty list of events ` +
`when streaming event '${eventId}' in common`
))
return
}
}
if (
Expand All @@ -486,10 +525,13 @@ export async function getInCommonEventsWithEvents(
typeof data.eventsTotal === 'number'
) {
if (totalEvents != null) {
resolved = true
inCommonStream.close()
reject(new Error(
`Received events total (with value ${data.eventsTotal} was ${totalEvents}) ` +
`two times when streaming event '${eventId}' in common`
))
return
}
totalEvents = data.eventsTotal
onEventsTotal?.(data.eventsTotal)
Expand All @@ -508,12 +550,12 @@ export async function getInCommonEventsWithEvents(
receivedOwners != null &&
receivedOwners === receivedEventIds &&
receivedEventIds === totalInCommon &&
Object.values(inCommon.inCommon).every((owners) => owners.length > 0) &&
receivedEvents != null &&
totalEvents != null &&
receivedEvents === totalEvents &&
Object.keys(inCommon.events).length > 0
Object.keys(inCommon.events).length === totalEvents
) {
resolved = true
inCommonStream.close()
resolve(inCommon)
return
Expand Down

0 comments on commit 8e6740a

Please sign in to comment.