RxJS重试整个链

时间:2018-03-31 21:01:49

标签: javascript rxjs observable retry-logic

我从实时流中读取图像并定期选择批次。 然后我将它们发送到服务器进行验证。如果任何失败验证,将抛出HTTP错误。如果发生这种情况,我想获得一批新的图像。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace Form
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class NewBookPage : ContentPage
    {
        public NewBookPage ()
        {
            InitializeComponent ();
        }

        private void Button_Clicked(object sender, EventArgs e)
        {
            Book book = new Book()
            {
                Name =  nameEntry.Text,
                Author = authorEntry.Text

            };
            DisplayAlert("Success",book.Name+"/"+book.Author, "great!");
        }
    }
}

我遇到的问题是只有HTTP请求被重试,因为那是新的observable。必须有一些方法将链的开头封装到一个Observable中吗?

2 个答案:

答案 0 :(得分:1)

learnrxjs - retry。该示例显示了在抛出错误时从源代码开始重新启动的所有内容。

该页面显示pipe语法,但如果您愿意,JSBin会显示流畅的操作符语法。

基本模式是

const retryMe = this.input.getImages()
  .flatMap(val => {
    Observable.of(val)
      // more operators
  })
  .retry(2);

答案 1 :(得分:1)

简单的方法是将复杂的 observable 包装在 defer 中,然后对结果 observable 使用重试。