Эх сурвалжийг харах

JsonRpcNotificationListener-running

melon.yin 2 жил өмнө
parent
commit
de03818ef5

+ 12 - 9
lib/listener/listener.dart

@@ -30,7 +30,7 @@ class JsonRpcNotificationListener {
   /// A sign for relating to the target communication. It's [nullable].
   final String? token;
 
-  WebSocketChannel? channel;
+  WebSocketChannel? _channel;
 
   final _handlersMap =
       <NotificationTypeEnum, Set<JsonRpcNotificationHandlerBase>>{};
@@ -42,10 +42,13 @@ class JsonRpcNotificationListener {
   List<JsonRpcNotificationHandlerBase> get handlers =>
       _handlersMap.entries.expand((e) => e.value).toList();
 
+  /// Status of running.
+  bool get running => _channel != null && _channel!.closeCode == null;
+
   /// Start listen remote notifications.
   void run() {
-    channel = WebSocketChannel.connect(Uri.parse(_buildConnectUrl()));
-    channel!.stream.listen(
+    _channel = WebSocketChannel.connect(Uri.parse(_buildConnectUrl()));
+    _channel!.stream.listen(
       (data) {
         final message = _parseMessageData(data);
         if (message != null) {
@@ -87,18 +90,18 @@ class JsonRpcNotificationListener {
 
   /// Close and dispose connection/other resources.
   void close() {
-    if (channel != null) {
-      if (channel!.closeCode != null) {
-        channel!.sink.close(wsStatus.normalClosure);
+    if (_channel != null) {
+      if (_channel!.closeCode != null) {
+        _channel!.sink.close(wsStatus.normalClosure);
       }
-      channel = null;
+      _channel = null;
     }
   }
 
   void _retry(dynamic error) {
     // https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent/code
-    channel?.sink.close(1013); // code: try again later.
-    channel = null;
+    _channel?.sink.close(1013); // code: try again later.
+    _channel = null;
 
     if (_retryLimit > _retryCount) {
       Future.delayed(const Duration(seconds: 1), () {