Skip to content

Commit 029df9d

Browse files
Wip add sync throttle & cache limiting
1 parent ecb71cb commit 029df9d

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

packages/attachments/src/AttachmentQueue.ts

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,58 @@ import { AbstractPowerSyncDatabase, ILogger } from '@powersync/common';
22
import { AttachmentContext } from './AttachmentContext.js';
33
import { LocalStorageAdapter } from './LocalStorageAdapter.js';
44
import { RemoteStorageAdapter } from './RemoteStorageAdapter.js';
5-
import { AttachmentRecord, AttachmentState } from './Schema.js';
5+
import { ATTACHMENT_TABLE, AttachmentRecord, AttachmentState } from './Schema.js';
66
import { StorageService } from './StorageService.js';
77
import { WatchedAttachmentItem } from './WatchedAttachmentItem.js';
88

99
export class AttachmentQueue {
1010
periodicSyncTimer?: ReturnType<typeof setInterval>;
11-
syncInterval: number = 30 * 1000;
1211
context: AttachmentContext;
1312
storageService: StorageService;
1413
localStorage: LocalStorageAdapter;
1514
remoteStorage: RemoteStorageAdapter;
15+
attachmentsDirectory?: string;
16+
tableName?: string;
17+
logger?: ILogger;
18+
syncInterval: number = 30 * 1000;
19+
syncThrottleDuration: number;
1620
downloadAttachments: boolean = true;
1721
watchActiveAbortController?: AbortController;
22+
archivedCacheLimit: number;
1823

1924
constructor({
2025
db,
2126
localStorage,
2227
remoteStorage,
2328
watchAttachments,
24-
tableName,
2529
logger,
26-
options
30+
tableName = ATTACHMENT_TABLE,
31+
syncInterval = 30 * 1000,
32+
syncThrottleDuration = 1000,
33+
downloadAttachments = true,
34+
archivedCacheLimit = 100
2735
}: {
2836
db: AbstractPowerSyncDatabase;
2937
remoteStorage: RemoteStorageAdapter;
3038
localStorage: LocalStorageAdapter;
31-
watchAttachments?: (onUpdate: (attachement: WatchedAttachmentItem[]) => void) => void;
39+
watchAttachments: (onUpdate: (attachement: WatchedAttachmentItem[]) => void) => void;
3240
tableName?: string;
3341
logger?: ILogger;
34-
options?: { syncInterval?: number; downloadAttachments?: boolean };
42+
syncInterval?: number;
43+
syncThrottleDuration?: number;
44+
downloadAttachments?: boolean;
45+
archivedCacheLimit?: number;
3546
}) {
3647
this.context = new AttachmentContext(db, tableName, logger ?? db.logger);
48+
this.remoteStorage = remoteStorage;
49+
this.localStorage = localStorage;
50+
this.watchAttachments = watchAttachments;
51+
this.tableName = tableName;
3752
this.storageService = new StorageService(this.context, localStorage, remoteStorage, logger ?? db.logger);
38-
if (options?.syncInterval != null) {
39-
this.syncInterval = options.syncInterval;
40-
}
41-
if (options?.downloadAttachments != null) {
42-
this.downloadAttachments = options.downloadAttachments;
43-
}
44-
45-
this.watchAttachments = watchAttachments ?? this.watchAttachments;
53+
this.syncInterval = syncInterval;
54+
this.syncThrottleDuration = syncThrottleDuration;
55+
this.downloadAttachments = downloadAttachments;
56+
this.archivedCacheLimit = archivedCacheLimit;
4657
}
4758

4859
watchAttachments(onUpdate: (attachement: WatchedAttachmentItem[]) => void): void {

0 commit comments

Comments
 (0)