Skip to content

Commit

Permalink
fix notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
karolsojko committed Feb 2, 2024
1 parent 8cf60bf commit c7117cc
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 16 deletions.
20 changes: 13 additions & 7 deletions packages/services/src/Domain/Sync/SyncBackoffService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { AbstractService } from '../Service/AbstractService'
import { InternalEventHandlerInterface } from '../Internal/InternalEventHandlerInterface'
import { InternalEventBusInterface } from '../Internal/InternalEventBusInterface'
import { InternalEventInterface } from '../Internal/InternalEventInterface'
import { ApplicationEvent } from '../Event/ApplicationEvent'
import { SyncEvent } from '../Event/SyncEvent'

export class SyncBackoffService
extends AbstractService
Expand Down Expand Up @@ -63,12 +63,18 @@ export class SyncBackoffService
}

async handleEvent(event: InternalEventInterface): Promise<void> {
if (event.type === ApplicationEvent.CompletedIncrementalSync) {
for (const payload of (event.payload as Record<string, unknown>)
.uploadedPayloads as ServerSyncPushContextualPayload[]) {
this.backoffPenalties.delete(payload.uuid)
this.backoffStartTimestamps.delete(payload.uuid)
}
if (
event.type !== SyncEvent.PaginatedSyncRequestCompleted ||
event.payload === undefined ||
(event.payload as Record<string, unknown>).uploadedPayloads === undefined
) {
return
}

for (const payload of (event.payload as Record<string, unknown>)
.uploadedPayloads as ServerSyncPushContextualPayload[]) {
this.backoffPenalties.delete(payload.uuid)
this.backoffStartTimestamps.delete(payload.uuid)
}
}

Expand Down
8 changes: 4 additions & 4 deletions packages/snjs/lib/Application/Application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ import {
LoggerInterface,
canBlockDeinit,
} from '@standardnotes/utils'
import { UuidString, ApplicationEventPayload } from '../Types'
import { UuidString } from '../Types'
import { applicationEventForSyncEvent } from '@Lib/Application/Event'
import { BackupServiceInterface, FilesClientInterface } from '@standardnotes/files'
import { ComputePrivateUsername } from '@standardnotes/encryption'
Expand Down Expand Up @@ -273,12 +273,12 @@ export class SNApplication implements ApplicationInterface, AppGroupManagedAppli
}),
)

const syncEventCallback = async (eventName: SyncEvent) => {
const syncEventCallback = async (eventName: SyncEvent, data?: unknown) => {
const appEvent = applicationEventForSyncEvent(eventName)
if (appEvent) {
await encryptionService.onSyncEvent(eventName)

await this.notifyEvent(appEvent)
await this.notifyEvent(appEvent, data)

if (appEvent === ApplicationEvent.CompletedFullSync) {
if (!this.handledFullSyncStage) {
Expand Down Expand Up @@ -529,7 +529,7 @@ export class SNApplication implements ApplicationInterface, AppGroupManagedAppli
return this.addEventObserver(filteredCallback, event)
}

private async notifyEvent(event: ApplicationEvent, data?: ApplicationEventPayload) {
private async notifyEvent(event: ApplicationEvent, data?: unknown) {
if (event === ApplicationEvent.Started) {
this.onStart()
} else if (event === ApplicationEvent.Launched) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export function RegisterApplicationServicesEvents(container: Dependencies, event
events.addEventHandler(container.get(TYPES.VaultInviteService), ApplicationEvent.Launched)
events.addEventHandler(container.get(TYPES.VaultInviteService), SyncEvent.ReceivedSharedVaultInvites)
events.addEventHandler(container.get(TYPES.VaultInviteService), WebSocketsServiceEvent.UserInvitedToSharedVault)
events.addEventHandler(container.get(TYPES.SyncBackoffService), ApplicationEvent.CompletedIncrementalSync)
events.addEventHandler(container.get(TYPES.SyncBackoffService), SyncEvent.PaginatedSyncRequestCompleted)

if (container.get(TYPES.FilesBackupService)) {
events.addEventHandler(container.get(TYPES.FilesBackupService), ApplicationEvent.ApplicationStageChanged)
Expand Down
5 changes: 4 additions & 1 deletion packages/snjs/lib/Services/Sync/SyncService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1047,7 +1047,6 @@ export class SyncService
}

if (response.status === PAYLOAD_TOO_LARGE) {
void this.notifyEvent(SyncEvent.PayloadTooLarge)
this.opStatus?.setIsTooLarge()
}

Expand All @@ -1070,6 +1069,10 @@ export class SyncService
uuidsToBackOff.push(uuidOrError.getValue())
}

void this.notifyEvent(SyncEvent.PayloadTooLarge, {
uuids: uuidsToBackOff.map((uuid) => uuid.value),
})

this.syncBackoffService.backoffItems(uuidsToBackOff)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ const ApplicationView: FunctionComponent<Props> = ({ application, mainApplicatio
onAppLaunch()
}

const removeAppObserver = application.addEventObserver(async (eventName) => {
const removeAppObserver = application.addEventObserver(async (eventName, data) => {
switch (eventName) {
case ApplicationEvent.Started:
onAppStart()
Expand Down Expand Up @@ -153,12 +153,19 @@ const ApplicationView: FunctionComponent<Props> = ({ application, mainApplicatio
message: 'Too many requests. Please try again later.',
})
break
case ApplicationEvent.SyncPayloadTooLarge:
case ApplicationEvent.SyncPayloadTooLarge: {
if ('uuids' in (data as Record<string, unknown>) === false) {
return
}
const notes = application.items.findItems((data as Record<string, unknown>).uuids as string[])
const noteTitles = notes.map((note) => `"${note.title}"`).join(', ')

addToast({
type: ToastType.Error,
message: 'Unable to sync. The payload of the request is too large.',
message: `Unable to sync. The payload of the request is too large for the following notes: ${noteTitles}`,
})
break
}
}
})

Expand Down

0 comments on commit c7117cc

Please sign in to comment.