Skip to content

Instantly share code, notes, and snippets.

@kosich
Last active April 26, 2019 10:27
Show Gist options
  • Save kosich/304b955e27ea88f622094b671cdfbb7b to your computer and use it in GitHub Desktop.
Save kosich/304b955e27ea88f622094b671cdfbb7b to your computer and use it in GitHub Desktop.
debounceTime vs throttleTime vs auditTime vs sampleTime
const { rxObserver, palette } = require('api/v0.3');
const { merge, timer, from, NEVER } = require('rxjs');
const { map, take, zip, auditTime, throttleTime, debounceTime, sampleTime, share, concat } = require('rxjs/operators');
// stream for coloring
const palette$ = from(palette);
const source$ = merge(
timer(0, 330),
timer(50, 180)
).pipe(
take(10),
// get color for each item
zip(palette$, Marble),
map(setCurrentTime),
share(),
concat(NEVER)
);
source$
.subscribe(rxObserver());
source$.pipe(
debounceTime(100),
map(setCurrentTime)
)
.subscribe(rxObserver('debounceTime(100)'));
source$.pipe(
throttleTime(100),
map(setCurrentTime)
)
.subscribe(rxObserver('throttleTime(100)'));
source$.pipe(
auditTime(100),
map(setCurrentTime)
)
.subscribe(rxObserver('auditTime(100)'));
source$.pipe(
sampleTime(100),
map(setCurrentTime)
)
.subscribe(rxObserver('sampleTime(100)'));
// helpers
// keeps colors, updated value to Date.now
function setCurrentTime({ color }){
return Marble(Date.now(), color);
}
// creates a colored Marble
function Marble(value, color) {
return {
valueOf: ()=>value
, color
};
}
@kosich
Copy link
Author

kosich commented Mar 5, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment