@@ -7,10 +7,12 @@ import 'package:ws/src/client/state_stream.dart';
7
7
import 'package:ws/src/client/ws_client_interface.dart' ;
8
8
import 'package:ws/src/client/ws_options.dart' ;
9
9
10
+ // coverage:ignore-start
10
11
/// {@nodoc}
11
12
@internal
12
13
IWebSocketClient $platformWebSocketClient (WebSocketOptions ? options) =>
13
14
WebSocketClientFake (protocols: options? .protocols);
15
+ // coverage:ignore-end
14
16
15
17
/// {@template ws_client_fake}
16
18
/// Fake WebSocket client for testing purposes.
@@ -24,19 +26,22 @@ final class WebSocketClientFake implements IWebSocketClient {
24
26
: protocols = protocols? .toList (),
25
27
_controller = StreamController <Object >.broadcast (),
26
28
_stateController = StreamController <WebSocketClientState >.broadcast (),
27
- isClosed = false ;
29
+ _isClosed = false ,
30
+ _state = WebSocketClientState .initial ();
28
31
29
32
/// {@nodoc}
30
33
@visibleForTesting
31
34
final List <String >? protocols;
32
35
33
36
@override
34
37
@visibleForTesting
35
- bool isClosed;
38
+ bool get isClosed => _isClosed;
39
+ bool _isClosed;
36
40
37
41
@override
38
42
@visibleForTesting
39
- WebSocketClientState state = WebSocketClientState .initial ();
43
+ WebSocketClientState get state => _state;
44
+ WebSocketClientState _state;
40
45
41
46
final StreamController <WebSocketClientState > _stateController;
42
47
@@ -64,28 +69,29 @@ final class WebSocketClientFake implements IWebSocketClient {
64
69
@override
65
70
@visibleForTesting
66
71
FutureOr <void > connect (String url) async {
67
- _stateController.add (state = WebSocketClientState .connecting (url: url));
72
+ _stateController.add (_state = WebSocketClientState .connecting (url: url));
68
73
await Future <void >.delayed (const Duration (milliseconds: 25 ));
69
- _stateController.add (state = WebSocketClientState .open (url: url));
74
+ _stateController.add (_state = WebSocketClientState .open (url: url));
70
75
}
71
76
72
77
@override
73
78
@visibleForTesting
74
79
FutureOr <void > disconnect (
75
80
[int ? code = 1000 , String ? reason = 'NORMAL_CLOSURE' ]) async {
76
- _stateController.add (state = WebSocketClientState .disconnecting (
81
+ _stateController.add (_state = WebSocketClientState .disconnecting (
77
82
closeCode: code, closeReason: reason));
78
83
await Future <void >.delayed (const Duration (milliseconds: 25 ));
79
- _stateController.add (state =
84
+ _stateController.add (_state =
80
85
WebSocketClientState .closed (closeCode: code, closeReason: reason));
81
86
}
82
87
83
88
@override
84
89
@visibleForTesting
85
90
FutureOr <void > close (
86
91
[int ? code = 1000 , String ? reason = 'NORMAL_CLOSURE' ]) async {
92
+ _isClosed = true ;
87
93
await disconnect (code, reason);
88
- isClosed = true ;
94
+ await Future < void >. delayed ( Duration .zero) ;
89
95
await _stateController.close ();
90
96
await _controller.close ();
91
97
}
0 commit comments