【发布时间】:2019-03-25 13:07:04
【问题描述】:
首先停止启动按钮,然后点击restore01按钮和restore02按钮恢复启动按钮,
那么错误是当点击停止按钮01或停止按钮02时停止按钮应该停止,但不是
注意console.log,启动按钮在启动时停止,然后单击restore01和restore02,然后启动按钮起作用,现在bug像冬天一样来了,当我在控制台中单击stop01按钮或stop02按钮时。 log it show stop stream Array [ "r", "" ] 但是开始按钮仍然有效,没有停止,你能想象一下我看到这种情况的感觉吗?天哪
代码很糟糕
const startDom = document.querySelector("#start")
const numberDom = document.querySelector("#number")
const restoreDom01 = document.querySelector("#restore01")
const stopDom01 = document.querySelector("#stop01")
const restoreDom02 = document.querySelector("#restore02")
const stopDom02 = document.querySelector("#stop02")
let init01 = false
let init02 = false
const origin01$ = new rxjs.Subject()
const origin02$ = new rxjs.Subject()
const restore01$ = rxjs.fromEvent(restoreDom01, "click")
restore01$.subscribe(() => origin01$.next("r"))
const stop01$ = rxjs.fromEvent(stopDom01, "click")
stop01$.subscribe(() => origin01$.next(""))
const restore02$ = rxjs.fromEvent(restoreDom02, "click")
restore02$.subscribe(() => origin02$.next("r"))
const stop02$ = rxjs.fromEvent(stopDom02, "click")
stop02$.subscribe(() => origin02$.next(""))
const [stopReg$, right$] = rxjs
.combineLatest(origin01$, origin02$)
.pipe(rxjs.operators.partition(arr => arr.some(isEmpty)));
const restoreReg$ = right$.pipe(
rxjs.operators.take(1),
rxjs.operators.repeatWhen(() => stopReg$)
);
const start$ = rxjs.fromEvent(startDom, "click");
start$
.pipe(
rxjs.operators.mapTo(1),
rxjs.operators.scan((acc, cur) => acc + cur, 0),
rxjs.operators.takeUntil(stopReg$),
rxjs.operators.repeatWhen(() => restoreReg$)
)
.subscribe(x => (numberDom.innerHTML = x));
stopReg$.subscribe(x => console.log("stop stream", x));
restoreReg$.subscribe(x => console.log("restore stream", x));
origin01$.next("")
origin02$.next("")
function isEmpty(n) {
return n === "";
}
可能不是很清楚,所以我告诉你步骤
点击2个恢复按钮,一个一个
点击开始按钮,点击后会看到数字加一
点击停止按钮,好的,这是个问题,现在开始按钮应该停止,但不是,你仍然可以添加一个点击开始按钮,但是在控制台你可以看到停止流被订阅,好奇怪
I also have other demo with the same problem using react and rxjs
【问题讨论】: