简体   繁体   English

如何对baconJS中的takeUntil流做出反应

[英]How to react on a takeUntil stream in baconJS

I have 3 event streams for mouseDown, mouseUp and mouseMove. 我有3个事件流,分别为mouseDown,mouseUp和mouseMove。 This works so far that when a user not hit the alt Key it does something on mouse move until mouse up. 到目前为止,这种方法一直有效,以至于当用户没有按下alt键时,它会在鼠标移动之前执行某些操作,直到鼠标向上移动为止。

this.mouseDown
   .filter((e) =>!e.altKey)
   .flatMap(() => this.mouseMove.takeUntil(this.mouseUp))
   .onValue((e) => this.setState({previewEndPoint: this.getNearestPoint(e)}));

My question is how can I react on the mouse up event in this case? 我的问题是在这种情况下如何应对鼠标上移事件?

Setting a variable in doAction and using it elsewhere is really bad sign and unnecessary. doAction设置变量并在其他地方使用它确实是一个不好的信号,而且没有必要。 I refactored your code to use combined streams instead. 我将您的代码重构为使用组合流。

var moveStart = this.mousedown
  .filter((e) =>!e.altKey && !e.metaKey)    

var move = moveStart.flatMap((e) =>
    this.mousemove
        .takeUntil(this.mouseup)
        .startWith(this.getNearestPoint(e))
  );

var startPoint = moveStart.map((e) => this.getNearestPoint(e))

var endPoint = move.map((e) => this.getNearestPoint(e))

var preview = startPoint.combine(endPoint, (start, end) => {
    preview: true,
    startPoint: start,
    previewEndPoint: end
})

preview.onValue((state) => this.setState(state));

move.onEnd(this.endLine.bind(this));

I had to create a stream that ist started by mouse down and is converted into the mouse move event using flatMap which will then stop on mouse up. 我必须创建一个流,该流由鼠标按下开始,并使用flatMap转换为鼠标移动事件,然后将鼠标停下。 Then I have just to listen to onValue and onEnd 然后我只需要听onValueonEnd

var move = this.mousedown
  .filter((e) =>!e.altKey && !e.metaKey)
  .doAction((e) => startPoint = this.getNearestPoint(e))
  .flatMap(() => this.mousemove.takeUntil(this.mouseup));

move.onValue((e) => this.setState({
  preview: true,
  startPoint: startPoint,
  previewEndPoint: this.getNearestPoint(e)
}));

move.onEnd(this.endLine.bind);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM