Skip to content

Commit 7ca39b3

Browse files
committed
Refactor sync client and options
1 parent 87cbfb9 commit 7ca39b3

18 files changed

+372
-395
lines changed

demos/benchmarks/lib/powersync.dart

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,15 @@ Future<String> getDatabasePath() async {
8888

8989
var currentConnector = BackendConnector();
9090

91+
const options = SyncOptions(
92+
params: {'size_bucket': AppConfig.sizeBucket},
93+
crudThrottleTime: Duration(milliseconds: 1),
94+
);
95+
9196
Future<void> resync() async {
9297
await db.disconnectAndClear();
9398
timer.start(db);
94-
db.connect(
95-
connector: currentConnector,
96-
params: {'size_bucket': AppConfig.sizeBucket},
97-
crudThrottleTime: const Duration(milliseconds: 1));
99+
db.connect(connector: currentConnector, options: options);
98100
}
99101

100102
Future<void> openDatabase() async {
@@ -106,8 +108,5 @@ Future<void> openDatabase() async {
106108
BenchmarkItem.updateItemBenchmarks();
107109

108110
timer.start(db);
109-
db.connect(
110-
connector: currentConnector,
111-
params: {'size_bucket': AppConfig.sizeBucket},
112-
crudThrottleTime: const Duration(milliseconds: 1));
111+
db.connect(connector: currentConnector, options: options);
113112
}

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

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import 'package:powersync_core/src/log_internal.dart';
1515
import 'package:powersync_core/src/open_factory/abstract_powersync_open_factory.dart';
1616
import 'package:powersync_core/src/open_factory/native/native_open_factory.dart';
1717
import 'package:powersync_core/src/schema.dart';
18+
import 'package:powersync_core/src/sync/internal_connector.dart';
1819
import 'package:powersync_core/src/sync/options.dart';
1920
import 'package:powersync_core/src/sync/streaming_sync.dart';
2021
import 'package:powersync_core/src/sync/sync_status.dart';
@@ -162,16 +163,21 @@ class PowerSyncDatabaseImpl
162163
Future<void> handleMessage(Object? data) async {
163164
if (data is List) {
164165
String action = data[0] as String;
165-
if (action == "getCredentials") {
166+
if (action == "getCredentialsCached") {
166167
await (data[1] as PortCompleter).handle(() async {
167168
final token = await connector.getCredentialsCached();
168169
logger.fine('Credentials: $token');
169170
return token;
170171
});
171-
} else if (action == "invalidateCredentials") {
172+
} else if (action == "prefetchCredentials") {
172173
logger.fine('Refreshing credentials');
174+
final invalidate = data[2] as bool;
175+
173176
await (data[1] as PortCompleter).handle(() async {
174-
await connector.prefetchCredentials();
177+
if (invalidate) {
178+
connector.invalidateCredentials();
179+
}
180+
return await connector.prefetchCredentials();
175181
});
176182
} else if (action == 'init') {
177183
final port = initPort = data[1] as SendPort;
@@ -360,15 +366,16 @@ Future<void> _syncIsolate(_PowerSyncDatabaseIsolateArgs args) async {
360366
sPort.send(['log', copy]);
361367
});
362368

363-
Future<PowerSyncCredentials?> loadCredentials() async {
369+
Future<PowerSyncCredentials?> getCredentialsCached() async {
364370
final r = IsolateResult<PowerSyncCredentials?>();
365-
sPort.send(['getCredentials', r.completer]);
371+
sPort.send(['getCredentialsCached', r.completer]);
366372
return r.future;
367373
}
368374

369-
Future<void> invalidateCredentials() async {
370-
final r = IsolateResult<void>();
371-
sPort.send(['invalidateCredentials', r.completer]);
375+
Future<PowerSyncCredentials?> prefetchCredentials(
376+
{required bool invalidate}) async {
377+
final r = IsolateResult<PowerSyncCredentials?>();
378+
sPort.send(['prefetchCredentials', r.completer, invalidate]);
372379
return r.future;
373380
}
374381

@@ -386,9 +393,11 @@ Future<void> _syncIsolate(_PowerSyncDatabaseIsolateArgs args) async {
386393
final storage = BucketStorage(connection);
387394
final sync = StreamingSyncImplementation(
388395
adapter: storage,
389-
credentialsCallback: loadCredentials,
390-
invalidCredentialsCallback: invalidateCredentials,
391-
uploadCrud: uploadCrud,
396+
connector: InternalConnector(
397+
getCredentialsCached: getCredentialsCached,
398+
prefetchCredentials: prefetchCredentials,
399+
uploadCrud: uploadCrud,
400+
),
392401
crudUpdateTriggerStream: crudUpdateController.stream,
393402
options: args.options,
394403
client: http.Client(),
@@ -426,6 +435,6 @@ Future<void> _syncIsolate(_PowerSyncDatabaseIsolateArgs args) async {
426435
// This should be rare - any uncaught error is a bug. And in most cases,
427436
// it should occur after the database is already open.
428437
await shutdown();
429-
throw error;
438+
Error.throwWithStackTrace(error, stack);
430439
});
431440
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import 'package:powersync_core/src/log.dart';
1111
import 'package:powersync_core/src/open_factory/abstract_powersync_open_factory.dart';
1212
import 'package:powersync_core/src/open_factory/web/web_open_factory.dart';
1313
import 'package:powersync_core/src/schema.dart';
14+
import 'package:powersync_core/src/sync/internal_connector.dart';
1415
import 'package:powersync_core/src/sync/streaming_sync.dart';
1516
import 'package:sqlite_async/sqlite_async.dart';
1617

@@ -142,9 +143,7 @@ class PowerSyncDatabaseImpl
142143

143144
sync = StreamingSyncImplementation(
144145
adapter: storage,
145-
credentialsCallback: connector.getCredentialsCached,
146-
invalidCredentialsCallback: connector.prefetchCredentials,
147-
uploadCrud: () => connector.uploadData(this),
146+
connector: InternalConnector.wrap(connector, this),
148147
crudUpdateTriggerStream: crudStream,
149148
options: resolved,
150149
client: BrowserClient(),

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

Lines changed: 0 additions & 147 deletions
This file was deleted.
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import 'package:meta/meta.dart';
2+
3+
import '../connector.dart';
4+
import '../database/powersync_database.dart';
5+
6+
/// A view over a backend connector that does not require a reference to the
7+
/// PowerSync database.
8+
@internal
9+
abstract interface class InternalConnector {
10+
/// Fetch or return cached credentials.
11+
Future<PowerSyncCredentials?> getCredentialsCached();
12+
13+
/// Ask the backend connector to fetch a new set of credentials.
14+
///
15+
/// [invalidate] describes whether the current ([getCredentialsCached])
16+
/// credentials are already invalid, or whether this call is a pre-fetch.
17+
///
18+
/// A call to [getCredentialsCached] after this future completes should return
19+
/// the same credentials.
20+
Future<PowerSyncCredentials?> prefetchCredentials({bool invalidate = false});
21+
22+
/// Requests the connector to upload a crud batch to the backend.
23+
Future<void> uploadCrud();
24+
25+
const factory InternalConnector({
26+
required Future<PowerSyncCredentials?> Function() getCredentialsCached,
27+
required Future<PowerSyncCredentials?> Function({required bool invalidate})
28+
prefetchCredentials,
29+
required Future<void> Function() uploadCrud,
30+
}) = _CallbackConnector;
31+
32+
factory InternalConnector.wrap(
33+
PowerSyncBackendConnector connector, PowerSyncDatabase db) {
34+
return _WrapConnector(connector, db);
35+
}
36+
}
37+
38+
final class _WrapConnector implements InternalConnector {
39+
final PowerSyncBackendConnector connector;
40+
final PowerSyncDatabase database;
41+
42+
_WrapConnector(this.connector, this.database);
43+
44+
@override
45+
Future<PowerSyncCredentials?> getCredentialsCached() async {
46+
return connector.getCredentialsCached();
47+
}
48+
49+
@override
50+
Future<PowerSyncCredentials?> prefetchCredentials({bool invalidate = false}) {
51+
if (invalidate) {
52+
connector.invalidateCredentials();
53+
}
54+
return connector.prefetchCredentials();
55+
}
56+
57+
@override
58+
Future<void> uploadCrud() {
59+
return connector.uploadData(database);
60+
}
61+
}
62+
63+
final class _CallbackConnector implements InternalConnector {
64+
final Future<PowerSyncCredentials?> Function() _getCredentialsCached;
65+
final Future<PowerSyncCredentials?> Function({required bool invalidate})
66+
_prefetchCredentials;
67+
final Future<void> Function() _uploadCrud;
68+
69+
const _CallbackConnector({
70+
required Future<PowerSyncCredentials?> Function() getCredentialsCached,
71+
required Future<PowerSyncCredentials?> Function({required bool invalidate})
72+
prefetchCredentials,
73+
required Future<void> Function() uploadCrud,
74+
}) : _getCredentialsCached = getCredentialsCached,
75+
_prefetchCredentials = prefetchCredentials,
76+
_uploadCrud = uploadCrud;
77+
78+
@override
79+
Future<PowerSyncCredentials?> getCredentialsCached() {
80+
return _getCredentialsCached();
81+
}
82+
83+
@override
84+
Future<PowerSyncCredentials?> prefetchCredentials({bool invalidate = false}) {
85+
return _prefetchCredentials(invalidate: invalidate);
86+
}
87+
88+
@override
89+
Future<void> uploadCrud() {
90+
return _uploadCrud();
91+
}
92+
}

0 commit comments

Comments
 (0)