本文整理汇总了TypeScript中rxjs/operators.retryWhen函数的典型用法代码示例。如果您正苦于以下问题:TypeScript retryWhen函数的具体用法?TypeScript retryWhen怎么用?TypeScript retryWhen使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了retryWhen函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: load
load(url: string): Observable<any> {
return Observable.create(observer => {
let xhr = new XMLHttpRequest();
let onLoad = function () {
if (xhr.status === 200) {
observer.next(JSON.parse(xhr.responseText));
observer.complete(); //有 complete(), 似乎正常執行時會呼叫unsubscribe()
} else {
observer.error(xhr.statusText);
}
};
xhr.addEventListener('load', onLoad);
xhr.open('GET', url);
xhr.send();
return () => {
console.log('in unsubscribe');
xhr.removeEventListener('load', onLoad);
xhr.abort();
}
}).pipe(
retryWhen(this.retryStrategy({attempts: 4, delayT: 1500}))
);
}
示例2: retryWhen1
retryWhen1() {
// emit value every 1s
const source = interval(1000);
const example = source.pipe(
map(val => {
if (val > 5) {
// error will be picked up by retryWhen
throw val;
}
return val;
}),
retryWhen(errors =>
errors.pipe(
// log error message
tap(val => console.log(`Value ${val} was too high!`)),
// restart in 5 seconds
delayWhen(val => timer(val * 1000))
)
)
);
/*
output:
0
1
2
3
4
5
"Value 6 was too high!"
--Wait 5 seconds then repeat
*/
const subscribe = example.subscribe(val => console.log(val));
}
示例3: retry
retry() {
// This fake async request will fail the first time
let fails = 0;
const fakeAsync = async(new Date().toLocaleTimeString(), () => ++fails <= 1);
// Add a retry routine using an alert
const fakeAsyncWithRetries = fakeAsync.pipe(retryWhen(errors => {
return errors.pipe(switchMap(err => {
return this.alert.open({
text: 'Retry?',
type: AlertType.Warning,
showCancelButton: true,
});
}));
}));
this.alert.open({
text: 'Show current time? (will fail the first time)',
showCancelButton: true,
confirmAction: fakeAsyncWithRetries
}).subscribe(result => {
this.alert.info(result.value, { title: 'Time' });
}, err => {
console.log(err);
this.alert.error(String(err ? err.reason : err), { title: 'Error' });
});
}
示例4: absEndpoint
delete<T>(endpoint: string, headers?: HttpHeaders) : Observable<T> {
return this.http.delete<T>(
absEndpoint(endpoint),
{ headers: authHeaders(headers) }
).pipe(
catchError(unathorizeHandler(this.router)),
retryWhen(errorRetry())
);
}
示例5: rawPost
rawPost(endpoint: string, body?: any, headers?: HttpHeaders) : Observable<HttpResponse<string>> {
return this.http.post(
absEndpoint(endpoint),
body,
{ headers: authHeaders(headers), observe: "response", responseType: "text" }
).pipe(
catchError(unathorizeHandler(this.router)),
retryWhen(errorRetry())
);
}
示例6: backoff
function backoff(maxTries, ms) {
return pipe(
retryWhen(attempts => zip(range(1, maxTries), attempts)
.pipe(
map(([i]) => i * i),
mergeMap(i => timer(i * ms))
)
)
);
}
示例7: backoff
function backoff(maxTries, ms) {
return pipe(
retryWhen(attempts => range(1, maxTries)
.pipe(
zip(attempts, (i) => i),
map(i => i * i),
mergeMap(i => timer(i * ms))
)
)
);
}
示例8: loadWithFetch
loadWithFetch(url: string): Observable<any> {
return Observable.defer(
() => Observable.fromPromise(fetch(url).then(
r => {
if (r.status === 200) {
return r.json();
} else {
return Promise.reject(r.statusText);
}
}))
).pipe(
retryWhen(this.retryStrategy())
);
}
示例9: retryWhen
export function retryAfter<T>(
count: number,
wait: number
): (source: Observable<T>) => Observable<T> {
return retryWhen(errors => errors.pipe(
// Each time an error occurs, increment the accumulator.
// When the maximum number of retries have been attempted, throw the error.
scan((acc, error) => {
if (acc >= count) { throw error; }
return acc + 1;
}, 0),
// Wait the specified number of milliseconds between retries.
delay(wait)
));
}
示例10: higherOrder
export function retryWhen<T>(this: Observable<T>, notifier: (errors: Observable<any>) => Observable<any>): Observable<T> {
return higherOrder(notifier)(this) as Observable<T>;
}