execute method

Future<TResult> execute(
  1. TArg arg, {
  2. KWorkPriority priority = KWorkPriority.medium,
})

Enqueues arg for execution at the given priority.

Throws KIsolateException if the isolate is not running or the queue is full.

Implementation

Future<TResult> execute(TArg arg, {KWorkPriority priority = KWorkPriority.medium}) {
  if (!_running) throw const KIsolateException('Isolate is not running.');
  if (pendingCount >= maxQueueSize) {
    throw KIsolateException('Task queue is full ($maxQueueSize). Apply backpressure or raise maxQueueSize.');
  }

  final id = _nextId;
  _nextId = _nextId >= _maxId ? 0 : _nextId + 1;

  final completer = Completer<TResult>();
  _pending[id] = completer;

  final task = _Queued<TArg>(id, arg);
  switch (priority) {
    case KWorkPriority.high:
      _high.add(task);
    case KWorkPriority.medium:
      _med.add(task);
    case KWorkPriority.low:
      _low.add(task);
  }

  _next();
  return completer.future;
}