当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript operators.retryWhen函数代码示例

本文整理汇总了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}))
    );
  }
开发者ID:Ed-Lee,项目名称:PS-RxJS,代码行数:29,代码来源:data.service.ts

示例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));
 }
开发者ID:zwvista,项目名称:SampleMisc,代码行数:33,代码来源:error-handling.service.ts

示例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' });
    });
  }
开发者ID:ng-vcl,项目名称:ng-vcl,代码行数:27,代码来源:demo.component.ts

示例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())
     );
 }
开发者ID:urandom,项目名称:readeef,代码行数:9,代码来源:api.ts

示例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())
     );
 }
开发者ID:urandom,项目名称:readeef,代码行数:10,代码来源:api.ts

示例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))
     )
   )
 );
}
开发者ID:BobChao87,项目名称:angular,代码行数:10,代码来源:backoff.ts

示例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))
     )
   )
 );
}
开发者ID:DallanQ,项目名称:rxjs,代码行数:11,代码来源:backoff.ts

示例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())
   );
 }
开发者ID:Ed-Lee,项目名称:PS-RxJS,代码行数:14,代码来源:data.service.ts

示例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)
  ));
}
开发者ID:iproduct,项目名称:course-angular,代码行数:16,代码来源:rx-operators.ts

示例10: higherOrder

export function retryWhen<T>(this: Observable<T>, notifier: (errors: Observable<any>) => Observable<any>): Observable<T> {
  return higherOrder(notifier)(this) as Observable<T>;
}
开发者ID:DallanQ,项目名称:rxjs,代码行数:3,代码来源:retryWhen.ts


注:本文中的rxjs/operators.retryWhen函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。