attach method

Future<ProjectBrowserSessionOutcome> attach({
  1. Uri? url,
  2. bool replace = false,
})

Attaches the session to a target.

url overrides the declared endpoint; without it the profile's serve endpoint is used. replace restarts an owned session instead of returning the current one.

Implementation

Future<ProjectBrowserSessionOutcome> attach({
  Uri? url,
  bool replace = false,
}) async {
  if (_attached && !replace) {
    return _record(
      ProjectBrowserSessionOutcome(
        ok: true,
        url: _url,
        ownership: _ownership,
        pid: _pid,
      ),
    );
  }
  final target = url ?? _declaredTarget();
  if (target == null) {
    return _record(
      _failure(
        'project_browser_target_unavailable',
        'the profile "${profile.id}" declares no endpoint and no url was '
            'given',
      ),
    );
  }
  if (replace && _ownership == ProjectSessionOwnership.owned) {
    await _stopOwned();
  }
  if (!_isLoopback(target)) {
    // A remote target is somebody else's: navigate to it, own nothing,
    // probe nothing.
    return _record(
      _borrowed(target, detail: 'remote target; not owned and not probed'),
    );
  }
  if (await _isReachable(target)) {
    return _record(_borrowed(target, detail: 'endpoint already served'));
  }
  final operation = profile.operation(ProjectOperationKind.serve);
  if (operation == null) {
    return _record(
      _failure(
        'project_browser_endpoint_unreachable',
        'no serve operation is declared for the profile "${profile.id}"',
      ),
    );
  }
  return _record(await _spawnAndAttach(operation, target));
}