ReactiveX (Rx) - Long-Term Event Detection

I wonder what the canonical approach is for solving the following problem in Rx: Let's say I have two observables, mouse_downand mouse_upwhose elements are mouse clicks. In a very simplified scenario, if I wanted to detect a long press, I could do it as follows (in this case using RxPy, but conceptually the same in any Rx implementation):

mouse_long_press = mouse_down.delay(1000).take_until(mouse_up).repeat()

However, problems arise when we need to raise some information from the observed mouse_downobservable to mouse_up. For example, consider if there were elements of observable stored information about which mouse button was clicked. Obviously, we would like only a couple mouse_downwith the mouse_upcorresponding button. One solution I came up with is the following:

mouse_long_press = mouse_down.select_many(lambda x:
    rx.Observable.just(x).delay(1000)\
        .take_until(mouse_up.where(lambda y: x.button == y.button))
)

If there is a more straightforward solution, I would love to hear it, but as far as I can tell, it works. However, things get complicated if we also want to determine how far the mouse moved between mouse_downand mouse_up. To do this, we need to introduce a new observable mouse_movethat carries information about the position of the mouse.

mouse_long_press = mouse_down.select_many(lambda x:
    mouse_move.select(lambda z: distance(x, z) > 100).delay(1000)\
        .take_until(mouse_up.where(lambda y: x.button == y.button))
)

, , . , 1 , . , , , . , , , . , . .

+4
1

, , . RxPy take_with_time, . , ( , take_with_time Rx).

mouse_long_press = mouse_down.select_many(lambda x:
    mouse_moves.take_with_time(1000).all(lambda z: distance(x, z) < 100)\
        .take_until(mouse_up.where(lambda y: x.button == y.button))
)

, - .

+1

Source: https://habr.com/ru/post/1583920/


All Articles