useSapSession<T> function

Future<T> useSapSession<T>(
  1. int rootHandle,
  2. int sessionIndex,
  3. Future<T> fn(
    1. GuiSession
    )
)

Use the pre-connected SAP root rootHandle to access session sessionIndex and run fn with it. Does NOT call SapObject.connect() — reuses the main isolate's COM connection.

Implementation

Future<T> useSapSession<T>(
  int rootHandle,
  int sessionIndex,
  Future<T> Function(GuiSession) fn,
) async {
  final api = SapApi();
  api.initialize();

  final root = SapObject(rootHandle, api, owned: false);
  if (root.isInvalid) {
    throw Exception('Invalid root handle: $rootHandle');
  }

  final app = GuiApplication(root);
  if (app.connections.count == 0) {
    throw Exception('[Isolate] no SAP connections');
  }

  final connection = app.connections.item(0);
  final count = connection.children.count;

  if (count <= sessionIndex) {
    throw Exception('Session $sessionIndex not found (have $count)');
  }

  final session = connection.children.item(sessionIndex);
  return await fn(session);
}