Skip to content

Commit 38ca626

Browse files
authored
Use Map.of instead of Map.from in grpc client. (#724)
* Use `Map.of` instead of `Map.from` in grpc client. `Map.of` creates a new map with the same keys, values and *type* as the original map, when used without type arguments or context type, where `Map.from` creates a `Map<dynamic, dynamic>`. (This code failed on an attempt to make `Map.unmodifiable` be more strictly typed, like `Map.of` instead of `Map.from`, showing that an intermediate map had type `Map<dynamic, dynamic>` unnecessarily). Same for using `List.of` instead of `List.from`. The new code should be (microscopically) more efficient and type safe, and is forwards-compatible with a stronger type on `Map.unmodifiable`. (The code can be optimized more. For example `List.of(list1)..addAll(list2)` can be just `list1 + list2` or `[...list1, ...list2]`, both of which may know the total number of elements when doing the initial list allocation. This is a minimal change to allow the type changes for `.unmodifiable` to get past this very initial blocker in internal tests.) * Add changelog and minor version increment. And my save removes trailing spaces.
1 parent 4f6fe9b commit 38ca626

File tree

4 files changed

+14
-10
lines changed

4 files changed

+14
-10
lines changed

CHANGELOG.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 4.0.2
2+
3+
* Internal optimization to client code.
4+
15
## 4.0.1
26

37
* Fix header and trailing not completing if the call is terminated. Fixes [#727](https://github.com/grpc/grpc-dart/issues/727)
@@ -31,19 +35,19 @@
3135

3236
## 3.2.1
3337

34-
* `package:http` now supports more versions: `>=0.13.0 <2.0.0`.
38+
* `package:http` now supports more versions: `>=0.13.0 <2.0.0`.
3539
* `package:protobuf` new supports more versions: `>=2.0.0 <4.0.0`.
3640

3741
## 3.2.0
3842

39-
* `ChannelOptions` now exposes `connectTimeout`, which is used on the
43+
* `ChannelOptions` now exposes `connectTimeout`, which is used on the
4044
socket connect. This is used to specify the maximum allowed time to wait
4145
for a connection to be established. If `connectTime` is longer than the system
4246
level timeout duration, a timeout may occur sooner than specified in
4347
`connectTimeout`. On timeout, a `SocketException` is thrown.
4448
* Require Dart 2.17 or greater.
4549
* Fix issue [#51](https://github.com/grpc/grpc-dart/issues/51), add support for custom error handling.
46-
* Expose client IP address to server
50+
* Expose client IP address to server
4751
* Add a `channelShutdownHandler` argument to `ClientChannel` and the subclasses.
4852
This callback can be used to react to channel shutdown or termination.
4953
* Export the `Code` protobuf enum from the `grpc.dart` library.

lib/src/client/call.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ class CallOptions {
8686

8787
CallOptions mergedWith(CallOptions? other) {
8888
if (other == null) return this;
89-
final mergedMetadata = Map.from(metadata)..addAll(other.metadata);
89+
final mergedMetadata = Map.of(metadata)..addAll(other.metadata);
9090
final mergedTimeout = other.timeout ?? timeout;
91-
final mergedProviders = List.from(metadataProviders)
91+
final mergedProviders = List.of(metadataProviders)
9292
..addAll(other.metadataProviders);
9393
final mergedCompression = other.compression ?? compression;
9494
return CallOptions._(
@@ -146,9 +146,9 @@ class WebCallOptions extends CallOptions {
146146
CallOptions mergedWith(CallOptions? other) {
147147
if (other == null) return this;
148148

149-
final mergedMetadata = Map.from(metadata)..addAll(other.metadata);
149+
final mergedMetadata = Map.of(metadata)..addAll(other.metadata);
150150
final mergedTimeout = other.timeout ?? timeout;
151-
final mergedProviders = List.from(metadataProviders)
151+
final mergedProviders = List.of(metadataProviders)
152152
..addAll(other.metadataProviders);
153153

154154
if (other is! WebCallOptions) {
@@ -241,7 +241,7 @@ class ClientCall<Q, R> implements Response {
241241
if (options.metadataProviders.isEmpty) {
242242
_sendRequest(connection, _sanitizeMetadata(options.metadata));
243243
} else {
244-
final metadata = Map<String, String>.from(options.metadata);
244+
final metadata = Map<String, String>.of(options.metadata);
245245
Future.forEach(
246246
options.metadataProviders,
247247
(MetadataProvider provider) => provider(metadata,

lib/src/shared/status.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ GrpcError? grpcErrorDetailsFromTrailers(Map<String, String> trailers) {
473473
}
474474

475475
Map<String, String> toCustomTrailers(Map<String, String> trailers) {
476-
return Map.from(trailers)
476+
return Map.of(trailers)
477477
..remove(':status')
478478
..remove('content-type')
479479
..remove('grpc-status')

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: grpc
22
description: Dart implementation of gRPC, a high performance, open-source universal RPC framework.
3-
version: 4.0.1
3+
version: 4.0.2
44

55
repository: https://github.com/grpc/grpc-dart
66

0 commit comments

Comments
 (0)