當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。