Skip to content

Commit 1ce3e4b

Browse files
committed
Also listen for connection status
1 parent bf04f1d commit 1ce3e4b

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

packages/powersync_core/doc/attachments.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ Next, start the sync process by calling `attachmentQueue.startSync()`.
9191

9292
To create a new attachment locally, call `AttachmentQueue.saveFile`. To represent the attachment, this method takes
9393
the contents to store, the media type, an optional file extension and id.
94-
The queue will store the contents in a local file and mark is as queued for uploads. It also invokes a callback
94+
The queue will store the contents in a local file and mark it as queued for upload. It also invokes a callback
9595
responsible for referencing the id of the generated attachment in the primary data model:
9696

9797
```dart

packages/powersync_core/lib/src/attachments/attachment_queue_service.dart

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ base class AttachmentQueue {
7373

7474
final Mutex _mutex = Mutex();
7575
bool _closed = false;
76-
StreamSubscription<List<WatchedAttachmentItem>>? _syncStatusSubscription;
76+
StreamSubscription<void>? _syncStatusSubscription;
77+
StreamSubscription<void>? _watchedAttachmentsSubscription;
7778
final AttachmentService _attachmentsService;
7879
final SyncingService _syncingService;
7980

@@ -168,10 +169,23 @@ base class AttachmentQueue {
168169
await _verifyAttachments(context);
169170
});
170171

172+
// Listen for connectivity changes and watched attachments
171173
await _syncingService.startSync();
172174

173-
// Listen for connectivity changes and watched attachments
174-
_syncStatusSubscription = _watchAttachments().listen((items) async {
175+
_watchedAttachmentsSubscription =
176+
_watchAttachments().listen((items) async {
177+
await _processWatchedAttachments(items);
178+
});
179+
180+
var previouslyConnected = _db.currentStatus.connected;
181+
_syncStatusSubscription = _db.statusStream.listen((status) {
182+
if (!previouslyConnected && status.connected) {
183+
_syncingService.triggerSync();
184+
}
185+
186+
previouslyConnected = status.connected;
187+
});
188+
_watchAttachments().listen((items) async {
175189
await _processWatchedAttachments(items);
176190
});
177191

@@ -187,10 +201,19 @@ base class AttachmentQueue {
187201
}
188202

189203
Future<void> _stopSyncingInternal() async {
190-
if (_closed || _syncStatusSubscription == null) return;
204+
if (_closed ||
205+
_syncStatusSubscription == null ||
206+
_watchedAttachmentsSubscription == null) {
207+
return;
208+
}
209+
210+
await (
211+
_syncStatusSubscription!.cancel(),
212+
_watchedAttachmentsSubscription!.cancel(),
213+
).wait;
191214

192-
await _syncStatusSubscription?.cancel();
193215
_syncStatusSubscription = null;
216+
_watchedAttachmentsSubscription = null;
194217
await _syncingService.stopSync();
195218

196219
_logger.info('AttachmentQueue stopped syncing.');

packages/powersync_core/lib/src/attachments/sync/syncing_service.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,8 @@ final class SyncingService {
8585
}
8686

8787
/// Enqueues a sync operation (manual trigger).
88-
Future<void> triggerSync() async {
89-
if (_isClosed) return;
90-
_syncTriggerController.add(null);
88+
void triggerSync() {
89+
if (!_isClosed) _syncTriggerController.add(null);
9190
}
9291

9392
/// Stops all ongoing sync operations.

0 commit comments

Comments
 (0)