Add zoneValues to Chain.capture (#113)
Allows using fewer zones when there is a need for both custom zone
values and stack trace chaining.
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 7795328..348c507 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,4 +1,6 @@
-## 1.10.1-dev
+## 1.11.0-dev
+* Added the param `zoneValues` to `Chain.capture` to be able to use
+ custom zone values with the `runZoned` internal calls
## 1.10.0
diff --git a/lib/src/chain.dart b/lib/src/chain.dart
index b685be9..8a03b6c 100644
--- a/lib/src/chain.dart
+++ b/lib/src/chain.dart
@@ -72,20 +72,23 @@
/// If [errorZone] is `false`, [onError] must be `null`.
///
/// If [callback] returns a value, it will be returned by [capture] as well.
+ ///
+ /// [zoneValues] is added to the [runZoned] calls.
static T capture<T>(T Function() callback,
{void Function(Object error, Chain)? onError,
bool when = true,
- bool errorZone = true}) {
+ bool errorZone = true,
+ Map<Object?, Object?>? zoneValues}) {
if (!errorZone && onError != null) {
throw ArgumentError.value(
onError, 'onError', 'must be null if errorZone is false');
}
if (!when) {
- if (onError == null) return runZoned(callback);
+ if (onError == null) return runZoned(callback, zoneValues: zoneValues);
return runZonedGuarded(callback, (error, stackTrace) {
onError(error, Chain.forTrace(stackTrace));
- }) as T;
+ }, zoneValues: zoneValues) as T;
}
var spec = StackZoneSpecification(onError, errorZone: errorZone);
@@ -102,9 +105,11 @@
// where T is a nullable type continue to work.
return null as T;
}
- },
- zoneSpecification: spec.toSpec(),
- zoneValues: {_specKey: spec, StackZoneSpecification.disableKey: false});
+ }, zoneSpecification: spec.toSpec(), zoneValues: {
+ ...?zoneValues,
+ _specKey: spec,
+ StackZoneSpecification.disableKey: false
+ });
}
/// If [when] is `true` and this is called within a [Chain.capture] zone, runs
diff --git a/pubspec.yaml b/pubspec.yaml
index bd4065c..2775c0d 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,5 +1,5 @@
name: stack_trace
-version: 1.10.1-dev
+version: 1.11.0-dev
description: A package for manipulating stack traces and printing them readably.
homepage: https://github.com/dart-lang/stack_trace
diff --git a/test/chain/chain_test.dart b/test/chain/chain_test.dart
index 0931951..892738f 100644
--- a/test/chain/chain_test.dart
+++ b/test/chain/chain_test.dart
@@ -117,6 +117,12 @@
});
});
+ test('Chain.capture() with custom zoneValues', () {
+ return Chain.capture(() {
+ expect(Zone.current[#enabled], true);
+ }, zoneValues: {#enabled: true});
+ });
+
group('Chain.disable()', () {
test('disables chain-tracking', () {
return Chain.disable(() {