Skip to content

Instantly share code, notes, and snippets.

@fauxparse
Last active January 17, 2020 03:05
Show Gist options
  • Save fauxparse/4d891327853b8a0053d1075e6c373b6b to your computer and use it in GitHub Desktop.
Save fauxparse/4d891327853b8a0053d1075e6c373b6b to your computer and use it in GitHub Desktop.
RxJS fiddles
const { of, fromEvent } = Rx;
const { expand, map, zip } = RxOperators;
const click$ = fromEvent(document, 'click')
of(1).pipe(
expand(x => (
of(x + 1)
.pipe(
zip(click$),
map(([x]) => x)
)
))
)
const { fromEvent, of, from } = Rx;
const { merge, mapTo, scan, mergeScan, mergeMap, delay, first } = RxOperators;
const button = document.createElement('button')
button.innerText = 'Step'
output.prepend(button)
fromEvent(button, 'click').pipe(
mapTo(1),
mergeScan((x, y) =>
from([-1, 1]).pipe(
mergeMap(d => of(x + d).pipe(delay(Math.random() * 50))), first()
), 0)
)
const { of, from, fromEvent, Observable } = Rx;
const { expand, delay, map, zip, mergeMap, first } = RxOperators;
const click$ = fromEvent(document, 'click')
const inc = source$ => Observable.create(observer =>
source$.subscribe(
value => observer.next(value + 1),
err => observer.error(err),
() => observer.complete()
)
);
const dec = source$ => Observable.create(observer =>
source$.subscribe(
value => observer.next(value - 1),
err => observer.error(err),
() => observer.complete()
)
);
const noop = source$ => Observable.create(observer =>
source$.subscribe(
value => observer.next(value),
err => observer.error(err),
() => observer.complete()
)
);
of(1).pipe(
expand(x => (
from([inc, dec, noop])
.pipe(
mergeMap(operation => of(x).pipe(operation, delay(Math.floor(Math.random() * 50)))),
first(),
zip(click$),
map(([x]) => x)
)
))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment