fix: Close generated sync sessions and expose the shared demo (#135)
This commit is contained in:
parent
4789f650be
commit
0f4b8116f4
2 changed files with 266 additions and 35 deletions
12
packages/serverpod_offline_sync/example/README.md
Normal file
12
packages/serverpod_offline_sync/example/README.md
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
# Example
|
||||
|
||||
The [Offline Sync Demo](https://github.com/marcelomendoncasoares/serverpod_offline_sync/tree/main/example/offline_sync_demo)
|
||||
is the shared example app for `serverpod_offline_sync`,
|
||||
`serverpod_offline_sync_client`, and `serverpod_offline_sync_server`.
|
||||
|
||||
The desktop Flutter app shows two independent local replicas alongside the
|
||||
server's merged data, so you can edit offline, sync each replica, and watch them
|
||||
converge. See the demo's README for setup and running instructions.
|
||||
|
||||
For the core client integration, start with
|
||||
[`offline_replica.dart`](https://github.com/marcelomendoncasoares/serverpod_offline_sync/blob/main/example/offline_sync_demo/lib/src/offline_replica.dart).
|
||||
|
|
@ -66,9 +66,32 @@ class OfflineSyncDatabaseSession implements DatabaseSession {
|
|||
syncBatchSize: syncBatchSize,
|
||||
continuousSyncInterval: continuousSyncInterval,
|
||||
persistentUserId: persistentUserId,
|
||||
);
|
||||
).._wrappedSession = session;
|
||||
|
||||
final OfflineSyncDatabase _db;
|
||||
DatabaseSession? _wrappedSession;
|
||||
Future<void>? _closeFuture;
|
||||
|
||||
/// Closes the underlying client database.
|
||||
///
|
||||
/// Supported for sessions created by [OfflineSyncDatabaseSession.wraps] around
|
||||
/// a [ClientDatabaseSession], including nested sync-session wrappers and the
|
||||
/// sessions returned by the generated client's `createSyncSession` method.
|
||||
/// Repeated or concurrent calls share the same close operation.
|
||||
///
|
||||
/// Throws [UnsupportedError] for sessions constructed directly from a
|
||||
/// [Database] or wrapping a non-client session. Those connections must be
|
||||
/// closed through their owner, such as the server.
|
||||
Future<void> close() => _closeFuture ??= switch (_wrappedSession) {
|
||||
final ClientDatabaseSession session => session.close(),
|
||||
final OfflineSyncDatabaseSession session => session.close(),
|
||||
_ => Future<void>.error(
|
||||
UnsupportedError(
|
||||
'Only sync sessions wrapping a ClientDatabaseSession can be closed. '
|
||||
'Close the underlying database through its owner.',
|
||||
),
|
||||
),
|
||||
};
|
||||
|
||||
@override
|
||||
OfflineSyncDatabase get db => _db;
|
||||
|
|
|
|||
Loading…
Reference in a new issue