Skip to content

chore(attachments): minor improvements and fix loop #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion demos/supabase-todolist/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ packages:
path: "../../packages/powersync_attachments_helper"
relative: true
source: path
version: "0.3.0"
version: "0.3.1"
realtime_client:
dependency: transitive
description:
Expand Down
6 changes: 6 additions & 0 deletions packages/powersync_attachments_helper/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.3.1

- Add periodic syncing and deleting of attachments
- Remove unnecessary delete
- Fix loop

## 0.3.0

- 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`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,18 @@ abstract class AbstractAttachmentQueue {
/// Return true if you want to ignore attachment
Future<bool> Function(Attachment attachment, Object exception)? onUploadError;

/// Interval in minutes to periodically run [syncingService.startPeriodicSync]
/// Default is 5 minutes
int intervalInMinutes;

AbstractAttachmentQueue(
{required this.db,
required this.remoteStorage,
this.attachmentDirectoryName = 'attachments',
this.attachmentsQueueTableName = defaultAttachmentsQueueTableName,
this.onDownloadError,
this.onUploadError,
performInitialSync = true}) {
this.intervalInMinutes = 5}) {
attachmentsService = AttachmentsService(
db, localStorage, attachmentDirectoryName, attachmentsQueueTableName);
syncingService = SyncingService(
Expand Down Expand Up @@ -68,6 +72,7 @@ abstract class AbstractAttachmentQueue {

watchIds();
syncingService.watchAttachments();
syncingService.startPeriodicSync(intervalInMinutes);

db.statusStream.listen((status) {
if (db.currentStatus.connected) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,6 @@ class AttachmentsService {
(id, filename, local_uri, media_type, size, timestamp, state) VALUES (?, ?, ?, ?, ?, ?, ?)
''', updatedRecords);

await db.executeBatch('''
DELETE FROM $table WHERE id = ?
''', ids);

return;
}

Expand Down
24 changes: 22 additions & 2 deletions packages/powersync_attachments_helper/lib/src/syncing_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class SyncingService {
final Future<bool> Function(Attachment attachment, Object exception)?
onUploadError;
bool isProcessing = false;
Timer? timer;

SyncingService(this.db, this.remoteStorage, this.localStorage,
this.attachmentsService, this.getLocalUri,
Expand Down Expand Up @@ -166,8 +167,7 @@ class SyncingService {
bool fileExists = await file.exists();

if (fileExists) {
log.info('ignore file $id.$fileExtension as it already exists');
return;
continue;
}

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

await attachmentsService.saveAttachments(attachments);
}

/// Delete attachments which have been archived
deleteArchivedAttachments() async {
await db.execute('''
DELETE FROM ${attachmentsService.table}
WHERE state = ${AttachmentState.archived.index}
''');
}

/// Periodically sync attachments and delete archived attachments
void startPeriodicSync(int intervalInMinutes) {
timer?.cancel();

timer = Timer.periodic(Duration(minutes: intervalInMinutes), (timer) {
log.info('Syncing attachments');
runSync();
log.info('Deleting archived attachments');
deleteArchivedAttachments();
});
}
}
4 changes: 2 additions & 2 deletions packages/powersync_attachments_helper/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: powersync_attachments_helper
description: A helper library for handling attachments when using PowerSync.
version: 0.3.0
version: 0.3.1
repository: https://github.com/powersync-ja/powersync.dart
homepage: https://www.powersync.com/
environment:
Expand All @@ -12,7 +12,7 @@ dependencies:

powersync: ^1.2.2
logging: ^1.2.0
sqlite_async: ^0.6.0
sqlite_async: ^0.6.1
path_provider: ^2.1.2

dev_dependencies:
Expand Down