LCOV - code coverage report

Current view
top level - src/worker - worker_request.dart
Test
lcov.info
Date
2026-11-07
Legend
Lines
hit
not hit
Branches
taken
not taken
# not executed
HitTotalCoverage
Lines434693.5%
Functions00-
Branches00-
Each row represents a line of source code
LineBranchHitsSource code
1import 'package:meta/meta.dart';
2
3import '../build_options.dart';
4import '../_impl/xplat/_internal_logger.dart';
5import '../_impl/xplat/_time_stamp.dart';
6import '../exceptions/squadron_error.dart';
7import '../tokens/_squadron_cancelation_token.dart';
8import '../typedefs.dart';
9import 'worker.dart';
10import 'worker_channel.dart';
11import 'worker_message.dart';
12
13/// [WorkerRequest]s are used to communicate from a client to a [Worker].
14/// Typically a [WorkerRequest] consists of a command ID and a list of
15/// arguments. The [command] ID is used by the [Worker] to dispatch the
16/// [WorkerRequest] to the method responsible for handling it.
17/// The command's arguments are passed as a list and should only contain
18/// primitive values or objects that can be transfered across workers. For
19/// applications running on a VM platform, Dart objects should be safe
20/// according to Dart's documentation of [SendPort.send]. [WorkerRequestImpl]
21/// also implements specific requests used for worker startup, stream/token
22/// cancelation, worker termination...
231extension type WorkerRequest._(List data) implements WorkerMessage {
241 factory WorkerRequest.from(List data) {
252 if (data.length != 7) {
260 throw SquadronErrorImpl.create('Invalid worker request');
27 }
281 return WorkerRequest._(data);
29 }
30
31 /// Creates a new request with the specified [command] ID and optional arguments.
321 factory WorkerRequest.userCommand(PlatformChannel channelInfo, int command,
33 List args, SquadronCancelationToken? token, bool inspectResponse) =>
342 WorkerRequest._([
351 BuildOptions.withTravelTime ? Timestamp.now() : null, // 0 - travel time
36 channelInfo, // 1 - channel
37 command, // 2 - command
38 args, // 3 - args
39 token, // 4 - cancelation token
40 null, // 5 - stream id
41 inspectResponse, // 6 - inspect response
42 ]);
43
44 /// Creates a new start request.
451 factory WorkerRequest.start(PlatformChannel channelInfo, List args) =>
462 WorkerRequest._([
471 BuildOptions.withTravelTime ? Timestamp.now() : null, // 0 - travel time
48 channelInfo, // 1 - channel
49 _connectCommand, // 2 - command
50 args, // 3 - args
51 null, // 4 - cancelation token
52 null, // 5 - stream id
53 true, // 6 - inspect response
54 ]);
55
56 /// Creates a new stream cancelation request.
573 factory WorkerRequest.cancelStream(StreamId streamId) => WorkerRequest._([
581 BuildOptions.withTravelTime ? Timestamp.now() : null, // 0 - travel time
59 null, // 1 - channel
60 _cancelStreamCommand, // 2 - command
61 null, // 3 - args
62 null, // 4 - cancelation token
63 streamId.handle, // 5 - stream id
64 null, // 6 - inspect response
65 ]);
66
67 /// Creates a new cancelation request.
681 factory WorkerRequest.cancel(SquadronCancelationToken token) =>
692 WorkerRequest._([
701 BuildOptions.withTravelTime ? Timestamp.now() : null, // 0 - travel time
71 null, // 1 - channel
72 _cancelTokenCommand, // 2 - command
73 null, // 3 - args
74 token, // 4 - cancelation token
75 null, // 5 - stream id
76 null, // 6 - inspect response
77 ]);
78
79 /// Creates a new termination request.
803 factory WorkerRequest.stop() => WorkerRequest._([
811 BuildOptions.withTravelTime ? Timestamp.now() : null, // 0 - travel time
82 null, // 1 - channel
83 _terminateCommand, // 2 - command
84 null, // 3 - args
85 null, // 4 - cancelation token
86 null, // 5 - stream id
87 null, // 6 - inspect response
88 ]);
89
90 /// The client's [WorkerChannel]. Only valid on the receiving end.
912 WorkerChannel? get channel => data[_$channel];
92
93 /// The channel method to be used for sending data back.
941 void Function(dynamic)? get reply =>
955 inspectResponse ? channel?.inspectAndReply : channel?.reply;
96
97 /// The client's channel info.
980 PlatformChannel? get channelInfo => data[_$channel];
99
100 /// Cancelation token.
1012 SquadronCancelationToken? get cancelToken => data[_$token];
102
103 /// Stream id.
1043 StreamId? get streamId => StreamId.from(data[_$streamId]);
105
106 /// The [command]'s ID.
1072 int get command => data[_$command];
108
109 /// The command's arguments, if any.
1102 List get args => data[_$args];
111
112 /// Flag indicating whether the Channel should inspect the payload to identify non-base type objects. In
113 /// Web Workers, ownership of these objects must be transfered across threads.
1142 bool get inspectResponse => data[_$inspectResponse];
115
116 /// flag for start requests.
1173 bool get isConnection => (command == _connectCommand);
118
119 /// flag for stream cancelation requests.
1203 bool get isStreamCancelation => (command == _cancelStreamCommand);
121
122 /// flag for cancelation requests.
1233 bool get isTokenCancelation => (command == _cancelTokenCommand);
124
125 /// flag for termination requests.
1263 bool get isTermination => (command == _terminateCommand);
127
128 /// In-place deserialization of a [WorkerRequest] received by the worker.
1291 void unwrapInPlace(InternalLogger? logger) {
1301 unwrapTravelTime();
1313 data[_$command] = (data[_$command] as num).toInt();
1323 data[_$channel] = WorkerChannel.deserialize(data[_$channel], logger);
1333 data[_$token] = SquadronCancelationToken.deserialize(data[_$token]);
1341 data[_$inspectResponse] ??= false;
1351 data[_$args] ??= const [];
136 }
137
138 /// In-place serialization of a [WorkerRequest].
1391 void wrapInPlace() {
1401 final token = data[_$token];
1411 if (token is SquadronCancelationToken) {
1422 data[_$token] = token.serialize();
143 }
144 }
145
146 static const int _connectCommand = -1;
147 static const int _cancelStreamCommand = -2;
148 static const int _cancelTokenCommand = -3;
149 static const int _terminateCommand = -4;
150}
151
152// 0 is reserved for travel time
153const _$channel = 1;
154const _$command = 2;
155const _$args = 3;
156const _$token = 4;
157const _$streamId = 5;
158const _$inspectResponse = 6;
159
160@internal
161extension CancelationTokenExt on WorkerRequest {
1621 void overrideCancelToken(SquadronCancelationToken token) {
1635 if (cancelToken == null || cancelToken!.id != token.id) {
1640 throw SquadronErrorImpl.create('Cancelation token mismatch');
165 }
1661 data[_$token] = token;
167 }
168}
Choose Features