diff --git a/packages/stream_chat_persistence/CHANGELOG.md b/packages/stream_chat_persistence/CHANGELOG.md index f85d13b7..64f0a502 100644 --- a/packages/stream_chat_persistence/CHANGELOG.md +++ b/packages/stream_chat_persistence/CHANGELOG.md @@ -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). diff --git a/packages/stream_chat_persistence/lib/src/db/shared/native_db.dart b/packages/stream_chat_persistence/lib/src/db/shared/native_db.dart index a30dd59d..ca9a98bf 100644 --- a/packages/stream_chat_persistence/lib/src/db/shared/native_db.dart +++ b/packages/stream_chat_persistence/lib/src/db/shared/native_db.dart @@ -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 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( diff --git a/packages/stream_chat_persistence/lib/src/db/shared/unsupported_db.dart b/packages/stream_chat_persistence/lib/src/db/shared/unsupported_db.dart index c840aa5f..b92105c5 100644 --- a/packages/stream_chat_persistence/lib/src/db/shared/unsupported_db.dart +++ b/packages/stream_chat_persistence/lib/src/db/shared/unsupported_db.dart @@ -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 constructDatabase( String userId, { bool logStatements = false, ConnectionMode connectionMode = ConnectionMode.regular, + bool webUseIndexedDbIfSupported = false, }) { throw UnsupportedError( 'No implementation of the constructDatabase api provided', diff --git a/packages/stream_chat_persistence/lib/src/db/shared/web_db.dart b/packages/stream_chat_persistence/lib/src/db/shared/web_db.dart index 394b38d0..89dc864b 100644 --- a/packages/stream_chat_persistence/lib/src/db/shared/web_db.dart +++ b/packages/stream_chat_persistence/lib/src/db/shared/web_db.dart @@ -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 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 _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); + } } diff --git a/packages/stream_chat_persistence/lib/src/stream_chat_persistence_client.dart b/packages/stream_chat_persistence/lib/src/stream_chat_persistence_client.dart index 6eb29139..3cb4eef1 100644 --- a/packages/stream_chat_persistence/lib/src/stream_chat_persistence_client.dart +++ b/packages/stream_chat_persistence/lib/src/stream_chat_persistence_client.dart @@ -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 _defaultDatabaseProvider( String userId, ConnectionMode mode, ) => - SharedDB.constructDatabase(userId, connectionMode: mode); + SharedDB.constructDatabase( + userId, + connectionMode: mode, + webUseIndexedDbIfSupported: _webUseIndexedDbIfSupported, + ); @override Future connect( @@ -86,7 +97,7 @@ class StreamChatPersistenceClient extends ChatPersistenceClient { ); } db = databaseProvider?.call(userId, _connectionMode) ?? - _defaultDatabaseProvider(userId, _connectionMode); + await _defaultDatabaseProvider(userId, _connectionMode); } @override