caffeine 2.0.0 copy "caffeine: ^2.0.0" to clipboard
caffeine: ^2.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';

typedef LoggerState = ({int logsCount});

final logMessage = Event<String>();

final logger = Store<LoggerState>.accum((ctx) {
  ctx.on(logMessage, (msg) async* {
    print(msg);
    yield (logsCount: ctx.current.logsCount + 1);
  });
  return (logsCount: 0);
});

typedef RemoteConfigState = ({String apiUrl, int number});

final loadRemoteConfig = Event<void>();

Future<RemoteConfigState> fetchRemoteConfig() => throw 'hello';

final remoteConfig = Store<RemoteConfigState>.accum((ctx) {
  ctx.on(loadRemoteConfig, (_) async* {
    logMessage(ctx, 'Requesting remote config...');
    yield await fetchRemoteConfig();
  });
  loadRemoteConfig(ctx, null);

  return (apiUrl: 'https://example.com/api', number: 42);
});

final systemState = Store.derive(
  (source) => (
    url: remoteConfig(source).apiUrl,
    doubledMessages: logger(source).logsCount * 2,
  ),
);

void main() {}
0
likes
140
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