enumerateWith static method

Stream<DiscoveredPrinter> enumerateWith({
  1. required UsbPlatform platform,
  2. bool includeNonZebra = false,
})

Test-visible variant that accepts an injected UsbPlatform.

Wraps the platform call in a stream so per-source errors surface through DiscoveryService.mergeSources' onError handler and don't crash the merged pipeline.

Implementation

static Stream<DiscoveredPrinter> enumerateWith({
  required UsbPlatform platform,
  bool includeNonZebra = false,
}) {
  final filter = UsbEnumerateFilter(
    vendorId: includeNonZebra ? null : UsbDeviceAddress.zebraVendorId,
    includeDescriptorStrings: true,
  );

  final controller = StreamController<DiscoveredPrinter>();

  () async {
    try {
      final records = await platform.enumerate(filter);
      for (final r in records) {
        if (controller.isClosed) return;
        controller.add(_toDiscoveredPrinter(r));
      }
    } catch (e, s) {
      if (!controller.isClosed) controller.addError(e, s);
    } finally {
      if (!controller.isClosed) await controller.close();
    }
  }();

  return controller.stream;
}