attach method

Future<AppSession> attach({
  1. required Uri vmUri,
  2. required DeviceTarget device,
  3. bool rebuild = false,
  4. bool observe = false,
})

Attach (or switch to) the app at vmUri on device. A pooled session bound to the same VM is activated as-is; a different app on the same device replaces the old entry. Other pooled apps stay connected. rebuild replaces even a live session of the same app, to move it to another input backend.

Implementation

Future<AppSession> attach({
  required Uri vmUri,
  required DeviceTarget device,
  bool rebuild = false,
  bool observe = false,
}) async {
  final existing = _pool[device.id];
  if (!rebuild && existing != null && !existing.deviceMode && existing.vmUri == vmUri && existing.isLive) {
    _active = existing;
    return existing;
  }
  CallProgress.checkAlive();
  if (existing != null) await _drop(existing);
  final key = _vmKey(vmUri);
  final retained = _retainedLogs.where((r) => r.key == key).firstOrNull;
  final app = await AppSession.connect(
    vmUri: vmUri,
    device: device,
    runtimeFactory: _runtimeFactory,
    appLogCapacity: config.appLogCapacity,
    showTouches: !observe && config.showTouches,
    appLogs: retained?.buffer,
  );
  if (CallProgress.cancelled) {
    await app.dispose();
    CallProgress.checkAlive();
  }
  _retainedLogs.remove(retained);
  _noteLostLogs(device.id, vmUri);
  app.captureSettleMs = config.captureSettleMs;
  _pool[device.id] = app;
  _ownedDevices.add(device.id);
  _active = app;
  watches.appAttached(app);
  return app;
}