LCOV - code coverage report

Current view
top level - src/worker - worker_request.dart
Test
lcov.info
Date
2026-02-21
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 '../_impl/xplat/_internal_logger.dart';
4import '../_impl/xplat/_time_stamp.dart';
5import '../exceptions/squadron_error.dart';
6import '../tokens/_squadron_cancelation_token.dart';
7import '../typedefs.dart';
8import 'worker.dart';
9import 'worker_channel.dart';
10import 'worker_message.dart';
11
12/// [WorkerRequest]s are used to communicate from a client to a [Worker].
13/// Typically a [WorkerRequest] consists of a command ID and a list of
14/// arguments. The [command] ID is used by the [Worker] to dispatch the
15/// [WorkerRequest] to the method responsible for handling it.
16/// The command's arguments are passed as a list and should only contain
17/// primitive values or objects that can be transfered across workers. For
18/// applications running on a VM platform, Dart objects should be safe
19/// according to Dart's documentation of [SendPort.send]. [WorkerRequestImpl]
20/// also implements specific requests used for worker startup, stream/token
21/// cancelation, worker termination...
22extension type WorkerRequest._(List data) implements WorkerMessage {
2310 factory WorkerRequest.from(List data) {
2420 if (data.length != 7) {
250 throw SquadronErrorImpl.create('Invalid worker request');
26 }
2710 return WorkerRequest._(data);
28 }
29
30 /// Creates a new request with the specified [command] ID and optional arguments.
3110 factory WorkerRequest.userCommand(PlatformChannel channelInfo, int command,
32 List args, SquadronCancelationToken? token, bool inspectResponse) =>
3320 WorkerRequest._([
3410 microsecTimeStamp(), // 0 - travel time
35 channelInfo, // 1 - channel
36 command, // 2 - command
37 args, // 3 - args
38 token, // 4 - cancelation token
39 null, // 5 - stream id
40 inspectResponse, // 6 - inspect response
41 ]);
42
43 /// Creates a new start request.
4411 factory WorkerRequest.start(PlatformChannel channelInfo, List args) =>
4522 WorkerRequest._([
4611 microsecTimeStamp(), // 0 - travel time
47 channelInfo, // 1 - channel
48 _connectCommand, // 2 - command
49 args, // 3 - args
50 null, // 4 - cancelation token
51 null, // 5 - stream id
52 true, // 6 - inspect response
53 ]);
54
55 /// Creates a new stream cancelation request.
5612 factory WorkerRequest.cancelStream(int streamId) => WorkerRequest._([
574 microsecTimeStamp(), // 0 - travel time
58 null, // 1 - channel
59 _cancelStreamCommand, // 2 - command
60 null, // 3 - args
61 null, // 4 - cancelation token
62 streamId, // 5 - stream id
63 null, // 6 - inspect response
64 ]);
65
66 /// Creates a new cancelation request.
672 factory WorkerRequest.cancel(SquadronCancelationToken token) =>
684 WorkerRequest._([
692 microsecTimeStamp(), // 0 - travel time
70 null, // 1 - channel
71 _cancelTokenCommand, // 2 - command
72 null, // 3 - args
73 token, // 4 - cancelation token
74 null, // 5 - stream id
75 null, // 6 - inspect response
76 ]);
77
78 /// Creates a new termination request.
7930 factory WorkerRequest.stop() => WorkerRequest._([
8010 microsecTimeStamp(), // 0 - travel time
81 null, // 1 - channel
82 _terminateCommand, // 2 - command
83 null, // 3 - args
84 null, // 4 - cancelation token
85 null, // 5 - stream id
86 null, // 6 - inspect response
87 ]);
88
89 /// The client's [WorkerChannel]. Only valid on the receiving end.
9020 WorkerChannel? get channel => data[_$channel];
91
92 /// The channel method to be used for sending data back.
9310 void Function(dynamic)? get reply =>
9434 inspectResponse ? channel?.inspectAndReply : channel?.reply;
95
96 /// The client's channel info.
970 PlatformChannel? get channelInfo => data[_$channel];
98
99 /// Cancelation token.
10020 SquadronCancelationToken? get cancelToken => data[_$token];
101
102 /// Stream id.
1038 int? get streamId => data[_$streamId];
104
105 /// The [command]'s ID.
10620 int get command => data[_$command];
107
108 /// The command's arguments, if any.
10920 List get args => data[_$args];
110
111 /// Flag indicating whether the Channel should inspect the payload to identify non-base type objects. In
112 /// Web Workers, ownership of these objects must be transfered across threads.
11320 bool get inspectResponse => data[_$inspectResponse];
114
115 /// flag for start requests.
11630 bool get isConnection => (command == _connectCommand);
117
118 /// flag for stream cancelation requests.
11930 bool get isStreamCancelation => (command == _cancelStreamCommand);
120
121 /// flag for cancelation requests.
12230 bool get isTokenCancelation => (command == _cancelTokenCommand);
123
124 /// flag for termination requests.
12530 bool get isTermination => (command == _terminateCommand);
126
127 /// In-place deserialization of a [WorkerRequest] received by the worker.
12810 void unwrapInPlace(InternalLogger? logger) {
12910 unwrapTravelTime();
13030 data[_$command] = (data[_$command] as num).toInt();
13124 data[_$streamId] = (data[_$streamId] as num?)?.toInt();
13230 data[_$channel] = WorkerChannel.deserialize(data[_$channel], logger);
13330 data[_$token] = SquadronCancelationToken.deserialize(data[_$token]);
13410 data[_$inspectResponse] ??= false;
13510 data[_$args] ??= const [];
136 }
137
138 /// In-place serialization of a [WorkerRequest].
13911 void wrapInPlace() {
14011 final token = data[_$token];
14111 if (token is SquadronCancelationToken) {
1424 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 {
1622 void overrideCancelToken(SquadronCancelationToken token) {
16310 if (cancelToken == null || cancelToken!.id != token.id) {
1640 throw SquadronErrorImpl.create('Cancelation token mismatch');
165 }
1662 data[_$token] = token;
167 }
168}
Choose Features