chore: rely on websockets for autosync if a websocket connection is open (#2672)

This commit is contained in:
Karol Sójko
2023-12-04 09:10:18 +01:00
committed by GitHub
parent 910f4a6388
commit 258422cf3c
12 changed files with 66 additions and 58 deletions

View File

@@ -3,4 +3,5 @@ export enum WebSocketsServiceEvent {
NotificationAddedForUser = 'NotificationAddedForUser',
MessageSentToUser = 'MessageSentToUser',
UserInvitedToSharedVault = 'UserInvitedToSharedVault',
ItemsChangedOnServer = 'ItemsChangedOnServer',
}

View File

@@ -55,6 +55,10 @@ export class WebSocketsService extends AbstractService<WebSocketsServiceEvent, D
}
}
isWebSocketConnectionOpen(): boolean {
return this.webSocket?.readyState === WebSocket.OPEN
}
public closeWebSocketConnection(): void {
this.webSocket?.close()
}
@@ -62,6 +66,9 @@ export class WebSocketsService extends AbstractService<WebSocketsServiceEvent, D
private onWebSocketMessage(messageEvent: MessageEvent) {
const eventData = JSON.parse(messageEvent.data)
switch (eventData.type) {
case 'ITEMS_CHANGED_ON_SERVER':
void this.notifyEvent(WebSocketsServiceEvent.ItemsChangedOnServer, eventData)
break
case 'USER_ROLES_CHANGED':
void this.notifyEvent(WebSocketsServiceEvent.UserRoleMessageReceived, eventData)
break

View File

@@ -16,7 +16,7 @@ export interface SyncServiceInterface extends AbstractService<SyncEvent> {
isDatabaseLoaded(): boolean
onNewDatabaseCreated(): Promise<void>
loadDatabasePayloads(): Promise<void>
beginAutoSyncTimer(): void
resetSyncState(): void
markAllItemsAsNeedingSyncAndPersist(): Promise<void>
downloadFirstSync(waitTimeOnFailureMs: number, otherSyncOptions?: Partial<SyncOptions>): Promise<void>