blob: 1df7d41624445556ef6941e03ba6a199d82eecfe [file] [log] [blame]
/********************************************************************************
* Copyright (c) 2020 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
********************************************************************************/
import {EMPTY, OperatorFunction, pipe, throwError, timer} from "rxjs";
import {catchError, concatMap, map, retryWhen} from "rxjs/operators";
/**
* If an error occurs, the observable is repeated after a given amount of time.
* @param timeInMs Time in miliseconds after which the observable is repeated.
* @param count Number of times the observable shall be repeated; if not specified, the observable is repeated until completion.
*/
export function retryAfter<T>(timeInMs: number, count?: number) {
let counter = 0;
return pipe(
retryWhen<T>((errors) => {
return errors.pipe(
concatMap((error) => {
if (typeof count === "number" && counter > count) {
return throwError(error);
}
counter++;
return timer(timeInMs).pipe(map(() => error));
})
);
})
);
}
/**
* If an error occurs, error is just ignored and the observable is finished without emitting any values.
*/
export function ignoreError<T>(): OperatorFunction<T, T> {
return catchError((err) => {
console.error(err);
return EMPTY;
});
}