lazy_broadcast 0.0.1

  • README.md
  • Installing
  • Versions
  • 42

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

with Flutter:


$ flutter packages get

Alternatively, your editor might support pub get or flutter packages 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';
  
Version Uploaded Documentation Archive
0.0.1 Oct 8, 2018 Go to the documentation of lazy_broadcast 0.0.1 Download lazy_broadcast 0.0.1 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
60
Overall:
Weighted score of the above. [more]
42
Learn more about scoring.

We analyzed this package on Feb 14, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.1.0
  • pana: 0.12.13

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:lazy_broadcast/lazy_broadcast.dart.

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.

Maintenance issues and suggestions

Provide a file named CHANGELOG.md. (-20 points)

Changelog entries help developers follow the progress of your package. See the example generated by stagehand.

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package.

Common filename patterns include main.dart, example.dart, and lazy_broadcast.dart. Packages with multiple examples should provide example/README.md.

For more information see the pub package layout conventions.

Package is pre-v0.1 release. (-10 points)

While nothing is inherently wrong with versions of 0.0.*, it might mean that the author is still experimenting with the general direction of the API.

Dependencies

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