initialize method
Initializes the worker isolate and ODBC environment.
- Spawns a new isolate via Isolate.spawn.
- Loads the ODBC driver in the worker.
- Initializes the ODBC environment there.
- 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;
}