Skip to content

Commit 0e2f287

Browse files
committed
Disconnect immediately if one client disconnects.
1 parent af641ea commit 0e2f287

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

packages/powersync/lib/src/web/sync_worker.dart

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class _ConnectedClient {
7272
request.crudThrottleTimeMs, request.syncParamsEncoded, this);
7373
return (JSObject(), null);
7474
case SyncWorkerMessageType.abortSynchronization:
75-
_runner?.unregisterClient(this);
75+
_runner?.disconnectClient(this);
7676
_runner = null;
7777
return (JSObject(), null);
7878
default:
@@ -155,6 +155,10 @@ class _SyncRunner {
155155
await sync?.abort();
156156
sync = null;
157157
}
158+
case _DisconnectClient(:final client):
159+
connections.remove(client);
160+
await sync?.abort();
161+
sync = null;
158162
case _ActiveDatabaseClosed():
159163
_logger.info('Remote database closed, finding a new client');
160164
sync?.abort();
@@ -279,9 +283,15 @@ class _SyncRunner {
279283
client, currentCrudThrottleTimeMs, currentSyncParamsEncoded));
280284
}
281285

286+
/// Remove a client, disconnecting if no clients remain..
282287
void unregisterClient(_ConnectedClient client) {
283288
_mainEvents.add(_RemoveConnection(client));
284289
}
290+
291+
/// Remove a client, and immediately disconnect.
292+
void disconnectClient(_ConnectedClient client) {
293+
_mainEvents.add(_DisconnectClient(client));
294+
}
285295
}
286296

287297
sealed class _RunnerEvent {}
@@ -300,6 +310,12 @@ final class _RemoveConnection implements _RunnerEvent {
300310
_RemoveConnection(this.client);
301311
}
302312

313+
final class _DisconnectClient implements _RunnerEvent {
314+
final _ConnectedClient client;
315+
316+
_DisconnectClient(this.client);
317+
}
318+
303319
final class _ActiveDatabaseClosed implements _RunnerEvent {
304320
const _ActiveDatabaseClosed();
305321
}

packages/powersync/lib/src/web/sync_worker_protocol.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ enum SyncWorkerMessageType {
1414

1515
/// Sent from client to the sync worker to request the synchronization
1616
/// starting.
17+
/// If parameters change, the sync worker reconnects.
1718
startSynchronization,
1819

19-
/// Te [SyncWorkerMessage.payload] for the request is a numeric id, the
20+
/// The [SyncWorkerMessage.payload] for the request is a numeric id, the
2021
/// response can be anything (void).
22+
/// This disconnects immediately, even if other clients are still open.
2123
abortSynchronization,
2224

2325
/// Sent from the sync worker to the client when it needs an endpoint to

0 commit comments

Comments
 (0)