toolServiceMethod method

Future<String?> toolServiceMethod(
  1. String service, {
  2. Duration timeout = const Duration(seconds: 2),
})

Finds the method the attached Flutter tool registered for service (reloadSources or hotRestart), e.g. s1.reloadSources.

Implementation

Future<String?> toolServiceMethod(
  String service, {
  Duration timeout = const Duration(seconds: 2),
}) async {
  final String? known = _toolServices[service];
  if (known != null) return known;

  final Completer<void> found = Completer<void>();
  final StreamSubscription<JsonRpcNotification> subscription = client
      .notifications
      .listen((notification) {
        if (notification.method != 'streamNotify') return;
        if (notification.params['streamId'] != 'Service') return;
        final Object? event = notification.params['event'];
        if (event is! Map || event['kind'] != 'ServiceRegistered') return;
        final Object? name = event['service'];
        final Object? method = event['method'];
        if (name is String && method is String) {
          _toolServices[name] = method;
          if (name == service && !found.isCompleted) found.complete();
        }
      });

  try {
    if (!_serviceStreamListening) {
      try {
        await client.call('streamListen', {'streamId': 'Service'}, timeout);
      } on JsonRpcException catch (e) {
        if (e.code != 103) rethrow;
      }
      _serviceStreamListening = true;
    }
    await found.future.timeout(timeout, onTimeout: () {});
    return _toolServices[service];
  } finally {
    await subscription.cancel();
  }
}