fix: persistence (#329)

* fix(llc): Save original passed persistence for client reconnection.

Signed-off-by: Sahil Kumar <[email protected]>

* fix(persistence): Check for empty channels before applying offset

Signed-off-by: Sahil Kumar <[email protected]>

* fix(llc): Remove debug logs

Signed-off-by: Sahil Kumar <[email protected]>

* fix(persistence): Reset isolate on successful disconnect

Signed-off-by: Sahil Kumar <[email protected]>

* use mutexes in persistence client

* fix(persistence): use `super.updateChannelStates`

Signed-off-by: Sahil Kumar <[email protected]>

* fix(persistence): fix database multiple times creation.

Signed-off-by: Sahil Kumar <[email protected]>

Co-authored-by: Sahil Kumar <[email protected]>
This commit is contained in:
Salvatore Giordano
2021-03-12 14:16:41 +01:00
committed by GitHub
co-authored by Sahil Kumar
parent ca1f70948e
commit 9952265109
11 changed files with 332 additions and 154 deletions
@@ -60,7 +60,6 @@ class MoorChatDatabase extends _$MoorChatDatabase {
/// Instantiate a new database instance
MoorChatDatabase.connect(
this._userId,
this._isolate,
DatabaseConnection connection,
) : super.connect(connection);
@@ -69,8 +68,6 @@ class MoorChatDatabase extends _$MoorChatDatabase {
/// User id to which the database is connected
String get userId => _userId;
MoorIsolate _isolate;
// you should bump this number whenever you change or add a table definition.
@override
int get schemaVersion => 2;
@@ -89,8 +86,5 @@ class MoorChatDatabase extends _$MoorChatDatabase {
);
/// Closes the database instance
Future<void> disconnect() async {
await _isolate?.shutdownAll();
await close();
}
Future<void> disconnect() => close();
}
@@ -76,17 +76,21 @@ class SharedDB {
/// [MoorChatDatabase.connect] created on a background isolate.
///
/// Generally used with [ConnectionMode.background].
static Future<MoorChatDatabase> constructMoorChatDatabase(
static MoorChatDatabase constructMoorChatDatabase(
String userId, {
bool logStatements = false,
}) async {
}) {
final dbName = 'db_$userId';
final isolate = await _createMoorIsolate(
dbName,
logStatements: logStatements,
return MoorChatDatabase.connect(
userId,
DatabaseConnection.delayed(Future(() async {
MoorIsolate isolate = await _createMoorIsolate(
dbName,
logStatements: logStatements,
);
return isolate.connect();
})),
);
final connection = await isolate.connect();
return MoorChatDatabase.connect(userId, isolate, connection);
}
}
@@ -1,3 +1,5 @@
import 'package:moor/backends.dart';
import 'package:stream_chat_persistence/src/db/moor_chat_database.dart';
import 'package:stream_chat_persistence/stream_chat_persistence.dart';
/// A Helper class to construct new instances of [MoorChatDatabase]
@@ -5,7 +7,7 @@ class SharedDB {
/// Returns a new instance of database.
///
/// Generally used with [ConnectionMode.regular].
static dynamic constructDatabase(
static Future<DelegatedDatabase> constructDatabase(
String userId, {
bool logStatements = false,
bool persistOnDisk = true,
@@ -16,7 +18,7 @@ class SharedDB {
/// Return a new instance of moor chat database.
///
/// Generally used with [ConnectionMode.background].
static dynamic constructMoorChatDatabase(
static MoorChatDatabase constructMoorChatDatabase(
String userId, {
bool logStatements = false,
}) {
@@ -22,10 +22,10 @@ class SharedDB {
/// default constructor.
///
/// Generally used with [ConnectionMode.background].
static Future<MoorChatDatabase> constructMoorChatDatabase(
static MoorChatDatabase constructMoorChatDatabase(
String userId, {
bool logStatements = false,
}) async {
}) {
final dbName = 'db_$userId';
return MoorChatDatabase(dbName, logStatements: logStatements);
}