Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/luckey-elijah/cached_streamable
Simple interface for creating a streamable data source that caches it latest value.
https://github.com/luckey-elijah/cached_streamable
cache dart dart-stream stream
Last synced: about 1 month ago
JSON representation
Simple interface for creating a streamable data source that caches it latest value.
- Host: GitHub
- URL: https://github.com/luckey-elijah/cached_streamable
- Owner: Luckey-Elijah
- License: mit
- Created: 2022-07-21T13:25:53.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-09-06T16:19:43.000Z (about 2 years ago)
- Last Synced: 2024-10-03T07:42:22.907Z (about 2 months ago)
- Topics: cache, dart, dart-stream, stream
- Language: Dart
- Homepage:
- Size: 18.6 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# `cached_streamable`
Simple interface for creating a streamable data source that caches it latest value.
*Think of it as an "extended" `StreamController`.*
[![MIT license](https://img.shields.io/badge/license-MIT-blue)](https://opensource.org/licenses/MIT)
[![style: very good analysis][very_good_analysis_badge]][very_good_analysis_link]## 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.)```dart
class CounterRepository extends CachedStreamable {
CounterRepository() : super(0);// Some arbitrary future that updates the internal cached value
Future increment() =>
Future.delayed(Duration.zero, () => value = value + 1);
}
```2. Use the `stream` to access all the updates to the cache.
```dart
Future main() async {
// prints "0" when first listened to
final repo = CounterRepository()..stream.listen(print);// prints "1"
await repo.increment();
}
```3. Don't forget to call `close` when you are done.
```dart
await repo.close();
```You don't have to extend the class. You can use `CachedStreamable` inline:
```dart
final counter = CachedStreamable(0);
counter.value++;
```[very_good_analysis_badge]: https://img.shields.io/badge/style-very_good_analysis-B22C89.svg
[very_good_analysis_link]: https://pub.dev/packages/very_good_analysis