鼠标事件currentTarget覆盖+ Rxjs问题

时间:2018-01-04 12:06:54

标签: javascript rxjs

目前我正在使用Rxjs,看起来原始事件在处理时可能已被部分丢弃。 CurrentTarget消失了。



const canvasMousedown$ = Rx.Observable.fromEvent(chart, 'mousedown');
const canvasMousemove$ = Rx.Observable.fromEvent(chart, 'mousemove');
const canvasMouseup$ = Rx.Observable.fromEvent(chart, 'mouseup');

const canvasSelection$ = canvasMousedown$.concatMap((startEvent) => {
  return canvasMousemove$.takeUntil(canvasMouseup$).map((endEvent) => [startEvent, endEvent])
});

canvasSelection$.subscribe((events) => {
  // currentTarget is gone
  // I need to past the events some third party function
  // which depends on the currenTarget 
   console.log(events);
});




我的第一个问题是否能以某种方式避免丢弃?

我的解决方法是简单地用原始的覆盖currentTarget:

event.currentTarget = somethingEl

但我使用的是typescript,事件是一个const: "无法分配到' currentTarget'因为它是一个常数或只读属性"

我可以以某种方式覆盖或克隆事件以包含新的currentTarget吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

创建事件对象的副本,以在事件发生后绕过浏览器设置currentTarget null。

const canvasMousedown$ = Rx.Observable.fromEvent(chart, 'mousedown')
    .map(e => ({ currentTarget: e.currentTarget }))
const canvasMousemove$ = Rx.Observable.fromEvent(chart, 'mousemove')
    .map(e => ({ currentTarget: e.currentTarget }))
const canvasMouseup$ = Rx.Observable.fromEvent(chart, 'mouseup')
    .map(e => ({ currentTarget: e.currentTarget }))

const canvasSelection$ = canvasMousedown$.concatMap((startEvent) => {
  return canvasMousemove$.takeUntil(canvasMouseup$).map((endEvent) => [startEvent, endEvent])
});

canvasSelection$.subscribe(events => {
    console.log(events)
})