caffeine 3.0.0 copy "caffeine: ^3.0.0" to clipboard
caffeine: ^3.0.0 copied to clipboard

A reactive microstore for Dart. Event-driven stateful stores, lazy derived state with automatic dependency tracking, glitch-free reactivity, and hierarchical scope-based lifecycle management.

example/caffeine_example.dart

import 'package:caffeine/caffeine.dart';

// Events
const increment = Event<void>(debugLabel: 'increment');
const setBy = Event<int>(debugLabel: 'setBy');
const resetAll = Event<void>(debugLabel: 'resetAll');

// Accum store: counter with three handlers.
final counter = Store<int>.accum(
  (ctx) {
    ctx.on(increment, (_) async* {
      yield ctx.current + 1;
    });
    ctx.on(setBy, (delta) async* {
      yield ctx.current + delta;
    });
    ctx.on(resetAll, (_) async* {
      yield 0;
    });
    return 0;
  },
  debugLabel: 'counter',
);

// Derived store: doubled view of counter. Recomputed automatically.
final doubled = counter.select((c) => c * 2, debugLabel: 'doubled');

Future<void> main() async {
  // resetAll is bound to the root scope — fires from anywhere broadcast here.
  final root = Scope(overrides: {resetAll});

  // Listen for state changes.
  root.stream(counter).listen((v) => print('counter -> $v'));
  root.stream(doubled).listen((v) => print('doubled -> $v'));

  // Fire some events.
  increment(root);
  increment(root);
  setBy(root, 5);
  await Future.delayed(Duration.zero);
  print('counter is ${root.read(counter)}, doubled is ${root.read(doubled)}');

  resetAll(root);
  await Future.delayed(Duration.zero);
  print('after reset: counter=${root.read(counter)}');

  root.dispose();
}
0
likes
150
points
102
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

A reactive microstore for Dart. Event-driven stateful stores, lazy derived state with automatic dependency tracking, glitch-free reactivity, and hierarchical scope-based lifecycle management.

Repository (GitHub)
View/report issues

Topics

#state-management #reactive #store

License

MIT (license)

Dependencies

meta

More

Packages that depend on caffeine