disposebag 1.4.0
disposebag: ^1.4.0 copied to clipboard

Dart native js
Flutter Android iOS Linux macOS web Windows

A package to help disposing Streams and closing Sinks easily.

example/disposebag_example.dart

import 'dart:async';

import 'package:disposebag/disposebag.dart';
import 'package:pedantic/pedantic.dart';

List<dynamic> get _disposables {
  final controllers = <StreamController>[
    StreamController<int>()..add(1)..add(2),
    StreamController<int>(sync: true)..add(3)..add(4),
    StreamController<int>.broadcast()..add(5)..add(6),
    StreamController<String>.broadcast(sync: true)..add('7')..add('8'),
  ];
  final subscriptions = <StreamSubscription>[
    Stream.periodic(const Duration(milliseconds: 100), (i) => i)
        .listen((data) => print('[1] $data')),
    Stream.periodic(const Duration(milliseconds: 10), (i) => i)
        .listen((data) => print('[2] $data')),
    for (int i = 0; i < controllers.length; i++)
      controllers[i].stream.listen((data) => print('[${i + 3}] $data')),
  ];
  return [...controllers, ...subscriptions];
}

void main() async {
  final bag = DisposeBag(_disposables);

  // add & addAll
  await bag.add(Stream.value(1).listen(null));
  await bag.addAll([
    Stream.value(2).listen(null),
    Stream.periodic(const Duration(seconds: 1)).listen(null),
  ]);

  // disposedBy
  await Stream.value(3).listen(null).disposedBy(bag);
  await StreamController<int>.broadcast().disposedBy(bag);
  await StreamController<int>.broadcast(sync: true).disposedBy(bag);

  // await before clearing
  await Future.delayed(const Duration(seconds: 1));
  unawaited(bag.clear());
  unawaited(bag.clear());
  unawaited(bag.clear());

  // adding after clearing
  await Future.delayed(const Duration(seconds: 1));
  await Stream.periodic(const Duration(milliseconds: 100), (i) => i)
      .listen(print)
      .disposedBy(bag);

  // await before disposing
  await Future.delayed(const Duration(seconds: 2));
  await bag.dispose();
  print("Bag disposed: ${bag.isDisposed}. It's all good");
}
17
likes
110
pub points
82%
popularity

A package to help disposing Streams and closing Sinks easily.

Repository (GitHub)
View/report issues

Documentation

API reference

Uploader

hoc081098@gmail.com

License

MIT (LICENSE)

Dependencies

collection, meta

More

Packages that depend on disposebag