submit<T, R> method

Future<R> submit<T, R>(
  1. T taskData,
  2. R processor(
    1. T
    ), {
  3. int priority = 0,
})

Submits a frame processing task to the pool.

taskData — Serializable task payload for the isolate worker. processor — Top-level or static function to execute in the isolate. priority — Higher priority tasks are processed first. Default: 0.

Returns a Future that completes with the processing result. If the queue is full, the oldest low-priority task is dropped.

Implementation

Future<R> submit<T, R>(
  T taskData,
  R Function(T) processor, {
  int priority = 0,
}) async {
  if (!_isInitialized || _isShuttingDown) {
    // Fallback to compute() if pool not ready
    return compute(processor, taskData);
  }

  final completer = Completer<R>();
  final task = _FrameTask<T, R>(
    data: taskData,
    processor: processor,
    completer: completer,
    priority: priority,
    submitTime: DateTime.now(),
  );

  // Queue management: drop oldest low-priority if queue is full
  if (_taskQueue.length >= frameQueueDepth) {
    final dropIdx = _taskQueue.indexWhere((t) => t.priority <= 0);
    if (dropIdx >= 0) {
      final dropped = _taskQueue.removeAt(dropIdx);
      dropped.completer.completeError(
        FrameDroppedException('Frame dropped due to queue overflow'),
      );
      _droppedFrames++;
    } else {
      // All tasks are high priority; drop the oldest
      final dropped = _taskQueue.removeAt(0);
      dropped.completer.completeError(
        FrameDroppedException('Frame dropped due to queue overflow'),
      );
      _droppedFrames++;
    }
  }

  // Insert task maintaining priority order (highest priority first)
  int insertIdx = _taskQueue.length;
  for (int i = 0; i < _taskQueue.length; i++) {
    if (_taskQueue[i].priority < priority) {
      insertIdx = i;
      break;
    }
  }
  _taskQueue.insert(insertIdx, task);

  // Try to dispatch immediately
  _dispatchPending();

  return completer.future;
}