LCOV - code coverage report

Current view
top level - src/_impl/native - _worker_channel.dart
Test
lcov.info
Date
2024-11-13
Legend
Lines
hit
not hit
Branches
taken
not taken
# not executed
HitTotalCoverage
Lines2323100.0%
Functions00-
Branches00-
Each row represents a line of source code
LineBranchHitsSource code
1import 'dart:async';
2import 'dart:isolate';
3
4import 'package:logger/web.dart';
5import 'package:meta/meta.dart';
6
7import '../../exceptions/squadron_error.dart';
8import '../../exceptions/squadron_exception.dart';
9import '../../exceptions/worker_exception.dart';
10import '../../typedefs.dart';
11import '../../worker/worker_channel.dart';
12import '../../worker/worker_response.dart';
13
14/// [WorkerChannel] implementation for the native world.
15final class _VmWorkerChannel implements WorkerChannel {
169 _VmWorkerChannel._(this._sendPort, this._logger);
17
18 final Logger? _logger;
19
20 /// [SendPort] to communicate with the [Isolate] if the channel is owned by
21 /// the worker owner. Otherwise, [SendPort] to return values to the client.
22 final SendPort _sendPort;
23
249 void _postResponse(WorkerResponse res) {
25 try {
269 final data = res.wrapInPlace();
2718 _sendPort.send(data);
28 } catch (ex, st) {
294 _logger?.e(() => 'Failed to post response $res: $ex');
302 throw SquadronErrorExt.create('Failed to post response: $ex', st);
31 }
32 }
33
34 /// Sends the [SendPort] to communicate with the [Isolate]. This method must
35 /// be called by the worker [Isolate] upon startup.
369 @override
379 void connect(PlatformChannel channelInfo) => inspectAndReply(channelInfo);
38
39 /// Sends a [WorkerResponse] with the specified data to the worker client.
40 /// This method must be called from the worker [Isolate] only.
419 @override
4218 void reply(dynamic data) => _postResponse(WorkerResponse.withResult(data));
43
44 /// Sends a [WorkerResponse] with the specified data to the worker client.
45 /// This method must be called from the worker [Isolate] only. On VM
46 /// platforms, this is the same as [reply] .
479 @override
489 void inspectAndReply(dynamic data) => reply(data);
49
509 @override
5118 void log(LogEvent message) => _postResponse(WorkerResponse.log(message));
52
53 /// Checks if [stream] can be streamed back to the worker client. Returns
54 /// `true` unless [stream] is a [ReceivePort].
556 @override
566 bool canStream(Stream<dynamic> stream) => stream is! ReceivePort;
57
58 /// Sends a [WorkerResponse.closeStream] to the worker client. This method
59 /// must be called from the worker [Isolate] only.
606 @override
6112 void closeStream() => _postResponse(WorkerResponse.closeStream());
62
63 /// Sends the [WorkerException] to the worker client. This method must be
64 /// called from the worker [Isolate] only.
655 @override
66 void error(Object err, [StackTrace? stackTrace, int? command]) {
675 final ex = SquadronException.from(err, stackTrace, command);
6810 _postResponse(WorkerResponse.withError(ex));
69 }
70}
71
72/// Creates a [WorkerChannel] from a [SendPort].
739@internal
74WorkerChannel? deserialize(PlatformChannel? channelInfo, [Logger? logger]) =>
759 (channelInfo == null) ? null : _VmWorkerChannel._(channelInfo, logger);
Choose Features