t-tasks 2.1.0 → 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/t-tasks.js CHANGED
@@ -519,31 +519,40 @@ let Task;
519
519
  }
520
520
  _Task.all = all;
521
521
  function any(tasks) {
522
- const cell = settleCell();
523
522
  const taskArray = Array.from(tasks);
524
- if (taskArray.length < 1) return Task.canceled();
525
- const cancel = (error) => taskArray.map((task) => task._cancel(error)).every((r) => r);
526
- const list = taskArray.map(() => Maybe.nothing());
527
- const resolved = (value) => {
528
- cell.resolve(value.map(Either.right));
529
- cancel(Maybe.nothing());
530
- };
531
- const rejected = (error, i) => {
532
- list[i] = Maybe.just(error);
533
- if (Maybe.everyJust(list)) cell.resolve(Maybe.just(Either.left(list.map((v) => Just.just(v)))));
534
- };
535
- taskArray.map((task, i) => {
536
- return task.matchTap({
537
- resolved: (value) => resolved(Maybe.just(value)),
538
- rejected: (error) => rejected(error, i),
539
- canceled: () => resolved(Maybe.nothing())
523
+ if (taskArray.length === 0) return Task.rejected([]);
524
+ return Task.fromCallback((resolve, reject, cancel) => {
525
+ const errors = taskArray.map(() => Maybe.nothing());
526
+ let remaining = taskArray.length;
527
+ const checkAllFailed = () => {
528
+ remaining--;
529
+ if (remaining === 0) reject(errors.map((e) => e.match({
530
+ just: (err) => err,
531
+ nothing: () => void 0
532
+ })));
533
+ };
534
+ taskArray.forEach((task, i) => {
535
+ task.matchTap({
536
+ resolved: (value) => {
537
+ resolve(value);
538
+ taskArray.forEach((t) => t.cancel());
539
+ },
540
+ rejected: (error) => {
541
+ errors[i] = Maybe.just(error);
542
+ checkAllFailed();
543
+ },
544
+ canceled: () => {
545
+ cancel();
546
+ taskArray.forEach((t) => t.cancel());
547
+ }
548
+ });
540
549
  });
541
- });
542
- return Task.create(cell.promise, cancel);
550
+ return taskArray;
551
+ }, (subtasks) => subtasks.forEach((t) => t.cancel()));
543
552
  }
544
553
  _Task.any = any;
545
554
  function limit(task, limitTask) {
546
- return Task.any([task, limitTask.chain(Task.canceled)]).chainRejected((errors) => Task.rejected(errors[0]));
555
+ return Task.any([task, limitTask.chain(Task.canceled)]);
547
556
  }
548
557
  _Task.limit = limit;
549
558
  function repeat(taskFunction) {