Skip to content

Commit

Permalink
Add support for announcements using the vm service, this means from n…
Browse files Browse the repository at this point in the history
…ow on discovery can work on web as well
  • Loading branch information
NicolaVerbeeck committed Mar 30, 2022
1 parent 137822e commit 8be1c96
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.1.0

- Add support for announcements using the vm service, this means from now on discovery can work on web as well

## 1.0.3

- Missed deprecation on constant
Expand Down
3 changes: 3 additions & 0 deletions example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ class DemoServer extends ToolingServer {

@override
int get protocolVersion => 4;

@override
String get name => 'demo';
}

Future<void> main() async {
Expand Down
54 changes: 51 additions & 3 deletions lib/src/create_server.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,62 @@
import 'package:dart_service_announcement/src/server_base.dart';

import 'package:dart_service_announcement/src/server_empty.dart'
if (dart.library.html) 'package:dart_service_announcement/src/web_server.dart'
if (dart.library.io) 'package:dart_service_announcement/src/io_server.dart';
import 'package:dart_service_announcement/src/vm_service_server.dart';

///Create the announcement server for the tooling server
///Create the announcement server(s) for the tooling server
// ignore: non_constant_identifier_names
BaseServerAnnouncementManager ServerAnnouncementManager(
String packageName,
int announcementPort,
ToolingServer server,
) =>
internalCreateServer(packageName, announcementPort, server);
_CombiningServerAnnouncementManager([
internalCreateServer(packageName, announcementPort, server),
internalCreateVMServiceServer(packageName, announcementPort, server),
]);

///A proxy ServerAnnouncementManager that combines multiple ServerAnnouncementManagers
class _CombiningServerAnnouncementManager
implements BaseServerAnnouncementManager {
final List<BaseServerAnnouncementManager> _managers;

_CombiningServerAnnouncementManager(this._managers);

@override
void addExtension(AnnouncementExtension extension) {
for (final manager in _managers) {
manager.addExtension(extension);
}
}

@override
int get announcementPort => _managers[0].announcementPort;

@override
String get packageName => _managers[0].packageName;

@override
void removeExtension(AnnouncementExtension extension) {
for (final manager in _managers) {
manager.removeExtension(extension);
}
}

@override
ToolingServer get server => _managers[0].server;

@override
Future<void> start() async {
for (final manager in _managers) {
await manager.start();
}
}

@override
Future<void> stop() async {
for (final manager in _managers) {
await manager.stop();
}
}
}
3 changes: 3 additions & 0 deletions lib/src/server_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ abstract class ToolingServer {

///The protocol version this tooling server 'speaks'
int get protocolVersion;

///The name of the tooling server. Must be in [a-zA-Z0-9_]. Eg: niddler, local_storage_inspector
String get name;
}

///Base for announcement extensions.
Expand Down
67 changes: 67 additions & 0 deletions lib/src/vm_service_server.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import 'dart:convert';
import 'dart:developer';

import 'package:dart_service_announcement/src/server_base.dart';

BaseServerAnnouncementManager internalCreateVMServiceServer(
String packageName,
int announcementPort,
ToolingServer server,
) =>
_VMServiceServerAnnouncementManager(packageName, announcementPort, server);

class _VMServiceServerAnnouncementManager
extends BaseServerAnnouncementManager {
final _extensions = <AnnouncementExtension>[];
var _started = false;

_VMServiceServerAnnouncementManager(
String packageName,
int announcementPort,
ToolingServer server,
) : super(packageName, announcementPort, server) {
registerExtension('ext.${server.name}.query', (method, params) async {
if (!_started) {
return ServiceExtensionResponse.error(
-1,
'Announcement server stopped',
);
}

final responseData = <String, dynamic>{};
responseData['packageName'] = packageName;
responseData['port'] = server.port;
responseData['pid'] = -1;
responseData['protocol'] = server.protocolVersion;
responseData['extensions'] = _extensions.map((ext) {
return {
'name': ext.name,
'data': base64.encoder.convert(ext.data()),
};
}).toList();
return ServiceExtensionResponse.result(json.encode(responseData));
});
}

@override
void addExtension(AnnouncementExtension extension) {
_extensions.add(extension);
}

@override
void removeExtension(AnnouncementExtension extension) {
_extensions.removeWhere((element) => element == extension);
}

@override
Future<void> start() {
_started = true;
return Future.value();
}

@override
Future<void> stop() {
_started = false;
return Future.value();
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: dart_service_announcement
description: Helper libraries for creating discoverable development services
version: 1.0.3
version: 1.1.0
homepage: https://github.com/Chimerapps/dart_tooling_announcement

environment:
Expand Down

0 comments on commit 8be1c96

Please sign in to comment.