I would like to achieve the following using RxJs:
Group message that are within ~200ms of the previous message
Emit group of messages when no new messages have been received within 250ms
Emit group of messages when group reaches 10 items.
Thanks to several other questions on SO, such as this one, it's quite easy to implement 1 and 2 using a combination of buffer and debounceTime, like so:
const subject$ = new Subject();
// Create the debounce
const notifier$ = subject$.pipe(
debounceTime(250)
);
// Subscribe to the subject using buffer and debounce
subject$
.pipe(
buffer(notifier$)
)
.subscribe(value => console.log(value));
// Add a number to the subject every 200ms untill it reaches 10
interval(200)
.pipe(
takeWhile(value => value <= 10),
)
.subscribe(value => subject$.next(value));
Here messages are buffered as long as they're emitted within 200ms of the last one. If it takes more than 200ms a new buffer is started. However, if messages keep coming in under 200ms the messages could be buffered forever. That's why I want to add a hard limit on the buffer size.
I created an example at StackBlitz to demonstrate the buffer debounce. But I can't figure out how to limit the buffer so that it emits when it reaches 10 items as well.
JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)