Skip to content

Instantly share code, notes, and snippets.

@griffinmichl
Created May 14, 2016 23:41
Show Gist options
  • Save griffinmichl/d87430e5451fc2d778d293d915de7b39 to your computer and use it in GitHub Desktop.
Save griffinmichl/d87430e5451fc2d778d293d915de7b39 to your computer and use it in GitHub Desktop.
const fetchContent = (url) =>
rx.Observable.create(observer =>
fetch(url)
.then(res => res.json())
.then(json => {
observer.onNext(json)
observer.onCompleted()
)
.catch(e => observer.onError())
)
const hover$ = rx.Observable.fromEvent(myButton, 'mouseover');
const leave$ = rx.Observable.fromEvent(myButton, 'mouseout');
const click$ = rx.Observable.fromEvent(myButton, 'click');
const hoverRequest$ = hover$
.flatMap(e =>
fetchContent(e.target.href).takeUntil(leave$)
)
const displayData$ = click$
.withLatestFrom(hoverRequest$)
.filter(([click, json]) => !!json)
.map(([click, json]) => json)
displayData$.subscribe(json => console.log(json))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment