lazy_broadcast 0.0.1

  • README.md
  • Installing
  • Versions
  • new53

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]
23
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
60
Overall:
Weighted score of the above. [more]
53
Learn more about scoring.

We analyzed this package on Oct 10, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0
  • pana: 0.12.4

Platforms

Detected platforms: Flutter, web, other

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

Maintenance issues and suggestions

Maintain CHANGELOG.md. (-20 points)

Changelog entries help clients to follow the progress in your code.

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package. Common file name patterns include: main.dart, example.dart or you could also use lazy_broadcast.dart.

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

While there is nothing inherently wrong with versions of 0.0.*, it usually means 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