Skip to content

Commit

Permalink
Disconnect immediately if one client disconnects.
Browse files Browse the repository at this point in the history
  • Loading branch information
rkistner committed Nov 4, 2024
1 parent af641ea commit 0e2f287
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
18 changes: 17 additions & 1 deletion packages/powersync/lib/src/web/sync_worker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class _ConnectedClient {
request.crudThrottleTimeMs, request.syncParamsEncoded, this);
return (JSObject(), null);
case SyncWorkerMessageType.abortSynchronization:
_runner?.unregisterClient(this);
_runner?.disconnectClient(this);
_runner = null;
return (JSObject(), null);
default:
Expand Down Expand Up @@ -155,6 +155,10 @@ class _SyncRunner {
await sync?.abort();
sync = null;
}
case _DisconnectClient(:final client):
connections.remove(client);
await sync?.abort();
sync = null;
case _ActiveDatabaseClosed():
_logger.info('Remote database closed, finding a new client');
sync?.abort();
Expand Down Expand Up @@ -279,9 +283,15 @@ class _SyncRunner {
client, currentCrudThrottleTimeMs, currentSyncParamsEncoded));
}

/// Remove a client, disconnecting if no clients remain..
void unregisterClient(_ConnectedClient client) {
_mainEvents.add(_RemoveConnection(client));
}

/// Remove a client, and immediately disconnect.
void disconnectClient(_ConnectedClient client) {
_mainEvents.add(_DisconnectClient(client));
}
}

sealed class _RunnerEvent {}
Expand All @@ -300,6 +310,12 @@ final class _RemoveConnection implements _RunnerEvent {
_RemoveConnection(this.client);
}

final class _DisconnectClient implements _RunnerEvent {
final _ConnectedClient client;

_DisconnectClient(this.client);
}

final class _ActiveDatabaseClosed implements _RunnerEvent {
const _ActiveDatabaseClosed();
}
4 changes: 3 additions & 1 deletion packages/powersync/lib/src/web/sync_worker_protocol.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ enum SyncWorkerMessageType {

/// Sent from client to the sync worker to request the synchronization
/// starting.
/// If parameters change, the sync worker reconnects.
startSynchronization,

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

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

0 comments on commit 0e2f287

Please sign in to comment.