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/src/task.ts CHANGED
@@ -647,7 +647,7 @@ export namespace Task {
647
647
  * @param tasks iterable of tasks to execute
648
648
  * @returns task resolving to the list of results
649
649
  */
650
- export function all<T>(tasks: Iterable<Task<T>>) {
650
+ export function all<T>(tasks: Iterable<Task<T>>): Task<T[]> {
651
651
  const taskArray = Array.from(tasks);
652
652
  if (taskArray.length === 0) {
653
653
  return Task.resolved([]);
@@ -693,41 +693,46 @@ export namespace Task {
693
693
  * @param tasks iterable of tasks to execute
694
694
  * @returns task resolving to the result of first successfull task
695
695
  */
696
- export function any<T>(tasks: Iterable<Task<T>>) {
697
- const cell = settleCell<Cancelable<T>>();
698
-
696
+ export function any<T>(tasks: Iterable<Task<T>>): Task<T> {
699
697
  const taskArray = Array.from(tasks);
700
698
 
701
- if (taskArray.length < 1) {
702
- return Task.canceled();
699
+ if (taskArray.length === 0) {
700
+ return Task.rejected([]);
703
701
  }
704
702
 
705
- const cancel = (error: Maybe<any>) => taskArray.map((task) => task._cancel(error)).every((r) => r);
706
-
707
- const list = taskArray.map<Maybe<any>>(() => Maybe.nothing());
708
-
709
- const resolved = (value: Maybe<T>) => {
710
- cell.resolve(value.map(Either.right));
711
- cancel(Maybe.nothing());
712
- };
713
-
714
- const rejected = (error: any, i: number) => {
715
- list[i] = Maybe.just(error);
703
+ return Task.fromCallback<Task<T>[], T>((resolve, reject, cancel) => {
704
+ const errors: Maybe<any>[] = taskArray.map(() => Maybe.nothing());
705
+ let remaining = taskArray.length;
716
706
 
717
- if (Maybe.everyJust(list)) {
718
- cell.resolve(Maybe.just(Either.left(list.map((v) => Just.just(v)))));
719
- }
720
- };
707
+ const checkAllFailed = () => {
708
+ remaining--;
709
+ if (remaining === 0) {
710
+ const rawErrors = errors.map((e) =>
711
+ e.match({ just: (err) => err, nothing: () => undefined })
712
+ );
713
+ reject(rawErrors);
714
+ }
715
+ };
721
716
 
722
- taskArray.map((task, i) => {
723
- return task.matchTap({
724
- resolved: (value) => resolved(Maybe.just(value)),
725
- rejected: (error) => rejected(error, i),
726
- canceled: () => resolved(Maybe.nothing()),
717
+ taskArray.forEach((task, i) => {
718
+ task.matchTap({
719
+ resolved: (value) => {
720
+ resolve(value);
721
+ taskArray.forEach((t) => t.cancel());
722
+ },
723
+ rejected: (error) => {
724
+ errors[i] = Maybe.just(error);
725
+ checkAllFailed();
726
+ },
727
+ canceled: () => {
728
+ cancel();
729
+ taskArray.forEach((t) => t.cancel());
730
+ },
731
+ });
727
732
  });
728
- });
729
733
 
730
- return Task.create(cell.promise, cancel);
734
+ return taskArray;
735
+ }, (subtasks) => subtasks.forEach((t) => t.cancel()));
731
736
  }
732
737
 
733
738
  /**
@@ -742,7 +747,7 @@ export namespace Task {
742
747
  * @returns task limites according to limitTask
743
748
  */
744
749
  export function limit<T>(task: Task<T>, limitTask: Task<void>) {
745
- return Task.any<T>([task, limitTask.chain<T>(Task.canceled)]).chainRejected((errors) => Task.rejected<T>(errors[0]));
750
+ return Task.any<T>([task, limitTask.chain<T>(Task.canceled)]);
746
751
  }
747
752
 
748
753
  /**