[LLC] Remove flutter dependency

Signed-off-by: Sahil Kumar <[email protected]>
This commit is contained in:
Sahil Kumar
2021-01-28 16:40:36 +05:30
parent f162b006ce
commit c87c1d5368
5 changed files with 45 additions and 35 deletions
@@ -1,4 +1,4 @@
import 'package:flutter/foundation.dart'; import 'package:meta/meta.dart';
import 'package:stream_chat/src/client.dart'; import 'package:stream_chat/src/client.dart';
import 'package:stream_chat/src/exceptions.dart'; import 'package:stream_chat/src/exceptions.dart';
@@ -1,8 +1,7 @@
import 'dart:async'; import 'dart:async';
import 'package:collection/collection.dart'; import 'package:collection/collection.dart';
import 'package:flutter/cupertino.dart'; import 'package:meta/meta.dart';
import 'package:flutter/foundation.dart';
import 'package:logging/logging.dart'; import 'package:logging/logging.dart';
import 'package:stream_chat/src/api/channel.dart'; import 'package:stream_chat/src/api/channel.dart';
import 'package:stream_chat/src/api/retry_policy.dart'; import 'package:stream_chat/src/api/retry_policy.dart';
+16 -10
View File
@@ -2,9 +2,9 @@ import 'dart:async';
import 'dart:convert'; import 'dart:convert';
import 'dart:math'; import 'dart:math';
import 'package:flutter/foundation.dart'; import 'package:meta/meta.dart';
import 'package:flutter/material.dart';
import 'package:logging/logging.dart'; import 'package:logging/logging.dart';
import 'package:rxdart/rxdart.dart';
import 'package:web_socket_channel/web_socket_channel.dart'; import 'package:web_socket_channel/web_socket_channel.dart';
import '../models/event.dart'; import '../models/event.dart';
@@ -99,9 +99,15 @@ class WebSocket {
/// The timeout that uses the reconnection monitor timer to consider the connection unhealthy /// The timeout that uses the reconnection monitor timer to consider the connection unhealthy
final int reconnectionMonitorTimeout; final int reconnectionMonitorTimeout;
final _connectionStatusController =
BehaviorSubject.seeded(ConnectionStatus.disconnected);
set _connectionStatus(ConnectionStatus status) =>
_connectionStatusController.add(status);
/// This notifies of connection status changes /// This notifies of connection status changes
final ValueNotifier<ConnectionStatus> connectionStatus = Stream<ConnectionStatus> get connectionStatusStream =>
ValueNotifier(ConnectionStatus.disconnected); _connectionStatusController.stream;
String _path; String _path;
int _retryAttempt = 1; int _retryAttempt = 1;
@@ -128,7 +134,7 @@ class WebSocket {
} }
_connecting = true; _connecting = true;
connectionStatus.value = ConnectionStatus.connecting; _connectionStatus = ConnectionStatus.connecting;
logger.info('connecting to $_path'); logger.info('connecting to $_path');
@@ -175,7 +181,7 @@ class WebSocket {
_reconnecting = false; _reconnecting = false;
_lastEventAt = DateTime.now(); _lastEventAt = DateTime.now();
connectionStatus.value = ConnectionStatus.connected; _connectionStatus = ConnectionStatus.connected;
_retryAttempt = 1; _retryAttempt = 1;
if (!_connectionCompleter.isCompleted) { if (!_connectionCompleter.isCompleted) {
@@ -199,7 +205,7 @@ class WebSocket {
_connecting = false; _connecting = false;
if (!_reconnecting) { if (!_reconnecting) {
connectionStatus.value = ConnectionStatus.disconnected; _connectionStatus = ConnectionStatus.disconnected;
} }
if (!_connectionCompleter.isCompleted) { if (!_connectionCompleter.isCompleted) {
@@ -258,7 +264,7 @@ class WebSocket {
logger.info('reconnect'); logger.info('reconnect');
if (!_reconnecting) { if (!_reconnecting) {
_reconnecting = true; _reconnecting = true;
connectionStatus.value = ConnectionStatus.connecting; _connectionStatus = ConnectionStatus.connecting;
} }
_reconnectTimer(); _reconnectTimer();
@@ -300,8 +306,8 @@ class WebSocket {
_cancelTimers(); _cancelTimers();
_reconnecting = false; _reconnecting = false;
_manuallyDisconnected = true; _manuallyDisconnected = true;
connectionStatus.value = ConnectionStatus.disconnected; _connectionStatus = ConnectionStatus.disconnected;
connectionStatus.dispose(); await _connectionStatusController.close();
return _channel.sink.close(); return _channel.sink.close();
} }
} }
+27 -19
View File
@@ -3,7 +3,7 @@ import 'dart:convert';
import 'dart:io'; import 'dart:io';
import 'package:dio/dio.dart'; import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart'; import 'package:meta/meta.dart';
import 'package:logging/logging.dart'; import 'package:logging/logging.dart';
import 'package:rxdart/rxdart.dart'; import 'package:rxdart/rxdart.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
@@ -93,8 +93,6 @@ class Client {
this.backgroundKeepAlive = const Duration(minutes: 1), this.backgroundKeepAlive = const Duration(minutes: 1),
RetryPolicy retryPolicy, RetryPolicy retryPolicy,
}) { }) {
WidgetsFlutterBinding.ensureInitialized();
_retryPolicy ??= RetryPolicy( _retryPolicy ??= RetryPolicy(
retryTimeout: (Client client, int attempt, ApiError error) => retryTimeout: (Client client, int attempt, ApiError error) =>
Duration(seconds: 1 * attempt), Duration(seconds: 1 * attempt),
@@ -180,7 +178,8 @@ class Client {
static const _defaultBaseURL = 'chat-us-east-1.stream-io-api.com'; static const _defaultBaseURL = 'chat-us-east-1.stream-io-api.com';
static const _tokenExpiredErrorCode = 40; static const _tokenExpiredErrorCode = 40;
VoidCallback _connectionStatusListener; StreamSubscription<ConnectionStatus> _connectionStatusSubscription;
Future<void> Function(ConnectionStatus) _connectionStatusHandler;
final BehaviorSubject<Event> _controller = BehaviorSubject<Event>(); final BehaviorSubject<Event> _controller = BehaviorSubject<Event>();
@@ -188,10 +187,20 @@ class Client {
/// Listen to this or use the [on] method to filter specific event types /// Listen to this or use the [on] method to filter specific event types
Stream<Event> get stream => _controller.stream; Stream<Event> get stream => _controller.stream;
final _wsConnectionStatusController =
BehaviorSubject.seeded(ConnectionStatus.disconnected);
set _wsConnectionStatus(ConnectionStatus status) =>
_wsConnectionStatusController.add(status);
/// The current status value of the websocket connection
ConnectionStatus get wsConnectionStatus =>
_wsConnectionStatusController.value;
/// This notifies the connection status of the websocket connection. /// This notifies the connection status of the websocket connection.
/// Listen to this to get notified when the websocket tries to reconnect. /// Listen to this to get notified when the websocket tries to reconnect.
final ValueNotifier<ConnectionStatus> wsConnectionStatus = Stream<ConnectionStatus> get wsConnectionStatusStream =>
ValueNotifier(ConnectionStatus.disconnected); _wsConnectionStatusController.stream;
/// The current user token /// The current user token
String token; String token;
@@ -275,8 +284,6 @@ class Client {
httpClient.lock(); httpClient.lock();
final userId = state.user.id; final userId = state.user.id;
_ws.connectionStatus.removeListener(_connectionStatusListener);
await _disconnect(); await _disconnect();
final newToken = await tokenProvider(userId); final newToken = await tokenProvider(userId);
@@ -345,8 +352,8 @@ class Client {
await _disconnect(); await _disconnect();
httpClient.close(); httpClient.close();
await _controller.close(); await _controller.close();
state.channels.values.forEach((c) => c.dispose());
state.dispose(); state.dispose();
await _wsConnectionStatusController.close();
} }
Map<String, String> get _httpHeaders => { Map<String, String> get _httpHeaders => {
@@ -443,17 +450,17 @@ class Client {
/// Connect the client websocket /// Connect the client websocket
Future<Event> connect() async { Future<Event> connect() async {
logger.info('connecting'); logger.info('connecting');
if (wsConnectionStatus.value == ConnectionStatus.connecting) { if (wsConnectionStatus == ConnectionStatus.connecting) {
logger.warning('Already connecting'); logger.warning('Already connecting');
throw Exception('Already connecting'); throw Exception('Already connecting');
} }
if (wsConnectionStatus.value == ConnectionStatus.connected) { if (wsConnectionStatus == ConnectionStatus.connected) {
logger.warning('Already connected'); logger.warning('Already connected');
throw Exception('Already connected'); throw Exception('Already connected');
} }
wsConnectionStatus.value = ConnectionStatus.connecting; _wsConnectionStatus = ConnectionStatus.connecting;
if (persistenceEnabled) { if (persistenceEnabled) {
await chatPersistenceClient.connect(state.user.id); await chatPersistenceClient.connect(state.user.id);
@@ -476,15 +483,14 @@ class Client {
logger: _detachedLogger('🔌'), logger: _detachedLogger('🔌'),
); );
_connectionStatusListener = () async { _connectionStatusHandler = (ConnectionStatus status) async {
final value = _ws.connectionStatus.value; _wsConnectionStatus = status;
wsConnectionStatus.value = value;
handleEvent(Event( handleEvent(Event(
type: EventType.connectionChanged, type: EventType.connectionChanged,
online: value == ConnectionStatus.connected, online: status == ConnectionStatus.connected,
)); ));
if (value == ConnectionStatus.connected && if (status == ConnectionStatus.connected &&
state.channels?.isNotEmpty == true) { state.channels?.isNotEmpty == true) {
unawaited(queryChannels(filter: { unawaited(queryChannels(filter: {
'cid': { 'cid': {
@@ -504,7 +510,8 @@ class Client {
} }
}; };
_ws.connectionStatus.addListener(_connectionStatusListener); _connectionStatusSubscription =
_ws.connectionStatusStream.listen(_connectionStatusHandler);
var event = await chatPersistenceClient?.getConnectionInfo(); var event = await chatPersistenceClient?.getConnectionInfo();
@@ -588,7 +595,7 @@ class Client {
logger.info('awaiting connection completer'); logger.info('awaiting connection completer');
await _connectCompleter.future; await _connectCompleter.future;
} }
if (wsConnectionStatus.value != ConnectionStatus.connected) { if (wsConnectionStatus != ConnectionStatus.connected) {
final errorMessage = final errorMessage =
'You cannot use queryChannels without an active connection. Please call setUser to connect the client.'; 'You cannot use queryChannels without an active connection. Please call setUser to connect the client.';
if (persistenceEnabled) { if (persistenceEnabled) {
@@ -946,6 +953,7 @@ class Client {
logger.info('Client disconnecting'); logger.info('Client disconnecting');
await _ws?.disconnect(); await _ws?.disconnect();
await _connectionStatusSubscription?.cancel();
} }
/// Requests users with a given query. /// Requests users with a given query.
-3
View File
@@ -9,8 +9,6 @@ environment:
sdk: ">=2.7.0 <3.0.0" sdk: ">=2.7.0 <3.0.0"
dependencies: dependencies:
flutter:
sdk: flutter
json_annotation: ^3.0.1 json_annotation: ^3.0.1
shared_preferences: ^0.5.7+3 shared_preferences: ^0.5.7+3
logging: ^0.11.4 logging: ^0.11.4
@@ -21,7 +19,6 @@ dependencies:
rxdart: ^0.24.1 rxdart: ^0.24.1
collection: ^1.14.12 collection: ^1.14.12
pedantic: ^1.9.2 pedantic: ^1.9.2
freezed: ^0.12.7
dev_dependencies: dev_dependencies:
build_runner: ^1.10.0 build_runner: ^1.10.0