lazy_broadcast 0.0.1

lazy-broadcast #

The problem #

With Flutter came the BLoC pattern, a new way of storing state to increase code-sharing with web.

This patterns relies heavily on broadcast streams. But this causes problems, as broadcast streams are not "pausable".

Which means that as soon as you start listening to a broadcast stream once, then even if you later remove the subscription; the stream still continuously runs in background!

import 'dart:async';

Stream<int> createStream() async* {
  int i = 0;
  while (true) {
    yield i;
    i++;
    print('compute');
    await Future.delayed(const Duration(seconds: 1));
  }
}

Future main() async {
  final foo = createStream().asBroadcastStream();
  // foo not running yet

  await for (final val in foo) {
    if (val > 5) {
      break;
    }
    print(val);
  }
  // foo still runing !
}

which will print the following:

0
compute
1
compute
2
compute
3
compute
4
compute
5
compute
compute
compute
compute
compute
...

This is of course heavily inefficient.

The solution #

This library attempts to solve this problem by introducing a lazy broadcast stream.

Its job will be to pause the inner stream whenever the broadcast stream doesn't have subscribers anymore. It will then resume the inner stream as soon as there's a new subscriber.

Which means we can now do the following:

Stream broadcast = myStream.transform(LazyBroadcastTransformer());

After modifying our first example, we now have the following:

Stream<int> createStream() async* {
  int i = 0;
  while (true) {
    yield i;
    i++;
    print('compute');
    await Future.delayed(const Duration(seconds: 1));
  }
}

Future main() async {
  final foo = createStream().transform(LazyBroadcastTransformer());
  // foo not running yet

  await for (final val in foo) {
    if (val > 5) {
      break;
    }
    print(val);
  }
  // foo is paused !
}

which prints:

compute
0
compute
1
compute
2
compute
3
compute
4
compute
5
compute

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  lazy_broadcast: ^0.0.1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:lazy_broadcast/lazy_broadcast.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
--
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
--
Overall:
Weighted score of the above. [more]
0
Learn more about scoring.

This package is not analyzed, because it is discontinued.

Health issues and suggestions

Document public APIs. (-1 points)

4 out of 4 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0