Fix streamNotify so that it works with binary data

Change-Id: I173ebf46b4ec2bcb138e26456ec40f1fc8faaa19
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/337900
Reviewed-by: Ben Konyi <bkonyi@google.com>
Commit-Queue: Dan Chevalier <danchevalier@google.com>
This commit is contained in:
Dan Chevalier 2023-11-22 21:06:50 +00:00 committed by Commit Queue
parent 613fa1f5aa
commit cf0d8e9556
6 changed files with 28 additions and 6 deletions

View file

@ -1,3 +1,6 @@
## 0.0.2
- Fixed an issue with streamNotify data type being too specific.
## 0.0.1 ## 0.0.1
- Initial version. - Initial version.

View file

@ -11,7 +11,7 @@ abstract class Client {
/// ///
/// This method should do any formatting needed on [data], then send it to /// This method should do any formatting needed on [data], then send it to
/// the [Client]. /// the [Client].
void streamNotify(String stream, Map<String, Object?> data); void streamNotify(String stream, Object data);
/// Called if the connection to the client should be closed. /// Called if the connection to the client should be closed.
Future<void> close(); Future<void> close();

View file

@ -27,7 +27,7 @@ abstract class StreamManager {
@mustCallSuper @mustCallSuper
void postEvent( void postEvent(
String stream, String stream,
Map<String, Object?> data, { Object data, {
Client? excludedClient, Client? excludedClient,
}) { }) {
final listeners = _streamListeners[stream] ?? const <Client>[]; final listeners = _streamListeners[stream] ?? const <Client>[];

View file

@ -1,7 +1,7 @@
name: dart_service_protocol_shared name: dart_service_protocol_shared
description: A package that implements service extensions and stream managers. description: A package that implements service extensions and stream managers.
version: 0.0.1 version: 0.0.2
repository: https://github.com/dart-lang/sdk/tree/main/pkg/dart_service_protocol_shared repository: https://github.com/dart-lang/sdk/tree/main/pkg/dart_service_protocol_shared
environment: environment:

View file

@ -25,7 +25,7 @@ class TestClient extends Client {
} }
@override @override
void streamNotify(String stream, Map<String, Object?> data) { void streamNotify(String stream, Object data) {
streamNotifyCount++; streamNotifyCount++;
} }
} }

View file

@ -2,6 +2,8 @@
// for details. All rights reserved. Use of this source code is governed by a // for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file. // BSD-style license that can be found in the LICENSE file.
import 'dart:typed_data';
import 'package:dart_service_protocol_shared/src/client.dart'; import 'package:dart_service_protocol_shared/src/client.dart';
import 'package:dart_service_protocol_shared/src/stream_manager.dart'; import 'package:dart_service_protocol_shared/src/stream_manager.dart';
@ -11,7 +13,7 @@ class TestStreamClient extends Client {
int closeCount = 0; int closeCount = 0;
int sendRequestCount = 0; int sendRequestCount = 0;
int streamNotifyCount = 0; int streamNotifyCount = 0;
Map<String, dynamic>? notification; Object? notification;
@override @override
Future<void> close() { Future<void> close() {
@ -26,7 +28,7 @@ class TestStreamClient extends Client {
} }
@override @override
void streamNotify(String stream, Map<String, Object?> data) { void streamNotify(String stream, Object data) {
streamNotifyCount++; streamNotifyCount++;
notification = data; notification = data;
} }
@ -137,6 +139,23 @@ void main() {
expect(clientB.notification, messageB); expect(clientB.notification, messageB);
}); });
test('postEvent can use binary data', () {
final messageA = Uint8List(4);
messageA[0] = 1;
messageA[1] = 2;
messageA[2] = 3;
messageA[3] = 4;
manager.streamListen(client, 'A');
manager.postEvent(
'A',
messageA,
);
expect(client.streamNotifyCount, 1);
expect(client.notification, messageA);
});
test('onClientDisconnect cancels a client from all streams', () async { test('onClientDisconnect cancels a client from all streams', () async {
final testClient = TestStreamClient(); final testClient = TestStreamClient();
final aClients = [ final aClients = [