initialize method

Future<bool> initialize()

Initializes the worker isolate and ODBC environment.

  1. Spawns a new isolate via Isolate.spawn.
  2. Loads the ODBC driver in the worker.
  3. Initializes the ODBC environment there.
  4. Returns when the worker is ready to accept requests.

One-time cost is typically ~50–100 ms. Safe to call multiple times; later calls return immediately if already initialized.

Returns true if initialization succeeds, false otherwise.

Implementation

Future<bool> initialize() async {
  if (_isInitialized) return true;

  final handshake = Completer<SendPort>();
  _receivePort = ReceivePort();
  _receivePort!.listen((Object? message) {
    if (message is SendPort) {
      if (!handshake.isCompleted) handshake.complete(message);
    } else if (message is WorkerResponse) {
      _handleResponse(message);
    }
  });

  _workerIsolate = await Isolate.spawn(
    workerEntry,
    _receivePort!.sendPort,
  );
  _workerSendPort = await handshake.future;

  final initResp = await _sendRequest<InitializeResponse>(
    InitializeRequest(_nextRequestId()),
  );
  return _isInitialized = initResp.success;
}