From cf484b5536a1da64c4a9bb5aef09ad17b3da5a38 Mon Sep 17 00:00:00 2001 From: Hakan Shehu Date: Tue, 26 Nov 2024 23:23:32 +0100 Subject: [PATCH] Add an event on socket connection open --- apps/desktop/src/main/services/socket-connection.ts | 7 ++++++- apps/desktop/src/main/services/sync-service.ts | 3 +++ apps/desktop/src/shared/types/events.ts | 8 +++++++- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/apps/desktop/src/main/services/socket-connection.ts b/apps/desktop/src/main/services/socket-connection.ts index 38f2e959..9aa5a891 100644 --- a/apps/desktop/src/main/services/socket-connection.ts +++ b/apps/desktop/src/main/services/socket-connection.ts @@ -3,6 +3,7 @@ import { BackoffCalculator } from '@/shared/lib/backoff-calculator'; import { Message } from '@colanode/core'; import { SelectAccount } from '@/main/data/app/schema'; import { syncService } from '@/main/services/sync-service'; +import { eventBus } from '@/shared/lib/event-bus'; export class SocketConnection { private readonly synapseUrl: string; @@ -55,6 +56,10 @@ export class SocketConnection { this.socket.onopen = () => { this.backoffCalculator.reset(); + eventBus.publish({ + type: 'socket_connection_opened', + accountId: this.account.id, + }); }; this.socket.onerror = () => { @@ -71,7 +76,7 @@ export class SocketConnection { } public sendMessage(message: Message): void { - if (this.socket) { + if (this.socket && this.isConnected()) { this.socket.send(JSON.stringify(message)); } } diff --git a/apps/desktop/src/main/services/sync-service.ts b/apps/desktop/src/main/services/sync-service.ts index 9ec2e426..ed843b10 100644 --- a/apps/desktop/src/main/services/sync-service.ts +++ b/apps/desktop/src/main/services/sync-service.ts @@ -34,6 +34,9 @@ class SyncService { this.syncLocalTransactions(event.userId); } else if (event.type === 'workspace_created') { this.requireNodeTransactions(event.workspace.userId); + } else if (event.type === 'socket_connection_opened') { + this.requireNodeTransactions(event.accountId); + this.requireCollaborations(event.accountId); } }); } diff --git a/apps/desktop/src/shared/types/events.ts b/apps/desktop/src/shared/types/events.ts index 14457274..d01838a9 100644 --- a/apps/desktop/src/shared/types/events.ts +++ b/apps/desktop/src/shared/types/events.ts @@ -132,6 +132,11 @@ export type ServerAvailabilityChangedEvent = { isAvailable: boolean; }; +export type SocketConnectionOpenedEvent = { + type: 'socket_connection_opened'; + accountId: string; +}; + export type Event = | NodeCreatedEvent | NodeUpdatedEvent @@ -155,4 +160,5 @@ export type Event = | QueryResultUpdatedEvent | RadarDataUpdatedEvent | NodeTransactionCreatedEvent - | ServerAvailabilityChangedEvent; + | ServerAvailabilityChangedEvent + | SocketConnectionOpenedEvent;