Skip to content

Commit 1177101

Browse files
DominicGBauerDominicGBauer
andauthored
chore(attachments): minor improvements and fix loop (#70)
* chore(attachments): minor improvements * docs: update changelog * chore: pr reverts --------- Co-authored-by: DominicGBauer <dominic@nomanini.com>
1 parent f9177e3 commit 1177101

File tree

6 files changed

+37
-10
lines changed

6 files changed

+37
-10
lines changed

demos/supabase-todolist/pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ packages:
429429
path: "../../packages/powersync_attachments_helper"
430430
relative: true
431431
source: path
432-
version: "0.3.0"
432+
version: "0.3.1"
433433
realtime_client:
434434
dependency: transitive
435435
description:

packages/powersync_attachments_helper/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 0.3.1
2+
3+
- Add periodic syncing and deleting of attachments
4+
- Remove unnecessary delete
5+
- Fix loop
6+
17
## 0.3.0
28

39
- BREAKING CHANGE: `reconcileId` has been removed in favour of `reconcileIds`. This will require a change to `watchIds` implementation which is shown in `example/getting_started.dart`

packages/powersync_attachments_helper/lib/src/attachments_queue.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,18 @@ abstract class AbstractAttachmentQueue {
3333
/// Return true if you want to ignore attachment
3434
Future<bool> Function(Attachment attachment, Object exception)? onUploadError;
3535

36+
/// Interval in minutes to periodically run [syncingService.startPeriodicSync]
37+
/// Default is 5 minutes
38+
int intervalInMinutes;
39+
3640
AbstractAttachmentQueue(
3741
{required this.db,
3842
required this.remoteStorage,
3943
this.attachmentDirectoryName = 'attachments',
4044
this.attachmentsQueueTableName = defaultAttachmentsQueueTableName,
4145
this.onDownloadError,
4246
this.onUploadError,
43-
performInitialSync = true}) {
47+
this.intervalInMinutes = 5}) {
4448
attachmentsService = AttachmentsService(
4549
db, localStorage, attachmentDirectoryName, attachmentsQueueTableName);
4650
syncingService = SyncingService(
@@ -68,6 +72,7 @@ abstract class AbstractAttachmentQueue {
6872

6973
watchIds();
7074
syncingService.watchAttachments();
75+
syncingService.startPeriodicSync(intervalInMinutes);
7176

7277
db.statusStream.listen((status) {
7378
if (db.currentStatus.connected) {

packages/powersync_attachments_helper/lib/src/attachments_service.dart

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,6 @@ class AttachmentsService {
9393
(id, filename, local_uri, media_type, size, timestamp, state) VALUES (?, ?, ?, ?, ?, ?, ?)
9494
''', updatedRecords);
9595

96-
await db.executeBatch('''
97-
DELETE FROM $table WHERE id = ?
98-
''', ids);
99-
10096
return;
10197
}
10298

packages/powersync_attachments_helper/lib/src/syncing_service.dart

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class SyncingService {
2020
final Future<bool> Function(Attachment attachment, Object exception)?
2121
onUploadError;
2222
bool isProcessing = false;
23+
Timer? timer;
2324

2425
SyncingService(this.db, this.remoteStorage, this.localStorage,
2526
this.attachmentsService, this.getLocalUri,
@@ -166,8 +167,7 @@ class SyncingService {
166167
bool fileExists = await file.exists();
167168

168169
if (fileExists) {
169-
log.info('ignore file $id.$fileExtension as it already exists');
170-
return;
170+
continue;
171171
}
172172

173173
log.info('Adding $id to queue');
@@ -179,4 +179,24 @@ class SyncingService {
179179

180180
await attachmentsService.saveAttachments(attachments);
181181
}
182+
183+
/// Delete attachments which have been archived
184+
deleteArchivedAttachments() async {
185+
await db.execute('''
186+
DELETE FROM ${attachmentsService.table}
187+
WHERE state = ${AttachmentState.archived.index}
188+
''');
189+
}
190+
191+
/// Periodically sync attachments and delete archived attachments
192+
void startPeriodicSync(int intervalInMinutes) {
193+
timer?.cancel();
194+
195+
timer = Timer.periodic(Duration(minutes: intervalInMinutes), (timer) {
196+
log.info('Syncing attachments');
197+
runSync();
198+
log.info('Deleting archived attachments');
199+
deleteArchivedAttachments();
200+
});
201+
}
182202
}

packages/powersync_attachments_helper/pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: powersync_attachments_helper
22
description: A helper library for handling attachments when using PowerSync.
3-
version: 0.3.0
3+
version: 0.3.1
44
repository: https://github.com/powersync-ja/powersync.dart
55
homepage: https://www.powersync.com/
66
environment:
@@ -12,7 +12,7 @@ dependencies:
1212

1313
powersync: ^1.2.2
1414
logging: ^1.2.0
15-
sqlite_async: ^0.6.0
15+
sqlite_async: ^0.6.1
1616
path_provider: ^2.1.2
1717

1818
dev_dependencies:

0 commit comments

Comments
 (0)