Merge pull request #1557 from GetStream/feat/connection-status

This commit is contained in:
Sahil Kumar
2023-05-19 01:50:33 +05:30
committed by GitHub
6 changed files with 27 additions and 9 deletions
+6
View File
@@ -1,3 +1,9 @@
## Upcoming
✅ Added
- Added support for `ChatPersistenceClient.isConnected` for checking if the client is connected to the database.
## 6.1.0 ## 6.1.0
🐞 Fixed 🐞 Fixed
@@ -14,6 +14,9 @@ import 'package:stream_chat/src/core/util/extension.dart';
/// A simple client used for persisting chat data locally. /// A simple client used for persisting chat data locally.
abstract class ChatPersistenceClient { abstract class ChatPersistenceClient {
/// Whether the connection is established.
bool get isConnected;
/// Creates a new connection to the client /// Creates a new connection to the client
Future<void> connect(String userId); Future<void> connect(String userId);
@@ -12,6 +12,9 @@ import 'package:stream_chat/src/db/chat_persistence_client.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
class TestPersistenceClient extends ChatPersistenceClient { class TestPersistenceClient extends ChatPersistenceClient {
@override
bool get isConnected => throw UnimplementedError();
@override @override
Future<void> connect(String userId) => throw UnimplementedError(); Future<void> connect(String userId) => throw UnimplementedError();
@@ -1,3 +1,7 @@
## Upcoming
- Added support for `StreamChatPersistenceClient.isConnected` for checking if the client is connected to the database.
## 6.1.0 ## 6.1.0
- Updated `dart` sdk environment range to support `3.0.0`. - Updated `dart` sdk environment range to support `3.0.0`.
@@ -58,7 +58,7 @@ class StreamChatPersistenceClient extends ChatPersistenceClient {
bool get _debugIsConnected { bool get _debugIsConnected {
assert(() { assert(() {
if (db == null) { if (!isConnected) {
throw StateError(''' throw StateError('''
$runtimeType hasn't been connected yet or used after `disconnect` $runtimeType hasn't been connected yet or used after `disconnect`
was called. Consider calling `connect` to create a connection. was called. Consider calling `connect` to create a connection.
@@ -79,6 +79,9 @@ class StreamChatPersistenceClient extends ChatPersistenceClient {
webUseIndexedDbIfSupported: _webUseIndexedDbIfSupported, webUseIndexedDbIfSupported: _webUseIndexedDbIfSupported,
); );
@override
bool get isConnected => db != null;
@override @override
Future<void> connect( Future<void> connect(
String userId, { String userId, {
@@ -405,7 +408,7 @@ class StreamChatPersistenceClient extends ChatPersistenceClient {
@override @override
Future<void> disconnect({bool flush = false}) async { Future<void> disconnect({bool flush = false}) async {
_logger.info('disconnect'); _logger.info('disconnect');
if (db != null) { if (isConnected) {
_logger.info('Disconnecting'); _logger.info('Disconnecting');
if (flush) { if (flush) {
_logger.info('Flushing'); _logger.info('Flushing');
@@ -16,9 +16,9 @@ void main() {
const userId = 'testUserId'; const userId = 'testUserId';
test('successfully connects with the Database', () async { test('successfully connects with the Database', () async {
final client = StreamChatPersistenceClient(logLevel: Level.ALL); final client = StreamChatPersistenceClient(logLevel: Level.ALL);
expect(client.db, isNull); expect(client.isConnected, false);
await client.connect(userId, databaseProvider: testDatabaseProvider); await client.connect(userId, databaseProvider: testDatabaseProvider);
expect(client.db, isNotNull); expect(client.isConnected, true);
expect(client.db, isA<DriftChatDatabase>()); expect(client.db, isA<DriftChatDatabase>());
expect(client.db!.userId, userId); expect(client.db!.userId, userId);
@@ -29,10 +29,9 @@ void main() {
test('throws if already connected', () async { test('throws if already connected', () async {
final client = StreamChatPersistenceClient(logLevel: Level.ALL); final client = StreamChatPersistenceClient(logLevel: Level.ALL);
expect(client.db, isNull); expect(client.isConnected, false);
await client.connect(userId, databaseProvider: testDatabaseProvider); await client.connect(userId, databaseProvider: testDatabaseProvider);
expect(client.db, isNotNull); expect(client.isConnected, true);
expect(client.db, isNotNull);
expect(client.db, isA<DriftChatDatabase>()); expect(client.db, isA<DriftChatDatabase>());
expect(client.db!.userId, userId); expect(client.db!.userId, userId);
expect( expect(
@@ -50,9 +49,9 @@ void main() {
const userId = 'testUserId'; const userId = 'testUserId';
final client = StreamChatPersistenceClient(logLevel: Level.ALL); final client = StreamChatPersistenceClient(logLevel: Level.ALL);
await client.connect(userId, databaseProvider: testDatabaseProvider); await client.connect(userId, databaseProvider: testDatabaseProvider);
expect(client.db, isNotNull); expect(client.isConnected, true);
await client.disconnect(flush: true); await client.disconnect(flush: true);
expect(client.db, isNull); expect(client.isConnected, false);
}); });
test('client function throws stateError if db is not yet connected', () { test('client function throws stateError if db is not yet connected', () {