runZoned<R> static method

R runZoned<R>(
  1. R Function() body, {
  2. Directory Function(String)? createDirectory,
  3. Directory Function()? getCurrentDirectory,
  4. void Function(String)? setCurrentDirectory,
  5. Directory Function()? getSystemTempDirectory,
  6. File Function(String)? createFile,
  7. Future<FileStat> Function(String)? stat,
  8. FileStat Function(String)? statSync,
  9. Future<bool> Function(String, String)? fseIdentical,
  10. bool Function(String, String)? fseIdenticalSync,
  11. Future<FileSystemEntityType> Function(String, bool)? fseGetType,
  12. FileSystemEntityType Function(String, bool)? fseGetTypeSync,
  13. Stream<FileSystemEvent> Function(String, int, bool)? fsWatch,
  14. bool Function()? fsWatchIsSupported,
  15. Link Function(String)? createLink,
  16. Future<Socket> Function(dynamic, int, {dynamic sourceAddress, int sourcePort, Duration? timeout})? socketConnect,
  17. Future<ConnectionTask<Socket>> Function(dynamic, int, {dynamic sourceAddress, int sourcePort})? socketStartConnect,
  18. Future<ServerSocket> Function(dynamic, int, {int backlog, bool shared, bool v6Only})? serverSocketBind,
  19. Stdin Function()? stdin,
  20. Stdout Function()? stdout,
  21. Stdout Function()? stderr,
  22. @Since.new("3.10") Never Function(int)? exit,
})

Runs body in a fresh Zone using the provided overrides.

See the documentation on the corresponding methods of IOOverrides for information about what the optional arguments do.

Implementation

static R runZoned<R>(
  R Function() body, {
  // Directory
  Directory Function(String)? createDirectory,
  Directory Function()? getCurrentDirectory,
  void Function(String)? setCurrentDirectory,
  Directory Function()? getSystemTempDirectory,

  // File
  File Function(String)? createFile,

  // FileStat
  Future<FileStat> Function(String)? stat,
  FileStat Function(String)? statSync,

  // FileSystemEntity
  Future<bool> Function(String, String)? fseIdentical,
  bool Function(String, String)? fseIdenticalSync,
  Future<FileSystemEntityType> Function(String, bool)? fseGetType,
  FileSystemEntityType Function(String, bool)? fseGetTypeSync,

  // _FileSystemWatcher
  Stream<FileSystemEvent> Function(String, int, bool)? fsWatch,
  bool Function()? fsWatchIsSupported,

  // Link
  Link Function(String)? createLink,

  // Socket
  Future<Socket> Function(
    dynamic,
    int, {
    dynamic sourceAddress,
    int sourcePort,
    Duration? timeout,
  })?
  socketConnect,
  Future<ConnectionTask<Socket>> Function(
    dynamic,
    int, {
    dynamic sourceAddress,
    int sourcePort,
  })?
  socketStartConnect,

  // ServerSocket
  Future<ServerSocket> Function(
    dynamic,
    int, {
    int backlog,
    bool v6Only,
    bool shared,
  })?
  serverSocketBind,

  // Standard Streams
  Stdin Function()? stdin,
  Stdout Function()? stdout,
  Stdout Function()? stderr,

  // Process exit
  @Since("3.10") Never Function(int)? exit,
}) {
  // Avoid building chains of override scopes. Just copy outer scope's
  // functions and `_previous`.
  var current = IOOverrides.current;
  _IOOverridesScope? currentScope;
  if (current is _IOOverridesScope) {
    currentScope = current;
    current = currentScope._previous;
  }
  IOOverrides overrides = _IOOverridesScope(
    current,
    // Directory
    createDirectory ?? currentScope?._createDirectory,
    getCurrentDirectory ?? currentScope?._getCurrentDirectory,
    setCurrentDirectory ?? currentScope?._setCurrentDirectory,
    getSystemTempDirectory ?? currentScope?._getSystemTempDirectory,

    // File
    createFile ?? currentScope?._createFile,

    // FileStat
    stat ?? currentScope?._stat,
    statSync ?? currentScope?._statSync,

    // FileSystemEntity
    fseIdentical ?? currentScope?._fseIdentical,
    fseIdenticalSync ?? currentScope?._fseIdenticalSync,
    fseGetType ?? currentScope?._fseGetType,
    fseGetTypeSync ?? currentScope?._fseGetTypeSync,

    // _FileSystemWatcher
    fsWatch ?? currentScope?._fsWatch,
    fsWatchIsSupported ?? currentScope?._fsWatchIsSupported,

    // Link
    createLink ?? currentScope?._createLink,

    // Socket
    socketConnect ?? currentScope?._socketConnect,
    socketStartConnect ?? currentScope?._socketStartConnect,

    // ServerSocket
    serverSocketBind ?? currentScope?._serverSocketBind,

    // Standard streams
    stdin ?? currentScope?._stdin,
    stdout ?? currentScope?._stdout,
    stderr ?? currentScope?._stderr,

    // Process exit
    exit ?? currentScope?._exit,
  );
  return dart_async.runZoned<R>(
    body,
    zoneValues: {_ioOverridesToken: overrides},
  );
}