in Education by
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)

1 Answer

0 votes
by
We could create another notifier to limit the number of item (eg. with elementAt), use the notifier that emits first (with race) and apply this recursively (with expand): const notifierDebouncing$ = subject$.pipe( debounceTime(PERIOD), take(1) ); const notifierLimiting$ = subject$.pipe( elementAt(AMOUNT - 1) ); const notifier$ = interval(0).pipe( take(1), expand(_ => race(notifierLimiting$, notifierDebouncing$)) ); subject$ .pipe(buffer(notifier$)) .subscribe(value => console.log(value)); What do you think? Here is an example, based on your demo app: https://stackblitz.com/edit/rxjs-buffer-debounce-cf4qjy (open the console, then move the cursor for 2000ms and stop for 500ms)

Related questions

0 votes
    I would like to achieve the following using RxJs: Group message that are within ~200ms of the previous ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    I create an angular library "mylib" I create a service which uses BehaviorSubject Observable from rxjs. For ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I have a page with PrimeNG tree and an autocomplete field. My requirement is Tree should be expanded to ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 26, 2022 in Education by JackTerrance
0 votes
    I would like to dynamically assign a class to a specific cell in a table. An object in "cols" ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 21, 2022 in Education by JackTerrance
0 votes
    I would like to dynamically assign a class to a specific cell in a table. An object in "cols" ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 15, 2022 in Education by JackTerrance
0 votes
    I would like to dynamically assign a class to a specific cell in a table. An object in "cols" ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 11, 2022 in Education by JackTerrance
0 votes
    I'm using Angular Material in my application. I have input fields like this: E-mail Which produces a ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I'm using Angular Material in my application. I have input fields like this: E-mail Which produces a ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I have a json file contains a icons object like this: "icons" : { "logo" : "fa fa-caret-down ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    so with ng2 rc.6 I am getting an error of: Uncaught (in promise) Error: (SystemJS) SyntaxError: ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 8, 2022 in Education by JackTerrance
0 votes
    I have these routes /items/:id /items /items/:id/edit ... etc I want to put the component, to ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 3, 2022 in Education by JackTerrance
0 votes
    I have a mat-table with a component app-order-statuses on every row. the component calls the statuses ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 3, 2022 in Education by JackTerrance
0 votes
    I get data from socket io this JSON every 10 sec.. data { "nr": "1" } data { "nr": " ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 3, 2022 in Education by JackTerrance
0 votes
    I have an angular 7 project and I'm using Angular universal. Everything is working fine except when I ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 30, 2022 in Education by JackTerrance
0 votes
    I have these routes /items/:id /items /items/:id/edit ... etc I want to put the component, to ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 26, 2022 in Education by JackTerrance
...