javascript - Rxjs: How to subscribe to an event only if another event happened before? -


i have 2 event streams:

let mousedowns = rx.observable.fromevent(document.body, "mousedown"); let mouseups = rx.observable.fromevent(document.body, "mouseup"); 

i subscribe mouseups preceeded mousedown.

normally remember state so:

let isdown = false; mousedowns.subscribe(() => down = true); mouseups.filter(() => isdown).subscribe(() => console.log("captured"); 

this feels rather un-"reactive"... elegant way same thing?

it depends on if want stop listening mouse ups until next mouse down. mean, if goal have observable tells if mouse down, you'd like:

const ismousedown = rx.observable     .merge(         mousedowns.map(() => true),         mouseups.map(() => false))     .startwith(false);  ismousedown.subscribe(state => console.log("is mouse down? " + state)); 

if want trigger after down/up sequence you'd do:

const myclicks = mousedowns.switchlatest(() => mouseups); myclicks.subscribe(() => console.log("down/up triggered")); 

Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -