/
stdio_overrides.dart
49 lines (41 loc) · 1.39 KB
/
stdio_overrides.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import 'dart:async';
import 'dart:io' as io;
const _asyncRunZoned = runZoned;
/// This class facilitates overriding [io.stdioType].
abstract class StdioOverrides {
static final _token = Object();
/// Returns the current [StdioOverrides] instance.
///
/// This will return `null` if the current [Zone] does not contain
/// any [StdioOverrides].
///
/// See also:
/// * [StdioOverrides.runZoned] to provide [StdioOverrides]
/// in a fresh [Zone].
///
static StdioOverrides? get current {
return Zone.current[_token] as StdioOverrides?;
}
/// Runs [body] in a fresh [Zone] using the provided overrides.
static R runZoned<R>(
R Function() body, {
io.StdioType Function(dynamic object) Function()? stdioType,
}) {
final overrides = _StdioOverridesScope(stdioType);
return _asyncRunZoned(
body,
zoneValues: {_token: overrides},
);
}
/// The [io.stdioType] that will be used for errors within the current [Zone].
io.StdioType Function(dynamic object) get stdioType => io.stdioType;
}
class _StdioOverridesScope extends StdioOverrides {
_StdioOverridesScope(this._stdioType);
final StdioOverrides? _previous = StdioOverrides.current;
final io.StdioType Function(dynamic object) Function()? _stdioType;
@override
io.StdioType Function(dynamic object) get stdioType {
return _stdioType?.call() ?? _previous?.stdioType ?? super.stdioType;
}
}