RxJS可观察到:先使用count然后使用notifier重试

时间:2018-08-27 17:24:48

标签: rxjs observable retrywhen

首先,我想使用一个简单的计数重试:

  • 重试源可观察selectionModel()次。
  • 然后,发出错误。

(最好在每次重试后立即发出错误,但是import sys from PyQt5 import QtCore, QtGui, QtWidgets from random import randint class Widget(QtWidgets.QWidget): def __init__(self, parent=None): super(Widget, self).__init__(parent) lay = QtWidgets.QVBoxLayout(self) button = QtWidgets.QPushButton("to list") button.clicked.connect(self.modelToList) self.listView = QtWidgets.QListView() lay.addWidget(button) lay.addWidget(self.listView) self.entry = QtGui.QStandardItemModel() self.listView.setModel(self.entry) for text in ("Itemname1", "Itemname2", "Itemname3", "Itemname4"): it = QtGui.QStandardItem(text) self.entry.appendRow(it) item = self.entry.item(randint(0, self.entry.rowCount()-1)) self.listView.setCurrentIndex(self.entry.indexFromItem(item)) def modelToList(self): l = [self.entry.item(i).text() for i in range(self.entry.rowCount())] print(l) if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) w = Widget() w.show() sys.exit(app.exec_()) 似乎不这样做。)

如果我理解正确,这就是class Vacation (models.Model): reference = models.CharField(max_length=30, default='test') start_date = models.DateField(default=0) end_date = models.DateField(default=0) start_hour = models.TimeField(default=0) end_hour = models.TimeField(default=0) monday = models.BooleanField() tuesday = models.BooleanField() wednesday = models.BooleanField() thursday = models.BooleanField() friday = models.BooleanField() saturday = models.BooleanField() sunday = models.BooleanField() @property # these are not the actual functions but only the results they give me def calcul_jours(self): work_days = [1, 1, 0, 0, 1, 1, 0] day_list = [6, 6 , 0, 0, 7, 7, 0] return day_list def calcul_heures(self): day_minutes = [180, 90, 0, 0, 90, 90, 90] night_minutes = [540, 360, 0, 0, 180, 360, 180] day_night = np.array([day_minutes, night_minutes]) return day_night def multiplication(self): days = Calcul.calcul_jours minutes = Calcul.calcul_heures multiplication = np.multiply(days, minutes) return multiplication 的行为:

n

然后,我想允许用户手动重试。当发出重试通知者可观察到的(retry(count))时,请重试可观察的对象,此后每次都发出错误。

我试图为此使用retry(count),但是,尽管重试确实发生了,但从未发出错误。

我想重试但还会发出任何错误,以便在重试运行时可以在用户界面中显示它们。

{
  new Rx.Observable(observer => {
    console.log("subscribe");
    observer.error("ERROR");
  })
    .retry(3)
    .subscribe({
      error: console.log
    });
}
// subscribe
// subscribe
// subscribe
// subscribe
// ERROR

此外,我不确定如何将其与retry$结合使用。如果我链接重试运算符,则它们会彼此触发。

1 个答案:

答案 0 :(得分:0)

retryWhen提供了一系列错误-您可以查看该错误流,并在发出3次发射后忽略它,然后仅在由用户触发时重试。

const retrySubject = new Rx.Subject();
const retry$ = retrySubject.asObservable();
new Rx.Observable(observer => {
  console.log("subscribe");
  observer.error("ERROR");
})
  .retryWhen(errors => Rx.Observable.merge(
    errors
      .do(error => console.log(error)) // log error
      .filter((error, index) => index < 3), // take only first 3 and ignore the rest
    retry$ // also retry with user request
  ))
  .subscribe();

retrySubject.next();

您可以使用take(3)代替filter,但这将停止errors流,因此错误记录也将停止。 filter操作员将使其保持“运行”状态。