LCOV - code coverage report

Current view
top level - src/_impl/native - _worker_channel.dart
Test
lcov.info
Date
2025-03-26
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';
13import '_typedefs.dart' as impl;
14
15/// [WorkerChannel] implementation for the native world.
16final class _VmWorkerChannel implements WorkerChannel {
1710 _VmWorkerChannel._(this._sendPort, this._logger);
18
19 final Logger? _logger;
20
21 /// [SendPort] to communicate with the [Isolate] if the channel is owned by
22 /// the worker owner. Otherwise, [SendPort] to return values to the client.
23 final impl.PlatformChannel _sendPort;
24
2510 void _postResponse(WorkerResponse res) {
26 try {
2710 final data = res.wrapInPlace();
2820 _sendPort.send(data);
29 } catch (ex, st) {
304 _logger?.e(() => 'Failed to post response $res: $ex');
312 throw SquadronErrorImpl.create('Failed to post response: $ex', st);
32 }
33 }
34
35 /// Sends the [SendPort] to communicate with the [Isolate]. This method must
36 /// be called by the worker [Isolate] upon startup.
3710 @override
3810 void connect(PlatformChannel channelInfo) => inspectAndReply(channelInfo);
39
40 /// Sends a [WorkerResponse] with the specified data to the worker client.
41 /// This method must be called from the worker [Isolate] only.
4210 @override
4320 void reply(dynamic data) => _postResponse(WorkerResponse.withResult(data));
44
45 /// Sends a [WorkerResponse] with the specified data to the worker client.
46 /// This method must be called from the worker [Isolate] only. On VM
47 /// platforms, this is the same as [reply] .
4810 @override
4910 void inspectAndReply(dynamic data) => reply(data);
50
5110 @override
5220 void log(LogEvent message) => _postResponse(WorkerResponse.log(message));
53
54 /// Checks if [stream] can be streamed back to the worker client. Returns
55 /// `true` unless [stream] is a [ReceivePort].
566 @override
576 bool canStream(Stream<dynamic> stream) => stream is! ReceivePort;
58
59 /// Sends a [WorkerResponse.closeStream] to the worker client. This method
60 /// must be called from the worker [Isolate] only.
616 @override
6212 void closeStream() => _postResponse(WorkerResponse.closeStream());
63
64 /// Sends the [WorkerException] to the worker client. This method must be
65 /// called from the worker [Isolate] only.
665 @override
67 void error(Object err, [StackTrace? stackTrace, int? command]) {
685 final ex = SquadronException.from(err, stackTrace, command);
6910 _postResponse(WorkerResponse.withError(ex));
70 }
71}
72
73/// Creates a [WorkerChannel] from a [SendPort].
7410@internal
75WorkerChannel? deserialize(PlatformChannel? channelInfo, [Logger? logger]) =>
76 (channelInfo == null)
77 ? null
7810 : _VmWorkerChannel._(channelInfo as impl.PlatformChannel, logger);
Choose Features