Skip to content

Instantly share code, notes, and snippets.

@mattpodwysocki
Created July 14, 2015 16:22
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mattpodwysocki/9aeb5ce339cdb38a5dc2 to your computer and use it in GitHub Desktop.
Save mattpodwysocki/9aeb5ce339cdb38a5dc2 to your computer and use it in GitHub Desktop.
const touchStart = Rx.Observable.fromEvent(window, 'touchstart').timestamp();
const touchMove = Rx.Observable.fromEvent(window, 'touchmmove');
const touchEnd = Rx.Observable.fromEvent(window, 'touchend').timestamp();
const touchAndHold = touchStart
.flatMap(() => touchEnd.takeUntil(touchMove), (x, y) => { start: x.timestamp, end: y.timestamp })
.filter(ts => (ts.end - ts.start) / 1000 > 2);
const subscription = touchAndHold.subscribe(
() => console.log('touch and hold!')
);
@JudahGabriel
Copy link

After a bit of experimentation, this isn't quite what I was looking for.

I want an observable that fires when the user is touching the screen and holds it there for a period of time. If he releases his finger, the observable shouldn't fire.

I ended up going with something like this:

var touchStart = Rx.Observable.fromEvent(this.canvasElement, "touchstart");
var touchEnd = Rx.Observable.fromEvent(this.canvasElement, "touchend");
var timer = Rx.Observable.timer(750);
var touchHold = touchStart
    .selectMany(() => timer.takeUntil(touchEnd), (e, t) => e)
    .subscribe(e => console.log("touch hold!"));

This seems to work for my needs. Thanks again for your help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment