mirror of
https://github.com/colanode/colanode.git
synced 2025-12-29 00:25:03 +01:00
Add an event on socket connection open
This commit is contained in:
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user