Skip to content

Instantly share code, notes, and snippets.

@m1m1s1ku
Created April 26, 2019 17:40
Show Gist options
  • Save m1m1s1ku/a6ee551f13315fbb9c3d5b565abe5709 to your computer and use it in GitHub Desktop.
Save m1m1s1ku/a6ee551f13315fbb9c3d5b565abe5709 to your computer and use it in GitHub Desktop.
DebounceTime(0) is BAD
// Write any JavaScript you want, just make sure that
// the last expression is an Rx.Observable
const { asapScheduler, animationFrameScheduler, asyncScheduler, queueScheduler, merge, EMPTY } = Rx;
const { map, debounceTime, startWith, filter } = RxOperators;
// New macro-task (long-running task)
const asyncScheduler$ = EMPTY.pipe(
startWith('async', asyncScheduler)
)
// (same micro-task) (short task, [in the current "tick"])
const asapScheduler$ = EMPTY.pipe(
startWith('asap', asapScheduler)
);
// Used for (same macrotask [just after every other current work])
const queueScheduler$ = EMPTY.pipe(
startWith('queue', queueScheduler)
);
// Used for (same macrotask that is Before repaint)
const animationFrameScheduler$ = EMPTY.pipe(
startWith('animationFrame', animationFrameScheduler)
);
// Used when value is iterable and not promiseLike, can produce many subtle 🐛
const nullScheduler$ = EMPTY.pipe(
startWith('noScheduler', null)
);
merge(asyncScheduler$, asapScheduler$, queueScheduler$, animationFrameScheduler$, nullScheduler$).pipe(
debounceTime(0),
filter(x => !!x),
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment