Skip to content

Commit 56b814e

Browse files
update sqlite_async.dart. Fix disconnectAndClear on web.
1 parent b91d2b6 commit 56b814e

File tree

12 files changed

+37
-22
lines changed

12 files changed

+37
-22
lines changed

demos/supabase-anonymous-auth/pubspec.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ packages:
342342
path: "../../packages/powersync"
343343
relative: true
344344
source: path
345-
version: "1.3.0-alpha.1"
345+
version: "1.3.0-alpha.2"
346346
realtime_client:
347347
dependency: transitive
348348
description:
@@ -464,10 +464,10 @@ packages:
464464
dependency: "direct main"
465465
description:
466466
name: sqlite_async
467-
sha256: "91f454cddc85617bea2c7c1544ff386887d0d2cf0ecdb3599015c05cc141ff4d"
467+
sha256: "7a68036b2fc2fae5fc0efbc6bd436deb79e39090282348d979e6a7c38c8d067e"
468468
url: "https://pub.dev"
469469
source: hosted
470-
version: "0.7.0-alpha.1"
470+
version: "0.7.0-alpha.2"
471471
stack_trace:
472472
dependency: transitive
473473
description:

demos/supabase-anonymous-auth/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ dependencies:
1616
supabase_flutter: ^2.0.2
1717
path: ^1.8.3
1818
logging: ^1.2.0
19-
sqlite_async: ^0.7.0-alpha.1
19+
sqlite_async: ^0.7.0-alpha.2
2020
universal_io: ^2.2.2
2121

2222
dev_dependencies:

demos/supabase-edge-function-auth/pubspec.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ packages:
342342
path: "../../packages/powersync"
343343
relative: true
344344
source: path
345-
version: "1.3.0-alpha.1"
345+
version: "1.3.0-alpha.2"
346346
realtime_client:
347347
dependency: transitive
348348
description:
@@ -464,10 +464,10 @@ packages:
464464
dependency: "direct main"
465465
description:
466466
name: sqlite_async
467-
sha256: "91f454cddc85617bea2c7c1544ff386887d0d2cf0ecdb3599015c05cc141ff4d"
467+
sha256: "7a68036b2fc2fae5fc0efbc6bd436deb79e39090282348d979e6a7c38c8d067e"
468468
url: "https://pub.dev"
469469
source: hosted
470-
version: "0.7.0-alpha.1"
470+
version: "0.7.0-alpha.2"
471471
stack_trace:
472472
dependency: transitive
473473
description:

demos/supabase-edge-function-auth/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ dependencies:
1616
supabase_flutter: ^2.0.2
1717
path: ^1.8.3
1818
logging: ^1.2.0
19-
sqlite_async: ^0.7.0-alpha.1
19+
sqlite_async: ^0.7.0-alpha.2
2020
universal_io: ^2.2.2
2121

2222
dev_dependencies:

demos/supabase-simple-chat/pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ packages:
374374
path: "../../packages/powersync"
375375
relative: true
376376
source: path
377-
version: "1.3.0-alpha.1"
377+
version: "1.3.0-alpha.2"
378378
realtime_client:
379379
dependency: transitive
380380
description:

demos/supabase-todolist/pubspec.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ packages:
430430
path: "../../packages/powersync"
431431
relative: true
432432
source: path
433-
version: "1.3.0-alpha.1"
433+
version: "1.3.0-alpha.2"
434434
powersync_attachments_helper:
435435
dependency: "direct main"
436436
description:
@@ -559,10 +559,10 @@ packages:
559559
dependency: "direct main"
560560
description:
561561
name: sqlite_async
562-
sha256: "91f454cddc85617bea2c7c1544ff386887d0d2cf0ecdb3599015c05cc141ff4d"
562+
sha256: "7a68036b2fc2fae5fc0efbc6bd436deb79e39090282348d979e6a7c38c8d067e"
563563
url: "https://pub.dev"
564564
source: hosted
565-
version: "0.7.0-alpha.1"
565+
version: "0.7.0-alpha.2"
566566
stack_trace:
567567
dependency: transitive
568568
description:

