load method

Future<DashboardBundle> load(
  1. DashboardBundleRef ref, {
  2. required List<String> connectedDevices,
})

Implementation

Future<DashboardBundle> load(
  DashboardBundleRef ref, {
  required List<String> connectedDevices,
}) async {
  switch (ref.source) {
    case BundleSource.inline:
      final def = ref.inlineDefinition;
      if (def == null) {
        throw DashboardBundleLoadException(
            ref.bundleId, 'inline definition missing');
      }
      return _parse(ref.bundleId, def);

    case BundleSource.marketUrl:
      final url = ref.url;
      final fetcher = _httpFetcher;
      if (fetcher == null) {
        throw DashboardBundleLoadException(
            ref.bundleId, 'httpFetcher not injected');
      }
      if (url == null) {
        throw DashboardBundleLoadException(ref.bundleId, 'url missing');
      }
      try {
        final json = await fetcher.fetch(url);
        return _parse(ref.bundleId, json);
      } catch (e) {
        throw DashboardBundleLoadException(ref.bundleId, 'fetch failed',
            cause: e);
      }

    case BundleSource.aggregatorServer:
      final serverId = ref.aggregatorServerId;
      if (serverId == null) {
        throw DashboardBundleLoadException(
            ref.bundleId, 'aggregatorServerId missing');
      }
      final conn = _conn.getConnection(serverId);
      final client = conn?.client;
      if (client == null) {
        throw DashboardBundleLoadException(
            ref.bundleId, 'Aggregator not connected');
      }
      try {
        final resources = await client.listResources();
        final bundleUri = _pickBundleUri(resources, ref.bundleId);
        if (bundleUri == null) {
          throw DashboardBundleLoadException(
              ref.bundleId, 'Bundle resource not found');
        }
        final res = await client.readResource(bundleUri);
        if (res.contents.isEmpty) {
          throw DashboardBundleLoadException(ref.bundleId, 'Empty resource');
        }
        final text = res.contents.first.text ?? '{}';
        final json = jsonDecode(text);
        if (json is! Map<String, dynamic>) {
          throw DashboardBundleLoadException(
              ref.bundleId, 'Invalid JSON object');
        }
        return _parse(ref.bundleId, json);
      } on DashboardBundleLoadException {
        rethrow;
      } catch (e) {
        throw DashboardBundleLoadException(ref.bundleId, 'Aggregator fetch failed',
            cause: e);
      }

    case BundleSource.synthesized:
      return _synthesize(ref.bundleId, connectedDevices);
  }
}