Skip to content

Commit 30a397e

Browse files
committed
Updates comments
1 parent 466875c commit 30a397e

File tree

3 files changed

+40
-21
lines changed

3 files changed

+40
-21
lines changed

lib/src/delayed_callback.dart

+14-7
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ final _random = Random();
66
/// Like [Future.delayed], but allows some control of the delay before the
77
/// callback execution.
88
final class DelayedCallback<T> {
9-
// Created a socket connection attempt whose delayFuture will complete after
10-
// the given delay.
9+
/// Executes the provided [callback] after [delay] elapses, unless aborted
10+
/// during the delay.
1111
DelayedCallback({
1212
required Duration delay,
1313
required Future<T> Function() callback,
@@ -28,6 +28,13 @@ final class DelayedCallback<T> {
2828
final Future<T> Function() _callback;
2929
bool _callbackRan = false;
3030
final _callbackCompleter = Completer<T>();
31+
32+
/// Returns a future that is completed with result of callback execution.
33+
///
34+
/// If the callback throws, then this future completes with the thrown error.
35+
///
36+
/// If the callback gets aborted before execution, the future is completed
37+
/// with a custom error.
3138
late final Future<T> callbackFuture = _callbackCompleter.future;
3239

3340
void _runCallback() {
@@ -44,8 +51,8 @@ final class DelayedCallback<T> {
4451
/// executed.
4552
bool get delayDone => !_delayTimer.isActive;
4653

47-
// Immediately skips delay and executes callback. Has no effect if the delay
48-
// has expired already.
54+
/// Immediately skips delay and executes callback. Has no effect if the delay
55+
/// has expired already, or if the callback was aborted.
4956
void skipDelay() {
5057
if (_delayTimer.isActive) {
5158
_delayTimer.cancel();
@@ -55,9 +62,9 @@ final class DelayedCallback<T> {
5562
}
5663
}
5764

58-
// Immediately completes delay with an error. The [callbackFuture] is going
59-
// to be completed with an error. Has no effect if the delay has expired
60-
// already.
65+
/// Aborts attempt at calling the callback. The [callbackFuture] is going to
66+
/// be completed with an error. Has no effect if the delay has expired
67+
/// already.
6168
void abort() {
6269
if (_delayTimer.isActive) {
6370
_delayTimer.cancel();

lib/src/socket.dart

+7-7
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class PhoenixSocket {
115115

116116
/// Whether the phoenix socket is ready to join channels. Note that this is
117117
/// not the same as the WebSocketConnected state, but rather is set to true
118-
/// when both web socket is connected, and the first heartbeat reply has been
118+
/// when both WebSocket is connected, and the first heartbeat reply has been
119119
/// received.
120120
bool get isOpen => _isOpen;
121121

@@ -140,15 +140,15 @@ class PhoenixSocket {
140140
/// Connects to the underlying WebSocket and prepares this PhoenixSocket for
141141
/// connecting to channels.
142142
///
143-
/// The returned future will complete as soon as the attempt to connect to a
144-
/// WebSocket is scheduled. If a WebSocket is connected or connecting, then
145-
/// it returns without any action.
146-
///
147-
/// To check whether the socket is ready for use, use [isOpen], or await on an
148-
/// event from [openStream].
143+
/// The returned future will complete when a connection is established and the
144+
/// first heartbeat round-trip completes.
149145
///
150146
/// If [immediately] is set to `true` and if a connection is not established,
151147
/// it will attempt to connect to a socket without delay.
148+
///
149+
/// If a connection is already open, then this method returns immediately. If
150+
/// you want to force a new connection, use [close] with reconnect parameter
151+
/// set to true.
152152
Future<void> connect({bool immediately = false}) async {
153153
if (_disposed) {
154154
throw StateError('PhoenixSocket cannot connect after being disposed.');

lib/src/socket_connection.dart

+19-7
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const forcedReconnectionRequested = 4002;
2121
/// necessary.
2222
class SocketConnectionManager {
2323
SocketConnectionManager({
24-
required Future<WebSocketChannel> Function() factory,
24+
required WebSocketChannelFactory factory,
2525
required List<Duration> reconnectDelays,
2626
required void Function(String) onMessage,
2727
required void Function(WebSocketConnectionState) onStateChange,
@@ -51,9 +51,11 @@ class SocketConnectionManager {
5151

5252
bool _disposed = false;
5353

54-
/// Requests to start connecting to the socket. Returns a future
54+
/// Requests to start connecting to the socket. Returns a future that
55+
/// completes when a WebSocket connection has been established.
5556
///
56-
/// Has no effect if the connection is already established.
57+
/// If [immediately] is false, and a connection is already established, then
58+
/// this method does not have any effect.
5759
///
5860
/// If [immediately] is set to true, then an attempt to connect is made
5961
/// immediately. This might result in dropping the current connection and
@@ -84,16 +86,26 @@ class SocketConnectionManager {
8486
/// Sends a message to the socket. Will start connecting to the socket if
8587
/// necessary.
8688
///
87-
/// Returns a future that completes when the message is successfully added to
88-
/// the socket.
89+
/// Returns a future that completes when the message is successfully passed to
90+
/// an established WebSocket.
8991
///
90-
/// If after call to [addMessage] a call to [dispose] or [stop] is made, then
91-
/// this future will complete with an error instead.
92+
/// The future will complete with error if the message couldn't be added to
93+
/// a WebSocket, either because this connection manager was disposed, or the
94+
/// WebSocket could not accept messages.
9295
Future<void> addMessage(String message) async {
9396
final connection = await _maybeConnect();
9497
return connection.send(message);
9598
}
9699

100+
/// Forces reconnection to the WebSocket. Returns a future that completes when
101+
/// a WebSocket connection has been established.
102+
///
103+
/// If a connection was already established, it gets closed with the provided
104+
/// code and optional reason.
105+
///
106+
/// If [immediately] is true, then the new connection attempt is executed
107+
/// immediately, irrespective of ordinary reconnection delays provided to the
108+
/// constructor.
97109
Future<void> reconnect(int code, {String? reason, bool immediately = false}) {
98110
final currentConnection = _connectionsStream.valueOrNull;
99111
final connectFuture = _connect();

0 commit comments

Comments
 (0)