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

pure golang context.Context implements in dart

pure golang context.Context implements in dart #

pub package ci

Totally inspected by context.Context from golang, and dart style fit.

Usage #

inject value or singleton in context

import 'package:contextdart/contextdart.dart';

class Logger {
  info(String msg) {
    
  }
}

void main() async {
  var ctx = Context.withValue(Logger());

  ctx.run(() {
    var log = Context.value<Logger>();

    log?.info("log");
  });
}

Cancelable context. #

import 'package:contextdart/contextdart.dart';

class Logger {
  info(String msg);
}

void main() async {
  var cctx = Context.withCancel();

  cctx.run(() {
    doAction();
    doDBAction();

    // cancel 
    cctx.cancel();
  });
}

doAction() async {
  return await Future.any([
    // if canceled, should ignore real action
    Context.done?.fisrt,
    Future(() => "do action"),
  ].whereType<Future>());
}

doDBAction() async {
  Context.done?.listen(() {
    // do some think like db rollback.
  });
}
2
likes
140
pub points
2%
popularity

Publisher

verified publisherpub.v42.one

pure golang context.Context implements in dart

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

More

Packages that depend on contextdart