close method

Future? close()

Free Runtime and close isolate thread that can be recreate when evaluate again.

Implementation

Future<dynamic>? close() {
  // Host-function handlers are reclaimed by the existing IsolateFunction
  // refcount path: when the worker frees its runtime on #close, the bound
  // globalThis functions are GC'd, and their cross-isolate #free messages
  // remove the handlers registered here. Manually destroying them up-front
  // races those late messages ("handler released"), so we don't.
  _hostFunctions.clear();
  _hostFunctionsBound = false;
  final sendPort = _sendPort;
  _sendPort = null;
  if (sendPort == null) return null;
  final ret = sendPort.then((sendPort) async {
    final closePort = ReceivePort();
    sendPort.send({#type: #close, #port: closePort.sendPort});
    final result = await closePort.first;
    closePort.close();
    if (result is Map && result.containsKey(#error)) {
      throw _decodeData(result[#error]);
    }
    return _decodeData(result);
  });
  return ret;
}