Skip to content

Commit 299b9f8

Browse files
committed
clean-up
1 parent d27436e commit 299b9f8

File tree

1 file changed

+13
-18
lines changed

1 file changed

+13
-18
lines changed

tictactoe/lib/src/concurrent_game_engine.dart

+13-18
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,32 @@ import 'package:async/async.dart';
66
import 'game_engine.dart';
77

88
class ConcurrentGameEngine implements GameEngine {
9-
final ReceivePort _receivePort = ReceivePort();
10-
late final StreamQueue<Object?> _receiveQueue = StreamQueue<Object?>(
11-
_receivePort,
12-
);
13-
SendPort? _sendPort;
149
Isolate? _isolate;
10+
SendPort? _sendPort;
11+
12+
final ReceivePort _receivePort = ReceivePort();
13+
late final StreamQueue _receiveQueue = StreamQueue(_receivePort);
1514

1615
@override
1716
Future<UiState> start() async {
1817
if (_isolate == null) {
1918
_isolate = await Isolate.spawn(_isolateEntryPoint, _receivePort.sendPort);
20-
_sendPort = await _receiveQueue.next as SendPort;
19+
_sendPort = await _receiveQueue.next;
2120
}
2221
_sendPort!.send('start');
23-
return (await _receiveQueue.next) as UiState;
22+
return await _receiveQueue.next;
2423
}
2524

2625
@override
2726
Future<UiState> reportMove(int row, int col) async {
2827
_sendPort!.send([row, col]);
29-
return (await _receiveQueue.next) as UiState;
28+
return await _receiveQueue.next;
3029
}
3130

3231
@override
3332
Future<UiState> makeMove() async {
3433
_sendPort!.send('makeMove');
35-
return (await _receiveQueue.next) as UiState;
34+
return await _receiveQueue.next;
3635
}
3736

3837
@override
@@ -45,22 +44,18 @@ class ConcurrentGameEngine implements GameEngine {
4544
}
4645

4746
void _isolateEntryPoint(SendPort sendPort) {
48-
final ReceivePort receivePort = ReceivePort();
47+
final receivePort = ReceivePort();
4948
sendPort.send(receivePort.sendPort);
5049

51-
final GameEngine engine = GameEngine();
50+
final engine = GameEngine();
5251

5352
receivePort.listen((Object? message) async {
54-
final UiState uiState;
5553
if (message == 'start') {
56-
uiState = await engine.start();
54+
sendPort.send(await engine.start());
5755
} else if (message == 'makeMove') {
58-
uiState = await engine.makeMove();
56+
sendPort.send(await engine.makeMove());
5957
} else if (message is List<int> && message.length == 2) {
60-
uiState = await engine.reportMove(message.first, message.last);
61-
} else {
62-
throw Exception('Invalid message sent to isolate: $message');
58+
sendPort.send(await engine.reportMove(message.first, message.last));
6359
}
64-
sendPort.send(uiState);
6560
});
6661
}

0 commit comments

Comments
 (0)