hubBleManagerFamily top-level property

AutoDisposeStateNotifierProviderFamily<HubBleManager, BleConnectionStatus, String> hubBleManagerFamily
final

Implementation

final hubBleManagerFamily = StateNotifierProvider.autoDispose
    .family<HubBleManager, BleConnectionStatus, String>((ref, hubSerialNumber) {
  final firmwareAutoUpdateNotifier =
      ref.watch(firmwareAutoUpdateFamily(hubSerialNumber).notifier);
  final manager = HubBleManager(hubSerialNumber, firmwareAutoUpdateNotifier);

  KeepAliveLink? keepAliveLink;

  void syncKeepAlive(BleConnectionStatus status) {
    final shouldKeepAlive = status is BleConnected ||
        status is BleConnecting ||
        status is BleScanning ||
        status is BleDeviceFound;

    if (shouldKeepAlive) {
      keepAliveLink ??= ref.keepAlive();
      return;
    }

    keepAliveLink?.close();
    keepAliveLink = null;
  }

  final removeListener =
      manager.addListener(syncKeepAlive, fireImmediately: true);

  ref.onDispose(() {
    keepAliveLink?.close();
    removeListener();
    manager.dispose();
  });

  return manager;
});