registerTask method

void registerTask(
  1. LikeSyncTask task
)

Registers a task in the synchronization queue.

Tasks are executed based on their LikeSyncPriority. If a task with the same ID already exists, its priority will be upgraded if the new task has a higher priority level.

Implementation

void registerTask(LikeSyncTask task) {
  if (_isDisposed) return;
  final existingTask = _pendingTasks[task.id];
  if (existingTask != null) {
    if (task.priority.index < existingTask.priority.index) {
      LikeLogger.log(
        level: LikeLogLevel.info,
        category: 'sync',
        message:
            'Upgrading task ${task.id} priority: ${existingTask.priority.name} -> ${task.priority.name}',
      );
      _taskQueue.remove(existingTask);
      _taskQueue.add(task);
      _pendingTasks[task.id] = task;
    }
    return;
  }

  if (!_isProcessing && _taskQueue.isEmpty) {
    _totalTasks = 0;
    _completedTasks = 0;
    _hasRecoveryTask = false;
  }

  if (task.isRecovery) _hasRecoveryTask = true;

  _totalTasks++;
  _taskQueue.add(task);
  _pendingTasks[task.id] = task;

  LikeLogger.log(
    level: LikeLogLevel.info,
    category: 'sync',
    message:
        'Registered sync task: ${task.id} (Priority: ${task.priority.name}, Total: $_totalTasks)',
  );

  _updateProgress();

  if (LikeConnectivityManager().hasConnection && !_isProcessing) {
    unawaited(processQueue());
  }
}