contextdart 1.2.1 copy "contextdart: ^1.2.1" to clipboard
contextdart: ^1.2.1 copied to clipboard

pure golang context.Context implements in dart

example/example.dart

import 'package:contextdart/contextdart.dart';

class Hello {}

void main() async {
  var ctx = Context.withValue([
    const MapEntry("some comparable key", "value will put into context"),
    Hello(),
  ]);

  ctx.run(() {
    getValueFromContextByKey();
    getValueFromContextByType();
    canceledContext();
  });
}

getValueFromContextByKey() {
  // in deep function;
  var v = Context.value("some comparable key");
  // ignore: avoid_print
  print(v);
}

getValueFromContextByType() {
  // in deep function;
  var v = Context.value<Hello>();
  // ignore: avoid_print
  print(v);
}

canceledContext() {
  var cctx = Context.withCancel();

  cctx.run(() {
    // async action
    Future.any([
      Context.done?.first,
      Stream.periodic(const Duration(seconds: 1))
          .map((e) => Exception("bomb!"))
          .first,
    ].whereType<Future>());

    // cancel cause something like timeout
    cctx.cancel();
  });
}
copied to clipboard
2
likes
140
points
81
downloads

Publisher

verified publisherpub.v42.one

Weekly Downloads

2024.07.06 - 2025.01.18

pure golang context.Context implements in dart

Repository (GitHub)

Documentation

API reference

License

MIT (license)

More

Packages that depend on contextdart