0 purchases
contextdart
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");
});
}
copied to clipboard
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.
});
}
copied to clipboard
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.