Skip to content

Commit

Permalink
feat(loop): Adds a task for retrying another task until it succeeds
Browse files Browse the repository at this point in the history
  • Loading branch information
kofno committed Jan 11, 2019
1 parent bd13fd0 commit b29c031
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/Task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,42 @@ class Task<E, T> {
resolve(result);
}
};
// tslint:disable-next-line:prefer-for-of
for (let i = 0; i < ts.length; i++) {
ts[i].fork(reject, resolveIf);
}
return noop;
});
}

/**
* `loop` returns a higher-order task forks the "inner" task until the inner task
* succeeds. Retries forking the task on the interval number the same as using
* setTimeout.
*
* The cancel function returned when the loop is forked can be called to cancel the
* loop. Cancelling the loop causes the task to never resolve.
*/
public static loop<E, T>(interval: number, task: Task<E, T>): Task<never, T> {
return new Task<never, T>((_, resolve) => {
let timeout: number | undefined;
const loop = () => {
task.fork(
() => {
timeout = window.setTimeout(loop, interval);
},
result => resolve(result)
);
};

loop();

return () => {
clearTimeout(timeout);
};
});
}

private fn: Computation<E, T>;

constructor(computation: Computation<E, T>) {
Expand Down Expand Up @@ -192,7 +221,7 @@ class Task<E, T> {
*/
public assign<K extends string, A>(
k: K,
other: Task<E, A> | ((t: T) => Task<E, A>),
other: Task<E, A> | ((t: T) => Task<E, A>)
): Task<E, T & { [k in K]: A }> {
return this.andThen(t => {
const task = other instanceof Task ? other : other(t);
Expand Down

0 comments on commit b29c031

Please sign in to comment.