computed 0.5.1 copy "computed: ^0.5.1" to clipboard
computed: ^0.5.1 copied to clipboard

A performant and flexible reactive programming framework for Dart

example/example.dart

import 'dart:async';

import 'package:built_collection/built_collection.dart';
import 'package:computed/computed.dart';

void main() async {
  final controller = StreamController<BuiltList<int>>(
      sync: true); // Use a sync controller to make debugging easier
  final source = controller.stream.asBroadcastStream();

  final anyNegative = $(() => source.use.any((element) => element < 0));

  final maybeReversed =
      $(() => anyNegative.use ? source.use.reversed.toBuiltList() : source.use);

  final append0 = $(() {
    return maybeReversed.use.rebuild((p0) => p0.add(0));
  });

  Computed.effect(() => print(append0.use));

  // ignore: unused_local_variable
  final unused = $(() {
    while (true) {
      print("Never prints, this computation is never used.");
    }
  });

  controller.add([1, 2, -3, 4].toBuiltList()); // prints [4, -3, 2, 1, 0]
  controller.add([1, 2, -3, -4].toBuiltList()); // prints [-4, -3, 2, 1, 0]
  controller.add([4, 5, 6].toBuiltList()); // prints [4, 5, 6, 0]
  controller.add([4, 5, 6].toBuiltList()); // Same result: Not printed again
}
5
likes
160
pub points
19%
popularity

Publisher

unverified uploader

A performant and flexible reactive programming framework for Dart

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

meta

More

Packages that depend on computed