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

retractedoutdated

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,
        () async {
      return "do action";
    }
  ].whereType<Future>());
}

doDBAction() async {
  Context.done()?.listen(() {
    // do some think like db rollback.
  });
}
2
likes
0
points
84
downloads

Publisher

verified publisherpub.v42.one

Weekly Downloads

pure golang context.Context implements in dart

Repository (GitHub)
View/report issues

License

unknown (license)

More

Packages that depend on contextdart