ctx 1.0.0 copy "ctx: ^1.0.0" to clipboard
ctx: ^1.0.0 copied to clipboard

Immutable context for carrying data through an application.

ctx #

An immutable context for carrying data, deadlines, and cancellation signals through your application.

Usage #

Carrying Values #

import 'package:ctx/ctx.dart';

void main() {
  final ctx = Context.empty()
      .withValue('user', 'alice')
      .withValue('role', 'admin');

  print(ctx['user']); // alice

  // Derive a new context without mutating the original:
  final scoped = ctx.withValue('role', 'editor');
  print(ctx['role']);    // admin
  print(scoped['role']); // editor
}

Cancellation & Timeouts #

// Explicit cancellation
final (ctx, cancel) = Context.empty().withCancel();
cancel();

// Automatic timeout
final (ctxTimeout, cancelTimeout) = Context.empty().withTimeout(Duration(seconds: 5));

// Non-cancelable branch
final detached = ctx.withoutCancel();

Zone Propagation #

You can propagate a context implicitly down an execution tree using run:

final ctx = Context.empty().withValue('user', 'alice');

ctx.run(() {
  print(Context.current['user']); // alice
});
0
likes
160
points
205
downloads

Documentation

API reference

Publisher

verified publisherpusk.software

Weekly Downloads

Immutable context for carrying data through an application.

Repository (GitHub)
View/report issues

License

MIT (license)

More

Packages that depend on ctx