LCOV - code coverage report

Current view
top level - src/_impl/native - _worker_channel.dart
Test
lcov.info
Date
2026-11-07
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 '../../build_options.dart';
8import '../../exceptions/squadron_error.dart';
9import '../../exceptions/squadron_exception.dart';
10import '../../exceptions/worker_exception.dart';
11import '../../typedefs.dart';
12import '../../worker/worker_channel.dart';
13import '../../worker/worker_response.dart';
14import '_typedefs.dart' as impl;
15
16/// [WorkerChannel] implementation for the native world.
17final class _VmWorkerChannel implements WorkerChannel {
181 _VmWorkerChannel._(this._sendPort, this._logger);
19
20 final Logger? _logger;
21
22 /// [SendPort] to communicate with the [Isolate] if the channel is owned by
23 /// the worker owner. Otherwise, [SendPort] to return values to the client.
24 final impl.PlatformChannel _sendPort;
25
261 void _postResponse(WorkerResponse res) {
27 try {
281 res.wrapInPlace();
292 _sendPort.send(res);
30 } catch (ex, st) {
314 _logger?.e(() => 'Failed to post response $res: $ex');
322 throw SquadronErrorImpl.create('Failed to post response: $ex', st);
33 }
34 }
35
36 /// Sends the [SendPort] to communicate with the [Isolate]. This method must
37 /// be called by the worker [Isolate] upon startup.
381 @override
391 void connect(PlatformChannel channelInfo) => inspectAndReply(channelInfo);
40
41 /// Sends a [WorkerResponse] with the specified data to the worker client.
42 /// This method must be called from the worker [Isolate] only.
431 @override
442 void reply(dynamic data) => _postResponse(WorkerResponse.withResult(data));
45
46 /// Sends a [WorkerResponse] with the specified data to the worker client.
47 /// This method must be called from the worker [Isolate] only. On VM
48 /// platforms, this is the same as [reply] .
491 @override
501 void inspectAndReply(dynamic data) => reply(data);
51
521 @override
53 void log(LogEvent message) {
54 if (BuildOptions.withCrossWorkerLogging) {
552 _postResponse(WorkerResponse.log(message));
56 }
57 }
58
59 /// Checks if [stream] can be streamed back to the worker client. Returns
60 /// `true` unless [stream] is a [ReceivePort].
611 @override
621 bool canStream(Stream<dynamic> stream) => stream is! ReceivePort;
63
64 /// Sends a [WorkerResponse.closeStream] to the worker client. This method
65 /// must be called from the worker [Isolate] only.
661 @override
672 void closeStream() => _postResponse(WorkerResponse.closeStream());
68
69 /// Sends the [WorkerException] to the worker client. This method must be
70 /// called from the worker [Isolate] only.
711 @override
72 void error(Object err, [StackTrace? stackTrace, int? command]) {
731 final ex = SquadronException.from(err, stackTrace, command);
742 _postResponse(WorkerResponse.withError(ex));
75 }
76}
77
78/// Creates a [WorkerChannel] from a [SendPort].
791@internal
80WorkerChannel? deserialize(PlatformChannel? channelInfo, [Logger? logger]) =>
81 (channelInfo == null)
82 ? null
831 : _VmWorkerChannel._(channelInfo as impl.PlatformChannel, logger);
Choose Features