workerEntry function

void workerEntry(
  1. SendPort mainSendPort
)

Entry point for the worker isolate. Must be top-level or static.

mainSendPort is the SendPort of the main isolate's ReceivePort. The worker sends its own SendPort as the first message, then listens for WorkerRequest messages and responds with WorkerResponse.

Implementation

void workerEntry(SendPort mainSendPort) {
  final receivePort = ReceivePort();
  mainSendPort.send(receivePort.sendPort);

  late NativeOdbcConnection conn;
  try {
    conn = NativeOdbcConnection();
  } on Object catch (_) {
    mainSendPort.send(const InitializeResponse(0, success: false));
    // Close the port so the main isolate detects the channel death instead
    // of having pending requests hang until timeout.
    receivePort.close();
    return;
  }

  receivePort.listen((message) {
    if (message == 'shutdown') {
      conn.dispose();
      receivePort.close();
      return;
    }
    if (message is WorkerRequest) {
      _handleRequest(message, mainSendPort, conn);
    }
  });
}