Luckey-Elijah / cached_streamable

Simple interface for creating a streamable data source that caches it latest value.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

cached_streamable

Simple interface for creating a streamable data source that caches it latest value.

Think of it as an "extended" StreamController.

MIT license style: very good analysis

Usage

  1. Create your implementation by extending CachedStreamable. Use the value getter and setter to update the cached value. You can use any single data type. This example uses int. (value getter is where you can access the latest data. value setter is where you can update the cache and notify listeners.)
class CounterRepository extends CachedStreamable<int> {
  CounterRepository() : super(0);

  // Some arbitrary future that updates the internal cached value
  Future<void> increment() =>
      Future<void>.delayed(Duration.zero, () => value = value + 1);
}
  1. Use the stream to access all the updates to the cache.
Future<void> main() async {
  // prints "0" when first listened to
  final repo = CounterRepository()..stream.listen(print);

  // prints "1"
  await repo.increment();
}
  1. Don't forget to call close when you are done.
await repo.close();

You don't have to extend the class. You can use CachedStreamable inline:

final counter = CachedStreamable<int>(0);
counter.value++;

About

Simple interface for creating a streamable data source that caches it latest value.

License:MIT License


Languages

Language:Dart 100.0%