Skip to content

Commit

Permalink
Added option to download attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
HeinrichvonStein committed Nov 21, 2024
1 parent 7e23d65 commit 239db5e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/green-waves-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@powersync/attachments': minor
---

Added option to download attachments
13 changes: 12 additions & 1 deletion packages/attachments/src/AbstractAttachmentQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export interface AttachmentQueueOptions {
* Whether to mark the initial watched attachment IDs to be synced
*/
performInitialSync?: boolean;
/**
* Should attachments be downloaded
*/
downloadAttachments?: boolean;
/**
* How to handle download errors, return { retry: false } to ignore the download
*/
Expand All @@ -35,7 +39,8 @@ export const DEFAULT_ATTACHMENT_QUEUE_OPTIONS: Partial<AttachmentQueueOptions> =
attachmentDirectoryName: 'attachments',
syncInterval: 30_000,
cacheLimit: 100,
performInitialSync: true
performInitialSync: true,
downloadAttachments: true
};

export abstract class AbstractAttachmentQueue<T extends AttachmentQueueOptions = AttachmentQueueOptions> {
Expand Down Expand Up @@ -426,6 +431,9 @@ export abstract class AbstractAttachmentQueue<T extends AttachmentQueueOptions =
}

watchDownloads() {
if (this.options.downloadAttachments) {
return;
}
this.idsToDownload(async (ids) => {
ids.map((id) => this.downloadQueue.add(id));
// No need to await this, the lock will ensure only one loop is running at a time
Expand All @@ -434,6 +442,9 @@ export abstract class AbstractAttachmentQueue<T extends AttachmentQueueOptions =
}

private async downloadRecords() {
if (this.options.downloadAttachments) {
return;
}
if (this.downloading) {
return;
}
Expand Down

0 comments on commit 239db5e

Please sign in to comment.