contextdart 1.0.1 contextdart: ^1.0.1 copied to clipboard
pure golang context.Context implements in dart
pure golang context.Context
implements in dart #
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.
});
}