fix(persistence): bring back experimental indexedDB on web (#1348)

* fix(persistence): bring back experimental indexedDB on web

* chore(persistence): update changelog to be upcoming

Co-authored-by: Salvatore Giordano <salvatoregiordanoo@gmail.com>
This commit is contained in:
Dawid
2022-10-10 13:38:45 +02:00
committed by GitHub
parent 37e01b118b
commit 144d517dea
5 changed files with 45 additions and 9 deletions
@@ -1,3 +1,7 @@
## Upcoming
- Reintroduce support for experimental indexedDB on Web.
## 5.0.0
- Included the changes from version [4.3.0](#430) and [4.4.0](#440).
@@ -15,11 +15,12 @@ import 'package:stream_chat_persistence/stream_chat_persistence.dart';
/// specifically for native platform applications.
class SharedDB {
/// Returns a new instance of [DriftChatDatabase].
static DriftChatDatabase constructDatabase(
static Future<DriftChatDatabase> constructDatabase(
String userId, {
bool logStatements = false,
ConnectionMode connectionMode = ConnectionMode.regular,
}) {
bool webUseIndexedDbIfSupported = false, // Ignored on native
}) async {
final dbName = 'db_$userId';
if (connectionMode == ConnectionMode.background) {
return DriftChatDatabase.connect(
@@ -5,10 +5,11 @@ import 'package:stream_chat_persistence/stream_chat_persistence.dart';
/// A Helper class to construct new instances of [DriftChatDatabase]
class SharedDB {
/// Returns a new instance of [DriftChatDatabase].
static DriftChatDatabase constructDatabase(
static Future<DriftChatDatabase> constructDatabase(
String userId, {
bool logStatements = false,
ConnectionMode connectionMode = ConnectionMode.regular,
bool webUseIndexedDbIfSupported = false,
}) {
throw UnsupportedError(
'No implementation of the constructDatabase api provided',
@@ -7,13 +7,32 @@ import 'package:stream_chat_persistence/src/stream_chat_persistence_client.dart'
/// specifically for Web applications.
class SharedDB {
/// Returns a new instance of [DriftChatDatabase].
static DriftChatDatabase constructDatabase(
static Future<DriftChatDatabase> constructDatabase(
String userId, {
bool logStatements = false,
ConnectionMode connectionMode = ConnectionMode.regular, // Ignored on web
}) {
bool webUseIndexedDbIfSupported = false,
}) async {
final dbName = 'db_$userId';
final queryExecutor = WebDatabase(dbName, logStatements: logStatements);
final queryExecutor = await _getQueryExecutor(
dbName,
useIndexedDbIfSupported: webUseIndexedDbIfSupported,
logStatements: logStatements,
);
return DriftChatDatabase(userId, queryExecutor);
}
static Future<WebDatabase> _getQueryExecutor(
String dbName, {
required bool useIndexedDbIfSupported,
required bool logStatements,
}) async {
if (useIndexedDbIfSupported) {
return WebDatabase.withStorage(
await DriftWebStorage.indexedDbIfSupported(dbName),
logStatements: logStatements,
);
}
return WebDatabase(dbName, logStatements: logStatements);
}
}
@@ -29,8 +29,14 @@ class StreamChatPersistenceClient extends ChatPersistenceClient {
/// Connection mode on which the client will work
ConnectionMode connectionMode = ConnectionMode.regular,
Level logLevel = Level.WARNING,
/// Whether to use an experimental storage implementation on the web
/// that uses IndexedDB if the current browser supports it.
/// Otherwise, falls back to the local storage based implementation.
bool webUseExperimentalIndexedDb = false,
LogHandlerFunction? logHandlerFunction,
}) : _connectionMode = connectionMode,
_webUseIndexedDbIfSupported = webUseExperimentalIndexedDb,
_logger = Logger.detached('💽')..level = logLevel {
_logger.onRecord.listen(logHandlerFunction ?? _defaultLogHandler);
}
@@ -41,6 +47,7 @@ class StreamChatPersistenceClient extends ChatPersistenceClient {
final Logger _logger;
final ConnectionMode _connectionMode;
final bool _webUseIndexedDbIfSupported;
final _mutex = ReadWriteMutex();
void _defaultLogHandler(LogRecord record) {
@@ -68,11 +75,15 @@ class StreamChatPersistenceClient extends ChatPersistenceClient {
return true;
}
DriftChatDatabase _defaultDatabaseProvider(
Future<DriftChatDatabase> _defaultDatabaseProvider(
String userId,
ConnectionMode mode,
) =>
SharedDB.constructDatabase(userId, connectionMode: mode);
SharedDB.constructDatabase(
userId,
connectionMode: mode,
webUseIndexedDbIfSupported: _webUseIndexedDbIfSupported,
);
@override
Future<void> connect(
@@ -86,7 +97,7 @@ class StreamChatPersistenceClient extends ChatPersistenceClient {
);
}
db = databaseProvider?.call(userId, _connectionMode) ??
_defaultDatabaseProvider(userId, _connectionMode);
await _defaultDatabaseProvider(userId, _connectionMode);
}
@override