Skip to content

Commit 61beb0d

Browse files
committed
migrate code to null safety
1 parent 8611283 commit 61beb0d

12 files changed

+208
-180
lines changed

lib/src/_stream_router.dart

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class _StreamRouter<T> {
3535
}
3636

3737
final Stream<T> _incoming;
38-
StreamSubscription<T> _subscription;
38+
late StreamSubscription<T> _subscription;
3939

4040
final List<_Route<T>> _routes = <_Route<T>>[];
4141
final StreamController<T> _defaultController =
@@ -44,7 +44,7 @@ class _StreamRouter<T> {
4444
/// Events that match [predicate] are sent to the stream created by this
4545
/// method, and not sent to any other router streams.
4646
Stream<T> route(_Predicate<T> predicate) {
47-
_Route route;
47+
_Route<T>? route;
4848
final controller = StreamController<T>.broadcast(onCancel: () {
4949
_routes.remove(route);
5050
});
@@ -62,8 +62,7 @@ class _StreamRouter<T> {
6262
}
6363

6464
void _handle(T event) {
65-
final route =
66-
_routes.firstWhere((r) => r.predicate(event), orElse: () => null);
65+
final route = _routes.firstWhereOrNull((r) => r.predicate(event));
6766
((route != null) ? route.controller : _defaultController).add(event);
6867
}
6968
}

lib/src/channel.dart

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,12 @@ class PhoenixChannel {
4343
/// Build a PhoenixChannel from a [PhoenixSocket].
4444
PhoenixChannel.fromSocket(
4545
this.socket, {
46-
this.topic,
47-
this.parameters = const {},
48-
Duration timeout,
46+
required this.topic,
47+
Map<String, dynamic>? parameters,
48+
Duration? timeout,
4949
}) : _controller = StreamController.broadcast(),
5050
_waiters = {},
51+
parameters = parameters ?? {},
5152
_timeout = timeout ?? socket.defaultTimeout {
5253
_joinPush = _prepareJoin();
5354
_logger = Logger('phoenix_socket.channel.$loggerName');
@@ -62,7 +63,7 @@ class PhoenixChannel {
6263
/// The [PhoenixSocket] through which this channel is established.
6364
final PhoenixSocket socket;
6465

65-
final StreamController<Message> _controller;
66+
final StreamController<Message > _controller;
6667
final Map<PhoenixChannelEvent, Completer<Message>> _waiters;
6768
final List<StreamSubscription> _subscriptions = [];
6869

@@ -71,11 +72,11 @@ class PhoenixChannel {
7172

7273
Duration _timeout;
7374
PhoenixChannelState _state = PhoenixChannelState.closed;
74-
Timer _rejoinTimer;
75+
Timer? _rejoinTimer;
7576
bool _joinedOnce = false;
76-
String _reference;
77-
Push _joinPush;
78-
Logger _logger;
77+
String? _reference;
78+
late Push _joinPush;
79+
late Logger _logger;
7980

8081
/// A list of push to be sent out once the channel is joined.
8182
final List<Push> pushBuffer = [];
@@ -93,7 +94,7 @@ class PhoenixChannel {
9394
bool get canPush =>
9495
socket.isConnected && _state == PhoenixChannelState.joined;
9596

96-
String _loggerName;
97+
String? _loggerName;
9798

9899
/// The name of the logger associated to this channel.
99100
String get loggerName => _loggerName ??= topic.replaceAll(
@@ -103,10 +104,7 @@ class PhoenixChannel {
103104
'_');
104105

105106
/// This channel's unique numeric reference.
106-
String get reference {
107-
_reference ??= socket.nextRef;
108-
return _reference;
109-
}
107+
String get reference => _reference ??= socket.nextRef;
110108

111109
/// Returns a future that will complete (or throw) when the provided
112110
/// reply arrives (or throws).
@@ -143,7 +141,7 @@ class PhoenixChannel {
143141
sub.cancel();
144142
}
145143

146-
_joinPush?.cancelTimeout();
144+
_joinPush.cancelTimeout();
147145

148146
_controller.close();
149147
_waiters.clear();
@@ -161,26 +159,32 @@ class PhoenixChannel {
161159
void triggerError(PhoenixException error) {
162160
_logger.fine('Receiving error on channel', error);
163161
if (!statesIgnoringErrors.contains(_state)) {
164-
trigger(error.message);
162+
if (error.message != null) {
163+
trigger(error.message!);
164+
}
165+
165166
_logger.warning('Got error on channel', error);
167+
166168
for (final waiter in _waiters.values) {
167169
waiter.completeError(error);
168170
}
169171
_waiters.clear();
172+
170173
final prevState = _state;
171174
_state = PhoenixChannelState.errored;
172175
if (prevState == PhoenixChannelState.joining) {
173176
_joinPush.reset();
174177
}
178+
175179
if (socket.isConnected) {
176180
_startRejoinTimer();
177181
}
178182
}
179183
}
180184

181185
/// Leave this channel.
182-
Push leave({Duration timeout}) {
183-
_joinPush?.cancelTimeout();
186+
Push leave({Duration? timeout}) {
187+
_joinPush.cancelTimeout();
184188
_rejoinTimer?.cancel();
185189

186190
final prevState = _state;
@@ -210,9 +214,10 @@ class PhoenixChannel {
210214
}
211215

212216
/// Join this channel using the associated [PhoenixSocket].
213-
Push join([Duration newTimeout]) {
217+
Push join([Duration? newTimeout]) {
214218
assert(!_joinedOnce);
215-
if (newTimeout is Duration) {
219+
220+
if (newTimeout != null) {
216221
_timeout = newTimeout;
217222
}
218223

@@ -238,7 +243,7 @@ class PhoenixChannel {
238243
/// Manually set timeout value for this push.
239244
///
240245
/// If not provided, the default timeout will be used.
241-
Duration newTimeout,
246+
Duration? newTimeout,
242247
]) =>
243248
pushEvent(
244249
PhoenixChannelEvent.custom(eventName),
@@ -253,7 +258,7 @@ class PhoenixChannel {
253258
Push pushEvent(
254259
PhoenixChannelEvent event,
255260
Map<String, dynamic> payload, [
256-
Duration newTimeout,
261+
Duration? newTimeout,
257262
]) {
258263
assert(_joinedOnce);
259264

@@ -290,7 +295,7 @@ class PhoenixChannel {
290295
];
291296
}
292297

293-
Push _prepareJoin([Duration providedTimeout]) {
298+
Push _prepareJoin([Duration? providedTimeout]) {
294299
final push = Push(
295300
this,
296301
event: PhoenixChannelEvent.join,
@@ -381,11 +386,12 @@ class PhoenixChannel {
381386
_controller.add(message.asReplyEvent());
382387
}
383388

384-
if (_waiters.containsKey(message.event)) {
389+
final waiter = _waiters[message.event];
390+
if (waiter != null) {
385391
_logger.finer(
386392
() => 'Notifying waiter for ${message.event}',
387393
);
388-
_waiters[message.event].complete(message);
394+
waiter.complete(message);
389395
} else {
390396
_logger.finer(() => 'No waiter to notify for ${message.event}');
391397
}

lib/src/events.dart

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ class PhoenixSocketCloseEvent extends PhoenixSocketEvent {
1818
});
1919

2020
/// The reason the socket was closed.
21-
final String reason;
21+
final String? reason;
2222

2323
/// The code of the socket close.
24-
final int code;
24+
final int? code;
2525
}
2626

2727
/// Error event for a [PhoenixSocket].
@@ -99,11 +99,8 @@ class PhoenixChannelEvent extends Equatable {
9999
/// The constant heartbeat event
100100
static PhoenixChannelEvent heartbeat = PhoenixChannelEvent._('heartbeat');
101101

102-
static Set<PhoenixChannelEvent> _statuses;
103-
104102
/// The constant set of possible internal channel event names.
105-
static Set<PhoenixChannelEvent> get statuses =>
106-
_statuses ??= {close, error, join, reply, leave};
103+
static Set<PhoenixChannelEvent> statuses = {close, error, join, reply, leave};
107104

108105
/// Whether the event name is an 'reply' event
109106
bool get isReply => value.startsWith(__chanReplyEventName);

lib/src/exceptions.dart

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,28 @@ class PhoenixException implements Exception {
1313
});
1414

1515
/// The associated error event.
16-
final PhoenixSocketErrorEvent socketError;
16+
final PhoenixSocketErrorEvent? socketError;
1717

1818
/// The associated close event.
19-
final PhoenixSocketCloseEvent socketClosed;
19+
final PhoenixSocketCloseEvent? socketClosed;
2020

2121
/// The associated channel event.
22-
final String channelEvent;
22+
final String? channelEvent;
2323

2424
/// The error message for this exception.
25-
Message get message {
26-
if (socketClosed is PhoenixSocketCloseEvent) {
25+
Message? get message {
26+
if (socketClosed != null) {
2727
return Message(event: PhoenixChannelEvent.error);
28-
} else if (socketError is PhoenixSocketErrorEvent) {
28+
} else if (socketError != null) {
2929
return Message(event: PhoenixChannelEvent.error);
3030
}
3131
return null;
3232
}
3333

3434
@override
3535
String toString() {
36-
if (socketError is PhoenixSocketErrorEvent) {
37-
return socketError.error.toString();
36+
if (socketError != null) {
37+
return socketError!.error.toString();
3838
} else {
3939
return 'PhoenixException: socket closed';
4040
}

lib/src/message.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,32 +50,32 @@ class Message implements Equatable {
5050
this.joinRef,
5151
this.ref,
5252
this.topic,
53-
this.event,
53+
required this.event,
5454
this.payload,
5555
}) : assert(event is PhoenixChannelEvent);
5656

5757
/// Reference of the channel on which the message is received.
5858
///
5959
/// Used by the [PhoenixSocket] to route the message on the proper
6060
/// [PhoenixChannel].
61-
final String joinRef;
61+
final String? joinRef;
6262

6363
/// The unique identifier for this message.
6464
///
6565
/// This identifier is used in the reply event name, allowing us
6666
/// to consider a message as a reply to a previous message.
67-
final String ref;
67+
final String? ref;
6868

6969
/// The topic of the channel on which this message is sent.
70-
final String topic;
70+
final String? topic;
7171

7272
/// The event name of this message.
7373
final PhoenixChannelEvent event;
7474

7575
/// The payload of this message.
7676
///
7777
/// This needs to be a JSON-encodable object.
78-
final Map<String, dynamic> payload;
78+
final Map<String, dynamic>? payload;
7979

8080
/// Encode a message to a JSON-encodable list of values.
8181
Object encode() {
@@ -91,7 +91,7 @@ class Message implements Equatable {
9191
}
9292

9393
@override
94-
List<Object> get props => [joinRef, ref, topic, event, payload];
94+
List<Object?> get props => [joinRef, ref, topic, event, payload];
9595

9696
@override
9797
bool get stringify => true;
@@ -105,7 +105,7 @@ class Message implements Equatable {
105105
return Message(
106106
ref: ref,
107107
payload: payload,
108-
event: PhoenixChannelEvent.replyFor(ref),
108+
event: PhoenixChannelEvent.replyFor(ref!),
109109
topic: topic,
110110
joinRef: joinRef,
111111
);

lib/src/message_serializer.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class MessageSerializer {
99
/// Default constructor returning the singleton instance of this class.
1010
factory MessageSerializer() => _instance ??= MessageSerializer._();
1111

12-
static MessageSerializer _instance;
12+
static MessageSerializer? _instance;
1313

1414
/// Yield a [Message] from some raw string arriving from a websocket.
1515
Message decode(String rawData) {

0 commit comments

Comments
 (0)