Skip to content

Commit b7b1252

Browse files
Merge branch '87-sync-multi-url' into 'main'
[Sync] Add Sync.clientMultiUrls to work with multiple servers See merge request objectbox/objectbox-dart!57
2 parents e78820c + 209f22d commit b7b1252

File tree

3 files changed

+31
-14
lines changed

3 files changed

+31
-14
lines changed

objectbox/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* Flutter for iOS/macOS: update to [objectbox-swift 1.9.1](https://github.com/objectbox/objectbox-swift/releases/tag/v1.9.1).
1212
Existing projects may have to run `pod repo update` and `pod update ObjectBox`.
1313
Notably requires at least iOS 12.0 and macOS 10.15.
14+
* Sync: add `Sync.clientMultiUrls` to work with multiple servers.
1415

1516
## 2.3.1 (2023-10-02)
1617

objectbox/lib/src/native/sync.dart

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ class SyncChange {
126126
}
127127

128128
/// Sync client is used to connect to an ObjectBox sync server.
129+
/// Use through [Sync].
129130
class SyncClient {
130131
final Store _store;
131132

@@ -137,25 +138,28 @@ class SyncClient {
137138
? _cSync
138139
: throw StateError('SyncClient already closed');
139140

140-
/// Creates a sync client associated with the given store and options.
141+
/// Creates a Sync client associated with the given store and options.
141142
/// This does not initiate any connection attempts yet: call start() to do so.
142-
SyncClient(this._store, String serverUrl, SyncCredentials creds) {
143+
SyncClient._(
144+
this._store, List<String> serverUrls, SyncCredentials credentials) {
145+
if (serverUrls.isEmpty) {
146+
throw ArgumentError.value(
147+
serverUrls, "serverUrls", "must contain at least one server URL");
148+
}
149+
143150
if (!Sync.isAvailable()) {
144151
throw UnsupportedError(
145152
'Sync is not available in the loaded ObjectBox runtime library. '
146153
'Please visit https://objectbox.io/sync/ for options.');
147154
}
148155

149-
final cServerUrl = serverUrl.toNativeUtf8();
150-
try {
151-
_cSync = checkObxPtr(
152-
C.sync1(InternalStoreAccess.ptr(_store), cServerUrl.cast()),
153-
'failed to create sync client');
154-
} finally {
155-
malloc.free(cServerUrl);
156-
}
156+
_cSync = withNativeStrings(
157+
serverUrls,
158+
(ptr, size) => checkObxPtr(
159+
C.sync_urls(InternalStoreAccess.ptr(_store), ptr, size),
160+
'failed to create Sync client'));
157161

158-
setCredentials(creds);
162+
setCredentials(credentials);
159163
}
160164

161165
/// Closes and cleans up all resources used by this sync client.
@@ -557,18 +561,23 @@ class Sync {
557561
/// Returns true if the loaded ObjectBox native library supports Sync.
558562
static bool isAvailable() => _syncAvailable;
559563

560-
/// Creates a sync client associated with the given store and configures it
564+
/// Creates a Sync client associated with the given store and configures it
561565
/// with the given options. This does not initiate any connection attempts
562566
/// yet, call [SyncClient.start()] to do so.
563567
///
564568
/// Before [SyncClient.start()], you can still configure some aspects of the
565569
/// client, e.g. its [SyncRequestUpdatesMode] mode.
566570
static SyncClient client(
567-
Store store, String serverUri, SyncCredentials creds) {
571+
Store store, String serverUrl, SyncCredentials credentials) =>
572+
clientMultiUrls(store, [serverUrl], credentials);
573+
574+
/// Like [client], but accepts a list of URLs to work with multiple servers.
575+
static SyncClient clientMultiUrls(
576+
Store store, List<String> serverUrls, SyncCredentials credentials) {
568577
if (syncClientsStorage.containsKey(store)) {
569578
throw StateError('Only one sync client can be active for a store');
570579
}
571-
final client = SyncClient(store, serverUri, creds);
580+
final client = SyncClient._(store, serverUrls, credentials);
572581
syncClientsStorage[store] = client;
573582
InternalStoreAccess.addCloseListener(store, client, client.close);
574583
return client;

objectbox_test/test/sync_test.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ void main() {
5050
expect(entity.hasFlag(OBXEntityFlags.SYNC_ENABLED), isTrue);
5151
});
5252

53+
test('Sync.clientMulti throws if empty URL list', () {
54+
expect(
55+
() => Sync.clientMultiUrls(store, [], SyncCredentials.none()),
56+
throwsA(isArgumentError.having((e) => e.message, 'message',
57+
contains('must contain at least one server URL'))));
58+
});
59+
5360
test('SyncCredentials string encoding', () {
5461
// Let's check some special characters and verify the data is how it would
5562
// look like if the same shared secret was provided to the sync-server via

0 commit comments

Comments
 (0)