submit method

Future<int> submit(
  1. void call(
    1. int callId,
    2. int nativePort
    )
)

Registers a pending call, invokes call with the freshly-assigned id and nativePort, and returns the future that completes when the batch carrying this id arrives. Throws a StateError if called after dispose — the port is closed, so the result could never arrive.

Implementation

Future<int> submit(void Function(int callId, int nativePort) call) {
  if (_disposed) {
    throw StateError('NitroCoalescer.submit() called after dispose()');
  }
  final id = _nextId++;
  final completer = Completer<int>();
  _pending[id] = completer;
  try {
    call(id, nativePort);
  } catch (_) {
    // The native call never reached the other side, so no batch will ever
    // carry this id — drop the slot instead of leaving a future that can
    // only resolve at dispose().
    _pending.remove(id);
    rethrow;
  }
  return completer.future;
}