From 7a5d2a8b46b74bbd49837a091dcc5fe29288f5de Mon Sep 17 00:00:00 2001 From: Luke Steyn Date: Mon, 16 Dec 2024 20:23:34 +1000 Subject: [PATCH] Minor refactoring to improve events server subscription handling --- sdk/src/events/eventSubscriber.ts | 12 ++++++++---- sdk/src/events/eventsServerLogProvider.ts | 12 +++++++++++- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/sdk/src/events/eventSubscriber.ts b/sdk/src/events/eventSubscriber.ts index 5cdffe364..cc24741c6 100644 --- a/sdk/src/events/eventSubscriber.ts +++ b/sdk/src/events/eventSubscriber.ts @@ -171,10 +171,14 @@ export class EventSubscriber { this.logProvider.eventEmitter.on( 'reconnect', async (reconnectAttempts) => { - if (reconnectAttempts > logProviderConfig.maxReconnectAttempts) { - console.log( - `EventSubscriber: Reconnect attempts ${reconnectAttempts}/${logProviderConfig.maxReconnectAttempts}, reconnecting...` - ); + const hitMaxReconnectAttempts = + reconnectAttempts > logProviderConfig.maxReconnectAttempts; + + console.log( + `EventSubscriber: Reconnect attempts ${reconnectAttempts}/${logProviderConfig.maxReconnectAttempts}, ${hitMaxReconnectAttempts ? 'Falling Back...' : 'Reconnecting...'}` + ); + + if (hitMaxReconnectAttempts) { this.logProvider.eventEmitter.removeAllListeners('reconnect'); await this.unsubscribe(); this.updateFallbackProviderType( diff --git a/sdk/src/events/eventsServerLogProvider.ts b/sdk/src/events/eventsServerLogProvider.ts index 798890a68..8aacd4fcc 100644 --- a/sdk/src/events/eventsServerLogProvider.ts +++ b/sdk/src/events/eventsServerLogProvider.ts @@ -77,7 +77,17 @@ export class EventsServerLogProvider implements LogProvider { if (parsedData.message !== undefined) { return; } - const event = JSON.parse(parsedData.data); + let event = JSON.parse(parsedData.data); + + try { + // Handling for double-serialised events + if (typeof event === 'string') { + event = JSON.parse(event); + } + } catch (error) { + // swallow + } + this.callback( event.txSig, event.slot,