"Leading" debounceTime which emits immediately #6550
wh1t3cAt1k
started this conversation in
Ideas / Feature request
Replies: 2 comments
-
(I'd appreciate recipes on how to achieve this using the available operators today!) |
Beta Was this translation helpful? Give feedback.
0 replies
-
const debounceTime = (duration: number) => $ =>
$.pipe(
switchMap(x =>
of(x).pipe(delay(duration))
)
)
const debouceTimeWithImmediateOnStart = (duration: number) => $ =>
merge(
$.pipe(take(1)),
$.pipe(skip(1), debounceTime(duration))
)
const debounceTimeWithImmediateOnColdStart = (duration: number) => $
(a$ =>
a$.pipe(switchMap(x =>
concat(
of(x),
race(
a$,
concat(timer(duration), a$.pipe(debounceTimeWithImmediateOnColdStart(duration)))
)
)
))
)($.pipe(debouceTimeWithImmediateOnStart(duration))
// same thing imperative style
const debounceTimeWithImmediateOnColdStart = (duration: number) => $
new Observable(a$ => {
let isColdStart = true
let coldStartTimeout = undefined as number | undefined;
let debounceTimeout = undefined as number | undefined;
let subscription = $.subscribe({
next: x => {
if (isColdStart) {
a$.next(x);
clearTimeout(coldStartTimeout)
isColdStart = false;
} else {
clearTimeout(debounceTimeout);
coldStartTimeout = setTimeout(() => {
a$.next(x);
clearTimeout(coldStartTimeout)
}, duration)
}
coldStartTimer = setTimeout(() => {
isColdStart = true
}, duration);
},
error: e => ($.error(e), unsubscribe()),
complete: () => ($.complete(), unsubscribe())
})
const unsubscribe = () => subscription.unsubscribe()
return unsubscribe
}) Homework: write marble tests to check if any of these actually work or not |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I'd love to see
debounceTime
which emits immediately upon receiving and then:This makes sense in scenarios where a bunch of events requiring reaction might bulk-arrive in a burst - the reaction is required ASAP, but we want to prevent handling more than one event in a burst.
Beta Was this translation helpful? Give feedback.
All reactions