Skip to content

Commit 9127f5c

Browse files
authored
Merge pull request #54 from Workiva/null_safety
Null safety
2 parents 8ba8bf8 + 8eaa324 commit 9127f5c

File tree

6 files changed

+45
-60
lines changed

6 files changed

+45
-60
lines changed

lib/src/client.dart

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class SockJSClient extends Disposable {
4343
String get disposableTypeName => 'SockJSClient';
4444

4545
// The native SockJS client object.
46-
js_interop.SockJS _jsClient;
46+
late js_interop.SockJS _jsClient;
4747

4848
// Event stream controllers.
4949
final StreamController<SockJSCloseEvent> _onCloseController =
@@ -70,7 +70,7 @@ class SockJSClient extends Disposable {
7070
/// final options = new SockJSOptions(
7171
/// transports: ['websocket', 'xhr-streaming', 'xhr-polling']);
7272
/// final client = new SockJSClient(uri, options: options);
73-
SockJSClient(Uri uri, {SockJSOptions options}) {
73+
SockJSClient(Uri uri, {SockJSOptions? options}) {
7474
try {
7575
_jsClient = js_interop.SockJS(uri.toString(), null, options?._toJs());
7676
// ignore: avoid_catches_without_on_clauses
@@ -120,7 +120,7 @@ class SockJSClient extends Disposable {
120120
/// Close this client.
121121
///
122122
/// Optionally, a [closeCode] and [reason] can be provided.
123-
void close([int closeCode, String reason]) {
123+
void close([int? closeCode, String? reason]) {
124124
_jsClient.close(closeCode, reason);
125125
}
126126

@@ -154,21 +154,17 @@ class SockJSClient extends Disposable {
154154

155155
void _onClose(js_interop.SockJSCloseEvent event) {
156156
_onCloseController.add(SockJSCloseEvent(
157-
// ignore: avoid_as
158-
event.code,
159-
// ignore: avoid_as
160-
event.reason,
161-
// ignore: avoid_as
162-
wasClean: event.wasClean));
157+
event.code,
158+
event.reason,
159+
wasClean: event.wasClean,
160+
));
163161
}
164162

165163
void _onMessage(js_interop.SockJSMessageEvent event) {
166-
// ignore: avoid_as
167164
_onMessageController.add(SockJSMessageEvent(event.data));
168165
}
169166

170-
// ignore: avoid_annotating_with_dynamic
171-
void _onOpen(dynamic _) {
167+
void _onOpen(_) {
172168
_onOpenController
173169
.add(SockJSOpenEvent(_jsClient.transport, Uri.parse(_jsClient.url)));
174170
}
@@ -179,13 +175,13 @@ class SockJSOptions {
179175
/// String to append to url for actual data connection.
180176
///
181177
/// Defaults to a random 4 digit number.
182-
final String server;
178+
final String? server;
183179

184180
/// A list of transports that may be used by SockJS.
185181
///
186182
/// By default, all available transports will be used. Specifying a whitelist
187183
/// can be useful if you need to disable certain fallback transports.
188-
final List<String> transports;
184+
final List<String>? transports;
189185

190186
/// Construct a [SockJSOptions] instance to be passed to the [SockJSClient]
191187
/// constructor.

lib/src/events.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ class SockJSCloseEvent extends SockJSEvent {
3232
final bool wasClean;
3333

3434
/// Constructs a [SockJSCloseEvent].
35-
SockJSCloseEvent(this.code, this.reason, {this.wasClean}) : super._('close');
35+
SockJSCloseEvent(this.code, this.reason, {required this.wasClean})
36+
: super._('close');
3637
}
3738

3839
/// Event that represents a message received by a SockJS Client from the server.

lib/src/js_interop.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ bool get hasSockJS => context['SockJS'] != null;
2626
@JS()
2727
class SockJS {
2828
/// Constructs a SockJS Client.
29-
external SockJS(String url, [Null _reserved, SockJSOptions options]);
29+
external SockJS(String url, [Null _reserved, SockJSOptions? options]);
3030

3131
/// The state of the client.
3232
///
@@ -50,7 +50,7 @@ class SockJS {
5050
external void addEventListener(String eventName, Function callback);
5151

5252
/// Closes this client.
53-
external void close([int closeCode, String reason]);
53+
external void close([int? closeCode, String? reason]);
5454

5555
/// Removes an event listener that was registered with [addEventListener].
5656
external void removeEventListener(String eventName, Function callback);
@@ -93,7 +93,7 @@ class SockJSOptions {
9393
/// Example:
9494
///
9595
/// {server: 'foo', transports: ['websocket', 'xhr-polling']}
96-
external factory SockJSOptions({String server, List<String> transports});
96+
external factory SockJSOptions({String? server, List<String>? transports});
9797

9898
/// String to append to url for actual data connection.
9999
///

pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: A Dart wrapper for the `sockjs-client` JS library.
44
homepage: https://github.com/Workiva/sockjs_client_wrapper
55

66
environment:
7-
sdk: ">=2.11.0 <3.0.0"
7+
sdk: '>=2.12.0 <3.0.0'
88

99
dependencies:
1010
js: ^0.6.1
@@ -17,4 +17,4 @@ dev_dependencies:
1717
dart_dev: '>=3.6.5 <5.0.0'
1818
dependency_validator: ^3.2.0
1919
test: ^1.15.7
20-
workiva_analysis_options: ^1.1.0
20+
workiva_analysis_options: ^1.3.0

test/sockjs_client_wrapper_test.dart

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -105,74 +105,74 @@ void _integrationSuite(
105105
SockJSClient createCorClient(),
106106
SockJSClient create404Client(),
107107
) {
108-
SockJSClient client;
108+
SockJSClient? client;
109109

110110
tearDown(() async {
111-
await client.dispose();
111+
await client!.dispose();
112112
client = null;
113113
});
114114

115115
test('connecting to a SockJS server', () async {
116116
client = createEchoClient();
117-
expect((await client.onOpen.first).transport, equals(expectedTransport));
117+
expect((await client!.onOpen.first).transport, equals(expectedTransport));
118118
});
119119

120120
test('sending and receiving messages', () async {
121121
client = createEchoClient();
122-
await client.onOpen.first;
122+
await client!.onOpen.first;
123123

124124
final c = Completer<Null>();
125125
final echos = <String>[];
126-
client.onMessage.listen((message) {
126+
client!.onMessage.listen((message) {
127127
echos.add(message.data);
128128
if (echos.length == 2) {
129129
c.complete();
130130
}
131131
});
132-
client
132+
client!
133133
..send('message1')
134134
..send('message2');
135135

136136
await c.future;
137-
client.close();
137+
client!.close();
138138

139139
expect(echos, equals(['message1', 'message2']));
140140
});
141141

142142
test('client closing the connection', () async {
143143
client = createEchoClient();
144-
await client.onOpen.first;
145-
client.close();
146-
await client.onClose.first;
144+
await client!.onOpen.first;
145+
client!.close();
146+
await client!.onClose.first;
147147
});
148148

149149
test('client closing the connection with code and reason', () async {
150150
client = createEchoClient();
151-
await client.onOpen.first;
152-
client.close(4001, 'Custom close.');
153-
final event = await client.onClose.first;
151+
await client!.onOpen.first;
152+
client!.close(4001, 'Custom close.');
153+
final event = await client!.onClose.first;
154154
expect(event.code, equals(4001));
155155
expect(event.reason, equals('Custom close.'));
156156
});
157157

158158
test('server closing the connection', () async {
159159
client = createCorClient();
160-
await client.onOpen.first;
161-
client.send('close');
162-
await client.onClose.first;
160+
await client!.onOpen.first;
161+
client!.send('close');
162+
await client!.onClose.first;
163163
});
164164

165165
test('server closing the connection with code and reason', () async {
166166
client = createCorClient();
167-
await client.onOpen.first;
168-
client.send('close::4001::Custom close.');
169-
final event = await client.onClose.first;
167+
await client!.onOpen.first;
168+
client!.send('close::4001::Custom close.');
169+
final event = await client!.onClose.first;
170170
expect(event.code, equals(4001));
171171
expect(event.reason, equals('Custom close.'));
172172
});
173173

174174
test('handle failed connection', () async {
175175
client = create404Client();
176-
await client.onClose.first;
176+
await client!.onClose.first;
177177
});
178178
}

tool/dart_dev/config.dart

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,51 +30,39 @@ final config = {
3030
..addTool(DevTool.fromFunction(stopExampleServer), alwaysRun: true),
3131
};
3232

33-
Process _exampleServer;
34-
Process _testServer;
33+
Process? _exampleServer;
34+
Process? _testServer;
3535

3636
Future<int> startExampleServer(DevToolExecutionContext _) async {
3737
_exampleServer = await Process.start('node', ['example/server.js'],
3838
mode: ProcessStartMode.inheritStdio);
39-
return firstOf([
39+
return Future.any([
4040
// Exit early if it fails to start,
41-
_exampleServer.exitCode,
41+
_exampleServer!.exitCode,
4242
// otherwise just wait a little bit to ensure it starts up completely.
4343
Future.delayed(Duration(seconds: 2)).then((_) => 0),
4444
]);
4545
}
4646

4747
Future<int> stopExampleServer(DevToolExecutionContext _) async {
4848
_exampleServer?.kill();
49-
await _exampleServer.exitCode;
49+
await _exampleServer?.exitCode;
5050
return 0;
5151
}
5252

5353
Future<int> startTestServer(DevToolExecutionContext _) async {
5454
_testServer = await Process.start('node', ['tool/server.js'],
5555
mode: ProcessStartMode.inheritStdio);
56-
return firstOf([
56+
return Future.any([
5757
// Exit early if it fails to start,
58-
_testServer.exitCode,
58+
_testServer!.exitCode,
5959
// otherwise just wait a little bit to ensure it starts up completely.
6060
Future.delayed(Duration(seconds: 2)).then((_) => 0),
6161
]);
6262
}
6363

6464
Future<int> stopTestServer(DevToolExecutionContext _) async {
6565
_testServer?.kill();
66-
await _testServer.exitCode;
66+
await _testServer?.exitCode;
6767
return 0;
6868
}
69-
70-
Future<T> firstOf<T>(Iterable<Future<T>> futures) {
71-
final c = Completer<T>();
72-
for (final future in futures) {
73-
future.then((v) {
74-
if (!c.isCompleted) {
75-
c.complete(v);
76-
}
77-
}).catchError(c.completeError);
78-
}
79-
return c.future;
80-
}

0 commit comments

Comments
 (0)