-
Notifications
You must be signed in to change notification settings - Fork 700
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b18904a
commit 7fbb199
Showing
11 changed files
with
1,127 additions
and
262 deletions.
There are no files selected for viewing
198 changes: 0 additions & 198 deletions
198
examples/concurrency/lib/basic_ports_example/bidi_ports_example_complete.dart
This file was deleted.
Oops, something went wrong.
39 changes: 0 additions & 39 deletions
39
examples/concurrency/lib/basic_ports_example/bidi_ports_setup_step1.dart
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
examples/concurrency/lib/robust_ports_example/complete.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import 'dart:async'; | ||
import 'dart:convert'; | ||
import 'dart:isolate'; | ||
|
||
void main() async { | ||
final worker = await Worker.spawn(); | ||
print(await worker.parseJson('{"key":"value"}')); | ||
print(await worker.parseJson('"banana"')); | ||
print(await worker.parseJson('[true, false, null, 1, "string"]')); | ||
print( | ||
await Future.wait([worker.parseJson('"yes"'), worker.parseJson('"no"')])); | ||
worker.close(); | ||
} | ||
|
||
// #docregion constructor | ||
class Worker { | ||
final SendPort _commands; | ||
final ReceivePort _responses; | ||
// #enddocregion constructor | ||
final Map<int, Completer<Object?>> _activeRequests = {}; | ||
int _idCounter = 0; | ||
bool _closed = false; | ||
|
||
Future<Object?> parseJson(String message) async { | ||
if (_closed) throw StateError('Closed'); | ||
final completer = Completer<Object?>.sync(); | ||
final id = _idCounter++; | ||
_activeRequests[id] = completer; | ||
_commands.send((id, message)); | ||
return await completer.future; | ||
} | ||
|
||
static Future<Worker> spawn() async { | ||
// Create a receive port and add it's initial message handler | ||
final initPort = RawReceivePort(); | ||
final connection = Completer<(ReceivePort, SendPort)>.sync(); | ||
initPort.handler = (initialMessage) { | ||
final commandPort = initialMessage as SendPort; | ||
connection.complete(( | ||
ReceivePort.fromRawReceivePort(initPort), | ||
commandPort, | ||
)); | ||
}; | ||
|
||
// Spawn the isolate | ||
final Isolate isolate; | ||
try { | ||
isolate = await Isolate.spawn(_startRemoteIsolate, (initPort.sendPort)); | ||
} on Object { | ||
initPort.close(); | ||
rethrow; | ||
} | ||
|
||
final (ReceivePort receivePort, SendPort sendPort) = | ||
await connection.future; | ||
|
||
return Worker._(receivePort, sendPort); | ||
} | ||
|
||
Worker._(this._responses, this._commands) { | ||
_responses.listen(_handleResponsesFromIsolate); | ||
} | ||
|
||
void _handleResponsesFromIsolate(dynamic message) { | ||
final (int id, Object? response) = message as (int, Object?); | ||
final completer = _activeRequests.remove(id)!; | ||
|
||
if (response is RemoteError) { | ||
completer.completeError(response); | ||
} else { | ||
completer.complete(response); | ||
} | ||
|
||
if (_closed && _activeRequests.isEmpty) _responses.close(); | ||
} | ||
|
||
static void _handleCommandsToIsolate( | ||
ReceivePort receivePort, | ||
SendPort sendPort, | ||
) { | ||
receivePort.listen((message) { | ||
if (message == 'shutdown') { | ||
receivePort.close(); | ||
return; | ||
} | ||
final (int id, String jsonText) = message as (int, String); | ||
try { | ||
final jsonData = jsonDecode(jsonText); | ||
sendPort.send((id, jsonData)); | ||
} catch (e) { | ||
sendPort.send((id, RemoteError(e.toString(), ''))); | ||
} | ||
}); | ||
} | ||
|
||
static void _startRemoteIsolate(SendPort sendPort) { | ||
final receivePort = ReceivePort(); | ||
sendPort.send(receivePort.sendPort); | ||
_handleCommandsToIsolate(receivePort, sendPort); | ||
} | ||
|
||
void close() { | ||
if (!_closed) { | ||
_closed = true; | ||
_commands.send("shutdown"); | ||
if (_activeRequests.isEmpty) _responses.close(); | ||
print('--- port closed --- '); | ||
} | ||
} | ||
} |
Oops, something went wrong.