Skip to content

Commit 814bade

Browse files
committed
Properly copy options
1 parent 88923e9 commit 814bade

File tree

5 files changed

+36
-17
lines changed

5 files changed

+36
-17
lines changed

packages/powersync_core/lib/src/database/native/native_powersync_database.dart

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ class PowerSyncDatabaseImpl
120120
@internal
121121
Future<void> connectInternal({
122122
required PowerSyncBackendConnector connector,
123-
required SyncOptions options,
123+
required ResolvedSyncOptions options,
124124
required AbortController abort,
125125
required Zone asyncWorkZone,
126126
}) async {
@@ -135,7 +135,6 @@ class PowerSyncDatabaseImpl
135135
SendPort? initPort;
136136
final hasInitPort = Completer<void>();
137137
final receivedIsolateExit = Completer<void>();
138-
final resolved = ResolvedSyncOptions(options);
139138

140139
Future<void> waitForShutdown() async {
141140
// Only complete the abortion signal after the isolate shuts down. This
@@ -183,7 +182,7 @@ class PowerSyncDatabaseImpl
183182
final port = initPort = data[1] as SendPort;
184183
hasInitPort.complete();
185184
var crudStream = database
186-
.onChange(['ps_crud'], throttle: resolved.crudThrottleTime);
185+
.onChange(['ps_crud'], throttle: options.crudThrottleTime);
187186
crudUpdateSubscription = crudStream.listen((event) {
188187
port.send(['update']);
189188
});
@@ -245,7 +244,7 @@ class PowerSyncDatabaseImpl
245244
_PowerSyncDatabaseIsolateArgs(
246245
receiveMessages.sendPort,
247246
dbRef,
248-
resolved,
247+
options,
249248
crudMutex.shared,
250249
syncMutex.shared,
251250
),

packages/powersync_core/lib/src/database/powersync_database_impl_stub.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class PowerSyncDatabaseImpl
115115
required PowerSyncBackendConnector connector,
116116
required AbortController abort,
117117
required Zone asyncWorkZone,
118-
required SyncOptions options,
118+
required ResolvedSyncOptions options,
119119
}) {
120120
throw UnimplementedError();
121121
}

packages/powersync_core/lib/src/database/powersync_db_mixin.dart

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -289,11 +289,10 @@ mixin PowerSyncDatabaseMixin implements SqliteConnection {
289289
// the lock for the connection.
290290
await initialize();
291291

292-
final resolvedOptions = SyncOptions(
293-
crudThrottleTime: options?.crudThrottleTime ?? crudThrottleTime,
294-
// ignore: deprecated_member_use_from_same_package
295-
retryDelay: options?.retryDelay ?? retryDelay,
296-
params: options?.params ?? params,
292+
final resolvedOptions = ResolvedSyncOptions.resolve(
293+
options,
294+
crudThrottleTime: crudThrottleTime,
295+
params: params,
297296
);
298297

299298
// ignore: deprecated_member_use_from_same_package
@@ -362,7 +361,7 @@ mixin PowerSyncDatabaseMixin implements SqliteConnection {
362361
@internal
363362
Future<void> connectInternal({
364363
required PowerSyncBackendConnector connector,
365-
required SyncOptions options,
364+
required ResolvedSyncOptions options,
366365
required AbortController abort,
367366
required Zone asyncWorkZone,
368367
});

packages/powersync_core/lib/src/database/web/web_powersync_database.dart

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,8 @@ class PowerSyncDatabaseImpl
118118
required PowerSyncBackendConnector connector,
119119
required AbortController abort,
120120
required Zone asyncWorkZone,
121-
required SyncOptions options,
121+
required ResolvedSyncOptions options,
122122
}) async {
123-
final resolved = ResolvedSyncOptions(options);
124-
125123
final storage = BucketStorage(database);
126124
StreamingSync sync;
127125
// Try using a shared worker for the synchronization implementation to avoid
@@ -130,7 +128,7 @@ class PowerSyncDatabaseImpl
130128
sync = await SyncWorkerHandle.start(
131129
database: this,
132130
connector: connector,
133-
options: options,
131+
options: options.source,
134132
workerUri: Uri.base.resolve('/powersync_sync.worker.js'),
135133
);
136134
} catch (e) {
@@ -139,13 +137,13 @@ class PowerSyncDatabaseImpl
139137
e,
140138
);
141139
final crudStream =
142-
database.onChange(['ps_crud'], throttle: resolved.crudThrottleTime);
140+
database.onChange(['ps_crud'], throttle: options.crudThrottleTime);
143141

144142
sync = StreamingSyncImplementation(
145143
adapter: storage,
146144
connector: InternalConnector.wrap(connector, this),
147145
crudUpdateTriggerStream: crudStream,
148-
options: resolved,
146+
options: options,
149147
client: BrowserClient(),
150148
// Only allows 1 sync implementation to run at a time per database
151149
// This should be global (across tabs) when using Navigator locks.

packages/powersync_core/lib/src/sync/options.dart

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ final class SyncOptions {
3333
this.params,
3434
this.syncImplementation = SyncClientImplementation.defaultClient,
3535
});
36+
37+
SyncOptions _copyWith({
38+
Duration? crudThrottleTime,
39+
Map<String, dynamic>? params,
40+
}) {
41+
return SyncOptions(
42+
crudThrottleTime: crudThrottleTime ?? this.crudThrottleTime,
43+
retryDelay: retryDelay,
44+
params: params ?? this.params,
45+
syncImplementation: syncImplementation,
46+
);
47+
}
3648
}
3749

3850
/// The PowerSync SDK offers two different implementations for receiving sync
@@ -63,6 +75,17 @@ enum SyncClientImplementation {
6375

6476
@internal
6577
extension type ResolvedSyncOptions(SyncOptions source) {
78+
factory ResolvedSyncOptions.resolve(
79+
SyncOptions? source, {
80+
Duration? crudThrottleTime,
81+
Map<String, dynamic>? params,
82+
}) {
83+
return ResolvedSyncOptions((source ?? SyncOptions())._copyWith(
84+
crudThrottleTime: crudThrottleTime,
85+
params: params,
86+
));
87+
}
88+
6689
Duration get crudThrottleTime =>
6790
source.crudThrottleTime ?? const Duration(milliseconds: 10);
6891

0 commit comments

Comments
 (0)