feat(persistence): allow experimental indexedDB on web
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
## Upcoming
|
||||
|
||||
- Added support for `Channel.ownCapabilities`
|
||||
- Allowed experimental use of indexedDb on web with `webUseExperimentalIndexedDb` parameter on `StreamChatPersistenceClient`
|
||||
|
||||
## 4.1.0
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:mutex/mutex.dart';
|
||||
import 'package:stream_chat/stream_chat.dart';
|
||||
|
||||
import 'package:stream_chat_persistence/src/db/drift_chat_database.dart';
|
||||
|
||||
/// Various connection modes on which [StreamChatPersistenceClient] can work
|
||||
@@ -29,8 +28,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 +46,7 @@ class StreamChatPersistenceClient extends ChatPersistenceClient {
|
||||
|
||||
final Logger _logger;
|
||||
final ConnectionMode _connectionMode;
|
||||
final bool _webUseIndexedDbIfSupported;
|
||||
final _mutex = ReadWriteMutex();
|
||||
|
||||
void _defaultLogHandler(LogRecord record) {
|
||||
@@ -68,11 +74,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 +96,7 @@ class StreamChatPersistenceClient extends ChatPersistenceClient {
|
||||
);
|
||||
}
|
||||
db = databaseProvider?.call(userId, _connectionMode) ??
|
||||
_defaultDatabaseProvider(userId, _connectionMode);
|
||||
await _defaultDatabaseProvider(userId, _connectionMode);
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
Reference in New Issue
Block a user