Add an event on socket connection open

This commit is contained in:
Hakan Shehu
2024-11-26 23:23:32 +01:00
parent 99b94f6f33
commit cf484b5536
3 changed files with 16 additions and 2 deletions

View File

@@ -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));
}
}

View File

@@ -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);
}
});
}

View File

@@ -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;