所以我有一些事件流:

let mouseUps = $(window)
  .asEventStream('mouseup');

let mouseDowns = $(window)
  .asEventStream('mousedown');

let mouseMoves = $(window)
  .asEventStream('mousemoves');

let drags = mouseDowns
  .flatMapLatest(() => mouseMoves.takeUntil(mouseUps));

let clicks = $(window)
  .asEventStream('click')
  .onValue(() => doThing());


我想忽略在拖动结束后立即触发的点击。我觉得必须有一个相当简单的方法来执行此操作,但是我仍在努力解决一些核心概念。

最佳答案

解决方案是使用stream.awaiting()创建一个属性,该属性可用于拖动后过滤掉点击。

var btn = $('#btn');
var mouseUps = btn.asEventStream('mouseup');
var mouseDowns = btn.asEventStream('mousedown');
var mouseMoves = btn.asEventStream('mousemove');
var drags = mouseDowns.flatMapLatest(function () {
    return mouseMoves.takeUntil(mouseUps);
});

var clicks = btn.asEventStream('click');
var isDraggingStarted = drags.awaiting(mouseDowns);

clicks.filter(isDraggingStarted.not()).onValue(doThing);


可以在此处找到示例:http://jsfiddle.net/a128jhxb/1/

10-06 05:04