@@ -7,6 +7,8 @@ import { SyncingService } from './SyncingService.js';
77import { WatchedAttachmentItem } from './WatchedAttachmentItem.js' ;
88import { AttachmentService } from './AttachmentService.js' ;
99
10+ export type AttachmentData = ArrayBuffer | Blob | string ;
11+
1012/**
1113 * AttachmentQueue manages the lifecycle and synchronization of attachments
1214 * between local and remote storage.
@@ -94,6 +96,7 @@ export class AttachmentQueue {
9496 downloadAttachments ?: boolean ;
9597 archivedCacheLimit ?: number ;
9698 } ) {
99+ console . debug ( 'AttachmentQueue constructor' )
97100 this . context = new AttachmentContext ( db , tableName , logger ?? db . logger ) ;
98101 this . remoteStorage = remoteStorage ;
99102 this . localStorage = localStorage ;
@@ -133,7 +136,12 @@ export class AttachmentQueue {
133136 * - Handles state transitions for archived and new attachments
134137 */
135138 async startSync ( ) : Promise < void > {
136- await this . stopSync ( ) ;
139+ console . debug ( '[QUEUE] AttachmentQueue startSync' )
140+ if ( this . attachmentService . watchActiveAttachments ) {
141+ await this . stopSync ( ) ;
142+ // re-create the watch after it was stopped
143+ this . watchActiveAttachments = this . attachmentService . watchActiveAttachments ( ) ;
144+ }
137145
138146 // Sync storage periodically
139147 this . periodicSyncTimer = setInterval ( async ( ) => {
@@ -143,12 +151,14 @@ export class AttachmentQueue {
143151 // Sync storage when there is a change in active attachments
144152 this . watchActiveAttachments . registerListener ( {
145153 onDiff : async ( ) => {
154+ console . debug ( '[QUEUE] watchActiveAttachments: diff detected, syncing storage' ) ;
146155 await this . syncStorage ( ) ;
147156 }
148157 } ) ;
149158
150159 // Process attachments when there is a change in watched attachments
151160 this . watchAttachments ( async ( watchedAttachments ) => {
161+ console . debug ( '[QUEUE] watchAttachments callback:' , watchedAttachments . length , 'items' ) ;
152162 // Need to get all the attachments which are tracked in the DB.
153163 // We might need to restore an archived attachment.
154164 const currentAttachments = await this . context . getAttachments ( ) ;
@@ -224,6 +234,7 @@ export class AttachmentQueue {
224234 }
225235
226236 if ( attachmentUpdates . length > 0 ) {
237+ console . debug ( '[QUEUE] Saving attachments:' , attachmentUpdates ) ;
227238 await this . context . saveAttachments ( attachmentUpdates ) ;
228239 }
229240 } ) ;
@@ -237,6 +248,7 @@ export class AttachmentQueue {
237248 */
238249 async syncStorage ( ) : Promise < void > {
239250 const activeAttachments = await this . context . getActiveAttachments ( ) ;
251+ console . debug ( '[QUEUE] syncStorage: processing' , activeAttachments . length , 'active attachments' ) ;
240252 await this . localStorage . initialize ( ) ;
241253 await this . syncingService . processAttachments ( activeAttachments ) ;
242254 await this . syncingService . deleteArchivedAttachments ( ) ;
@@ -275,7 +287,7 @@ export class AttachmentQueue {
275287 updateHook
276288 } : {
277289 // TODO: create a dedicated type for data
278- data : ArrayBuffer | Blob | string ;
290+ data : AttachmentData ;
279291 fileExtension : string ;
280292 mediaType ?: string ;
281293 metaData ?: string ;
0 commit comments