LCOV - code coverage report

Current view
top level - /src - worker_response.dart
Test
lcov.info
Date
2022-04-02
Legend
Lines
hit
not hit
Branches
taken
not taken
# not executed
HitTotalCoverage
Lines212295.5%
Functions00-
Branches00-
Each row represents a line of source code
LineBranchHitsSource code
1import 'dart:async';
2
3import 'squadron_exception.dart';
4
5/// Class used to communicate from a [Worker] to clients.
6/// [WorkerResponse]s are used to provide individual results to the client. [Future]-based services simply return a
7/// single [WorkerResponse] with the result. [Stream]ing services will return one [WorkerResponse]s for each stream
8/// item and mmust send a [WorkerResponse.endOfStream] message to indicate completion. [WorkerResponse]s can also
9/// send error messages.
10class WorkerResponse {
11 /// [WorkerResponse] with a valid [result].
121 WorkerResponse(this._result)
13 : _error = null,
14 _eos = false;
15
16 /// [WorkerResponse] with an error message and an optional (string) [StackTrace].
172 WorkerResponse.withError(Object exception, [StackTrace? stackTrace])
182 : _error = SquadronException.from(
192 error: exception, stackTrace: stackTrace ?? StackTrace.current),
20 _result = null,
21 _eos = false;
22
23 /// Special [WorkerResponse] message to indicate the end of a stream.
241 const WorkerResponse._endOfStream()
25 : _result = null,
26 _error = null,
27 _eos = true;
28
29 /// End of stream response.
30 static const closeStream = WorkerResponse._endOfStream();
31
32 static const _$result = 'a';
33 static const _$error = 'b';
34 static const _$eos = 'c';
35
36 /// Creates a new [WorkerResponse] from a message sent by the worker.
372 WorkerResponse.deserialize(Map message)
382 : _result = message[_$result],
393 _error = SquadronException.deserialize(message[_$error]),
402 _eos = message[_$eos] ?? false;
41
42 /// [WorkerResponse] serialization.
432 Map<String, dynamic> serialize() {
442 if (_error != null) {
454 return {_$error: _error!.serialize()};
462 } else if (_eos) {
471 return const {_$eos: true};
482 } else if (_result == null) {
490 return const {};
50 } else {
513 return {_$result: _result};
52 }
531 }
54
55 /// Flag indicating the end of the [Stream]ing operation.
563 bool get endOfStream => _eos;
57 final bool _eos;
58
59 /// The [WorkerResponse] exception, if any.
603 SquadronException? get error => _error;
61 final SquadronException? _error;
62
63 /// Flag indicating whether an error occured.
643 bool get hasError => _error != null;
65
66 /// Retrieves the result associated to this [WorkerResponse]. If the [WorkerResponse] contains an error,
67 /// an the [error] exception is thrown.
685 dynamic get result => hasError ? throw error! : _result;
69 final dynamic _result;
70}
Choose Features