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

@@ -137,9 +137,6 @@ import { TYPES } from './Dependencies/Types'
import { RegisterApplicationServicesEvents } from './Dependencies/DependencyEvents'
import { Result } from '@standardnotes/domain-core'
/** How often to automatically sync, in milliseconds */
const DEFAULT_AUTO_SYNC_INTERVAL = 30_000
type LaunchCallback = {
receiveChallenge: (challenge: Challenge) => void
}
@@ -165,7 +162,6 @@ export class SNApplication implements ApplicationInterface, AppGroupManagedAppli
private serviceObservers: ObserverRemover[] = []
private managedSubscribers: ObserverRemover[] = []
private autoSyncInterval!: ReturnType<typeof setInterval>
/** True if the result of deviceInterface.openDatabase yields a new database being created */
private createdNewDatabase = false
@@ -463,7 +459,7 @@ export class SNApplication implements ApplicationInterface, AppGroupManagedAppli
throw 'Application has been destroyed.'
}
await this.handleStage(ApplicationStage.LoadedDatabase_12)
this.beginAutoSyncTimer()
this.sync.beginAutoSyncTimer()
await this.sync.sync({
mode: SyncMode.DownloadFirst,
source: SyncSource.External,
@@ -503,14 +499,6 @@ export class SNApplication implements ApplicationInterface, AppGroupManagedAppli
}
}
private beginAutoSyncTimer() {
this.autoSyncInterval = setInterval(() => {
const logger = this.dependencies.get<LoggerInterface>(TYPES.Logger)
logger.info('Syncing from autosync')
void this.sync.sync({ sourceDescription: 'Auto Sync' })
}, DEFAULT_AUTO_SYNC_INTERVAL)
}
private async handleStage(stage: ApplicationStage) {
await this.events.publishSync(
{
@@ -743,9 +731,6 @@ export class SNApplication implements ApplicationInterface, AppGroupManagedAppli
public deinit(mode: DeinitMode, source: DeinitSource): void {
this.dealloced = true
clearInterval(this.autoSyncInterval)
;(this.autoSyncInterval as unknown) = undefined
for (const uninstallObserver of this.serviceObservers) {
uninstallObserver()
}

View File

@@ -1331,6 +1331,15 @@ export class Dependencies {
)
})
this.factory.set(TYPES.WebSocketsService, () => {
return new WebSocketsService(
this.get<DiskStorageService>(TYPES.DiskStorageService),
this.options.webSocketUrl,
this.get<WebSocketApiService>(TYPES.WebSocketApiService),
this.get<InternalEventBus>(TYPES.InternalEventBus),
)
})
this.factory.set(TYPES.SyncService, () => {
return new SyncService(
this.get<ItemManager>(TYPES.ItemManager),
@@ -1347,6 +1356,7 @@ export class Dependencies {
sleepBetweenBatches: this.options.sleepBetweenBatches,
},
this.get<Logger>(TYPES.Logger),
this.get<WebSocketsService>(TYPES.WebSocketsService),
this.get<InternalEventBus>(TYPES.InternalEventBus),
)
})
@@ -1390,15 +1400,6 @@ export class Dependencies {
)
})
this.factory.set(TYPES.WebSocketsService, () => {
return new WebSocketsService(
this.get<DiskStorageService>(TYPES.DiskStorageService),
this.options.webSocketUrl,
this.get<WebSocketApiService>(TYPES.WebSocketApiService),
this.get<InternalEventBus>(TYPES.InternalEventBus),
)
})
this.factory.set(TYPES.WebSocketApiService, () => {
return new WebSocketApiService(this.get<WebSocketServer>(TYPES.WebSocketServer))
})

View File

@@ -40,6 +40,7 @@ export function RegisterApplicationServicesEvents(container: Dependencies, event
events.addEventHandler(container.get(TYPES.SubscriptionManager), ApplicationEvent.UserRolesChanged)
events.addEventHandler(container.get(TYPES.SubscriptionManager), SessionEvent.Restored)
events.addEventHandler(container.get(TYPES.SyncService), IntegrityEvent.IntegrityCheckCompleted)
events.addEventHandler(container.get(TYPES.SyncService), WebSocketsServiceEvent.ItemsChangedOnServer)
events.addEventHandler(container.get(TYPES.UserService), AccountEvent.SignedInOrRegistered)
events.addEventHandler(container.get(TYPES.VaultInviteService), ApplicationEvent.Launched)
events.addEventHandler(container.get(TYPES.VaultInviteService), SyncEvent.ReceivedSharedVaultInvites)