From 026036f5905c4e93c669d6dede9520f51fafec12 Mon Sep 17 00:00:00 2001 From: Juan Date: Tue, 26 Nov 2024 06:03:36 -0300 Subject: [PATCH 1/5] Better event source close resolve --- src/loaders/api.ts | 44 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/src/loaders/api.ts b/src/loaders/api.ts index 34980fd..094237a 100644 --- a/src/loaders/api.ts +++ b/src/loaders/api.ts @@ -309,6 +309,8 @@ export async function getInCommonEventsWithEvents( onEventsTotal?: (eventsTotal: number) => void, onEvents?: (events: Drop[]) => void, ): Promise { + let resolved = false + let totalInCommon: number | null = null let totalEvents: number | null = null let receivedEventIds: number | null = null @@ -333,10 +335,32 @@ 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 && + Object.values(inCommon.inCommon).every((owners) => owners.length > 0) && + receivedEvents != null && + totalEvents != null && + receivedEvents === totalEvents && + Object.keys(inCommon.events).length > 0 + ) { + resolve(inCommon) + } else { + reject(new Error( + `Connection lost to event '${eventId}' in common streaming` + )) + } }) inCommonStream.addEventListener('message', (ev) => { @@ -344,6 +368,8 @@ export async function getInCommonEventsWithEvents( 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` @@ -351,6 +377,8 @@ export async function getInCommonEventsWithEvents( return } if (!data || typeof data !== 'object') { + resolved = true + inCommonStream.close() reject(new Error( `Malformed data '${data}' when streaming event '${eventId}' in common` )) @@ -394,10 +422,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) @@ -426,10 +457,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) @@ -486,10 +520,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) @@ -514,6 +551,7 @@ export async function getInCommonEventsWithEvents( receivedEvents === totalEvents && Object.keys(inCommon.events).length > 0 ) { + resolved = true inCommonStream.close() resolve(inCommon) return From e7a31c09107aa67472411b92d209f67aa1f991da Mon Sep 17 00:00:00 2001 From: Juan Date: Tue, 26 Nov 2024 06:04:43 -0300 Subject: [PATCH 2/5] Version 1.17.3 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 9d49929..3d13a68 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@poap-xyz/poap-family", - "version": "1.17.2", + "version": "1.17.3", "author": { "name": "POAP", "url": "https://poap.xyz" From c56bfc713597831fc1b656184722a61f81d72b91 Mon Sep 17 00:00:00 2001 From: Juan Date: Tue, 26 Nov 2024 06:07:50 -0300 Subject: [PATCH 3/5] Check exact events --- src/loaders/api.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/loaders/api.ts b/src/loaders/api.ts index 094237a..285b588 100644 --- a/src/loaders/api.ts +++ b/src/loaders/api.ts @@ -353,7 +353,7 @@ export async function getInCommonEventsWithEvents( receivedEvents != null && totalEvents != null && receivedEvents === totalEvents && - Object.keys(inCommon.events).length > 0 + Object.keys(inCommon.events).length === totalEvents ) { resolve(inCommon) } else { @@ -549,7 +549,7 @@ export async function getInCommonEventsWithEvents( receivedEvents != null && totalEvents != null && receivedEvents === totalEvents && - Object.keys(inCommon.events).length > 0 + Object.keys(inCommon.events).length === totalEvents ) { resolved = true inCommonStream.close() From 4b4e6548b37410b8228c18671ec2ed0bd4edfb9e Mon Sep 17 00:00:00 2001 From: Juan Date: Tue, 26 Nov 2024 06:07:57 -0300 Subject: [PATCH 4/5] Fail when no events given --- src/loaders/api.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/loaders/api.ts b/src/loaders/api.ts index 285b588..d5f1bde 100644 --- a/src/loaders/api.ts +++ b/src/loaders/api.ts @@ -511,7 +511,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 ( From 2c8e82db38e2a3d1968ac4aa220d1788d7cd79be Mon Sep 17 00:00:00 2001 From: Juan Date: Tue, 26 Nov 2024 06:20:12 -0300 Subject: [PATCH 5/5] Remove check for at least one owner --- src/loaders/api.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/loaders/api.ts b/src/loaders/api.ts index d5f1bde..79225f9 100644 --- a/src/loaders/api.ts +++ b/src/loaders/api.ts @@ -349,7 +349,6 @@ 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 && @@ -551,7 +550,6 @@ 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 &&