feat(persistence): allow experimental indexedDB on web

This commit is contained in:
geweald
2022-06-20 12:02:03 +02:00
committed by geweald
parent bb2d008e03
commit 72dbca0d35
5 changed files with 42 additions and 10 deletions
@@ -1,6 +1,7 @@
## Upcoming ## Upcoming
- Added support for `Channel.ownCapabilities` - Added support for `Channel.ownCapabilities`
- Allowed experimental use of indexedDb on web with `webUseExperimentalIndexedDb` parameter on `StreamChatPersistenceClient`
## 4.1.0 ## 4.1.0
@@ -15,11 +15,12 @@ import 'package:stream_chat_persistence/stream_chat_persistence.dart';
/// specifically for native platform applications. /// specifically for native platform applications.
class SharedDB { class SharedDB {
/// Returns a new instance of [DriftChatDatabase]. /// Returns a new instance of [DriftChatDatabase].
static DriftChatDatabase constructDatabase( static Future<DriftChatDatabase> constructDatabase(
String userId, { String userId, {
bool logStatements = false, bool logStatements = false,
ConnectionMode connectionMode = ConnectionMode.regular, ConnectionMode connectionMode = ConnectionMode.regular,
}) { bool webUseIndexedDbIfSupported = false, // Ignored on native
}) async {
final dbName = 'db_$userId'; final dbName = 'db_$userId';
if (connectionMode == ConnectionMode.background) { if (connectionMode == ConnectionMode.background) {
return DriftChatDatabase.connect( 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] /// A Helper class to construct new instances of [DriftChatDatabase]
class SharedDB { class SharedDB {
/// Returns a new instance of [DriftChatDatabase]. /// Returns a new instance of [DriftChatDatabase].
static DriftChatDatabase constructDatabase( static Future<DriftChatDatabase> constructDatabase(
String userId, { String userId, {
bool logStatements = false, bool logStatements = false,
ConnectionMode connectionMode = ConnectionMode.regular, ConnectionMode connectionMode = ConnectionMode.regular,
bool webUseIndexedDbIfSupported = false,
}) { }) {
throw UnsupportedError( throw UnsupportedError(
'No implementation of the constructDatabase api provided', '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. /// specifically for Web applications.
class SharedDB { class SharedDB {
/// Returns a new instance of [DriftChatDatabase]. /// Returns a new instance of [DriftChatDatabase].
static DriftChatDatabase constructDatabase( static Future<DriftChatDatabase> constructDatabase(
String userId, { String userId, {
bool logStatements = false, bool logStatements = false,
ConnectionMode connectionMode = ConnectionMode.regular, // Ignored on web ConnectionMode connectionMode = ConnectionMode.regular, // Ignored on web
}) { bool webUseIndexedDbIfSupported = false,
}) async {
final dbName = 'db_$userId'; final dbName = 'db_$userId';
final queryExecutor = WebDatabase(dbName, logStatements: logStatements); final queryExecutor = await _getQueryExecutor(
dbName,
useIndexedDbIfSupported: webUseIndexedDbIfSupported,
logStatements: logStatements,
);
return DriftChatDatabase(userId, queryExecutor); 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);
}
} }
@@ -1,7 +1,6 @@
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:mutex/mutex.dart'; import 'package:mutex/mutex.dart';
import 'package:stream_chat/stream_chat.dart'; import 'package:stream_chat/stream_chat.dart';
import 'package:stream_chat_persistence/src/db/drift_chat_database.dart'; import 'package:stream_chat_persistence/src/db/drift_chat_database.dart';
/// Various connection modes on which [StreamChatPersistenceClient] can work /// Various connection modes on which [StreamChatPersistenceClient] can work
@@ -29,8 +28,14 @@ class StreamChatPersistenceClient extends ChatPersistenceClient {
/// Connection mode on which the client will work /// Connection mode on which the client will work
ConnectionMode connectionMode = ConnectionMode.regular, ConnectionMode connectionMode = ConnectionMode.regular,
Level logLevel = Level.WARNING, 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, LogHandlerFunction? logHandlerFunction,
}) : _connectionMode = connectionMode, }) : _connectionMode = connectionMode,
_webUseIndexedDbIfSupported = webUseExperimentalIndexedDb,
_logger = Logger.detached('💽')..level = logLevel { _logger = Logger.detached('💽')..level = logLevel {
_logger.onRecord.listen(logHandlerFunction ?? _defaultLogHandler); _logger.onRecord.listen(logHandlerFunction ?? _defaultLogHandler);
} }
@@ -41,6 +46,7 @@ class StreamChatPersistenceClient extends ChatPersistenceClient {
final Logger _logger; final Logger _logger;
final ConnectionMode _connectionMode; final ConnectionMode _connectionMode;
final bool _webUseIndexedDbIfSupported;
final _mutex = ReadWriteMutex(); final _mutex = ReadWriteMutex();
void _defaultLogHandler(LogRecord record) { void _defaultLogHandler(LogRecord record) {
@@ -68,11 +74,15 @@ class StreamChatPersistenceClient extends ChatPersistenceClient {
return true; return true;
} }
DriftChatDatabase _defaultDatabaseProvider( Future<DriftChatDatabase> _defaultDatabaseProvider(
String userId, String userId,
ConnectionMode mode, ConnectionMode mode,
) => ) =>
SharedDB.constructDatabase(userId, connectionMode: mode); SharedDB.constructDatabase(
userId,
connectionMode: mode,
webUseIndexedDbIfSupported: _webUseIndexedDbIfSupported,
);
@override @override
Future<void> connect( Future<void> connect(
@@ -86,7 +96,7 @@ class StreamChatPersistenceClient extends ChatPersistenceClient {
); );
} }
db = databaseProvider?.call(userId, _connectionMode) ?? db = databaseProvider?.call(userId, _connectionMode) ??
_defaultDatabaseProvider(userId, _connectionMode); await _defaultDatabaseProvider(userId, _connectionMode);
} }
@override @override