demos/supabase-todolist/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ dependencies:
1717
supabase_flutter: ^2.0.1
1818
path: ^1.8.3
1919
logging: ^1.2.0
20-
sqlite_async: ^0.7.0-alpha.1
20+
sqlite_async: ^0.7.0-alpha.2
2121
camera: ^0.10.5+7
2222
image: ^4.1.3
2323
universal_io: ^2.2.2

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ Future<void> _powerSyncDatabaseIsolate(
332332
updateStream: updateController.stream,
333333
retryDelay: args.retryDelay,
334334
client: http.Client());
335-
sync.streamingSync();
335+
sync.streamingSync(null);
336336
sync.statusStream.listen((event) {
337337
sPort.send(['status', event]);
338338
});

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,16 @@ mixin PowerSyncDatabaseMixin implements SqliteConnection {
125125
/// Use [connect] to connect again.
126126
Future<void> disconnect() async {
127127
if (disconnecter != null) {
128-
await disconnecter!.abort();
128+
/// Checking `disconnecter.aborted` prevents race conditions
129+
/// where multiple calls to `disconnect` can attempt to abort
130+
/// the controller more than once before it has finished aborting.
131+
if (disconnecter!.aborted == false) {
132+
await disconnecter!.abort();
133+
disconnecter = null;
134+
} else {
135+
/// Wait for the abort to complete. Continue updating the sync status after completed
136+
await disconnecter!.onAbort;
137+
}
129138
}
130139
setStatus(
131140
SyncStatus(connected: false, lastSyncedAt: currentStatus.lastSyncedAt));

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,7 @@ class PowerSyncDatabaseImpl
124124

125125
// Disconnect if connected
126126
await disconnect();
127-
final disconnector = AbortController();
128-
disconnecter = disconnector;
127+
disconnecter = AbortController();
129128

130129
await isInitialized;
131130

@@ -145,7 +144,7 @@ class PowerSyncDatabaseImpl
145144
sync.statusStream.listen((event) {
146145
setStatus(event);
147146
});
148-
sync.streamingSync();
147+
sync.streamingSync(disconnecter);
149148
}
150149

151150
/// Takes a read lock, without starting a transaction.

packages/powersync/lib/src/streaming_sync.dart

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'dart:async';
22
import 'dart:convert' as convert;
33
import 'package:http/http.dart' as http;
4+
import 'package:powersync/src/abort_controller.dart';
45
import 'package:powersync/src/exceptions.dart';
56
import 'package:powersync/src/log_internal.dart';
67

@@ -48,10 +49,13 @@ class StreamingSyncImplementation {
4849
statusStream = _statusStreamController.stream;
4950
}
5051

51-
Future<void> streamingSync() async {
52+
Future<void> streamingSync(AbortController? abortController) async {
5253
crudLoop();
5354
var invalidCredentials = false;
5455
while (true) {
56+
if (abortController?.aborted == true) {
57+
return;
58+
}
5559
_updateStatus(connecting: true);
5660
try {
5761
if (invalidCredentials && invalidCredentialsCallback != null) {
@@ -60,7 +64,7 @@ class StreamingSyncImplementation {
6064
await invalidCredentialsCallback!();
6165
invalidCredentials = false;
6266
}
63-
await streamingSyncIteration();
67+
await streamingSyncIteration(abortController);
6468
// Continue immediately
6569
} catch (e, stacktrace) {
6670
final message = _syncErrorMessage(e);
@@ -173,7 +177,7 @@ class StreamingSyncImplementation {
173177
_statusStreamController.add(newStatus);
174178
}
175179

176-
Future<bool> streamingSyncIteration() async {
180+
Future<bool> streamingSyncIteration(AbortController? abortController) async {
177181
adapter.startSession();
178182
final bucketEntries = await adapter.getBucketStates();
179183

@@ -201,6 +205,9 @@ class StreamingSyncImplementation {
201205
bool haveInvalidated = false;
202206

203207
await for (var line in merged) {
208+
if (abortController?.aborted == true) {
209+
return false;
210+
}
204211
_updateStatus(connected: true, connecting: false);
205212
if (line is Checkpoint) {
206213
targetCheckpoint = line;

packages/powersync_attachments_helper/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ dependencies:
1313
powersync: ^1.3.0-alpha.2
1414
logging: ^1.2.0
1515
sqlite3: ^2.3.0
16-
sqlite_async: ^0.7.0-alpha.1
16+
sqlite_async: ^0.7.0-alpha.2
1717
path_provider: ^2.1.1
1818

1919
dev_dependencies:

0 commit comments

Comments
 (0)