addTask method

Future<_TaskInfo> addTask(
  1. Function doSomething
)

Implementation

Future<_TaskInfo> addTask(Function doSomething) {
  _isCancelQueue = false;
  _mId++;
  _TaskInfo taskInfo = _TaskInfo(_mId, doSomething);

  /// 创建future
  Completer<_TaskInfo> taskCompleter = Completer<_TaskInfo>();

  /// 创建当前任务stream
  StreamController<_TaskInfo> streamController = StreamController();
  taskInfo.controller = streamController;

  /// 添加到任务队列
  _taskList.add(taskInfo);

  /// 当前任务的stream添加监听
  streamController.stream.listen((_TaskInfo completeTaskInfo) {
    if (completeTaskInfo.id == taskInfo.id) {
      taskCompleter.complete(completeTaskInfo);
      streamController.close();
    }
  });

  /// 触发任务
  _doTask();

  return taskCompleter.future;
}