Published
Edited
May 8, 2020
1 star
Insert cell
md`# batching | collecting | recursing | grouping`
Insert cell
import { scan, expand, EMPTY, bufferTime, windowTime, groupBy, tap, interval, of, from, take } from '@egman24/rxjs-6-5-5'
Insert cell
md`
#### scan

- https://www.learnrxjs.io/learn-rxjs/operators/transformation/scan
`
Insert cell
interval(1000).pipe(
// NOTE: acts like manual/firstclass buffer... can select what to keep and what to drop, can move from in memory to persistent storage, etc...
// NOTE: can have predicates in acc object, that allow action downstream ONLY if certain needs are met... values can be emitted, but not acted upon or filtered, etc...
scan((acc, val) => {
return {
...acc,
current: val,
last: acc.current,
all: [...(acc.all || []), val]
}
}, {}),
tap(console.log)
)//.subscribe()
Insert cell
md`
#### expand

> recursion

- https://www.learnrxjs.io/learn-rxjs/operators/transformation/expand
- https://rxjs.dev/api/operators/expand
- https://rxjs-dev.firebaseapp.com/api/index/const/EMPTY
`
Insert cell
of(2).pipe(
//recursively call supplied function
expand(val => {
//2,3,4,5,6
console.log(`Passed value: ${val}`);
//3,4,5,6
return of(1 + val);
}),
//call 5 times
take(5)
)//.subscribe()
Insert cell
// NOTE: without take, *need* base case to break out (EMPTY)
// https://rxjs-dev.firebaseapp.com/api/index/const/EMPTY

of(2).pipe(
//recursively call supplied function
expand(val => {
//2,3,4,5,6
console.log(`Passed value: ${val}`);
//3,4,5,6
return val > 10
? EMPTY
: of(1 + val);
})
).subscribe()
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more