Instantly share code, notes, and snippets.

Embed
What would you like to do?
Array vs Stream
// simple array
const arr = [{ x: 1, y: 4 }, { x: 20.2, y: 40.4 }, { x: 130.2, y: 240.1 }]
// stream of mouse position looks similar
const stream = [{ x: 1, y: 4 }, { x: 20.2, y: 40.4 }, { x: 130.2, y: 240.1 }]
// simply priting all array items
Rx.Observable.fromArray(arr)
.subscribe(x => console.log(`Array item: ${x.x}, ${x.y}`))
// printing each item in the array after introducing a delay of 200 ms
Rx.Observable.fromArray(arr).zip(
Rx.Observable.interval(200), x => x
).subscribe(x => console.log(`Delayed array item: ${x.x}, ${x.y}`))
// priting all mouse move positions
Rx.Observable.fromEvent(document, 'mousemove')
.subscribe(x => console.log(`Mouse move: ${x.x}, ${x.y}`))
/**
* priting all mouse move positions debounced over 0.5 sec
* stop mouse movement for this to take effect
*/
Rx.Observable.fromEvent(document, 'mousemove')
.debounce(val => Rx.Observable.interval(500))
.subscribe(x => console.log(`Debounced mouse move: ${x.x}, ${x.y}`))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment