0

We want to buffer the data until the below two events are reached such as the debounce time reached to 5 or value is changed. But based on the below test we seeing it is not working properly.

The behavior I want to achieve is.

a) When there is a change in the value, I want to emit those buffered values.

b) When debounce time reached, I want to emit the buffered value.

import {
    mergeMap,
    tap,
    map,
    takeUntil,
    concatMap,
    throttle,
    throttleTime,
    debounceTime,
    distinctUntilChanged,
    bufferWhen,
} from 'rxjs/operators';
import { of, from, race } from 'rxjs';
import 'jest';




describe('test ', () => {
    const source$ = from([
        { name: 'Brian' },
        { name: 'Joe' },
        { name: 'Joe' },
        { name: 'Joe' },
        { name: 'Joe' },
        { name: 'Sue' },
        { name: 'Brian' },
        { name: 'Brian' },
        { name: 'Brian' },
        { name: 'Brian' },
        { name: 'Brian' },
        { name: 'Brian' },
        { name: 'Brian' },
        { name: 'Brian' },
        { name: 'Brian' },
        { name: 'Brian' },
        { name: 'Joe' },
        { name: 'Joe' },
        { name: 'Sue' }

    ]);


    const distinctEvent$ = source$.pipe(
        distinctUntilChanged((prev, curr) => prev.name === curr.name),
        tap(() => console.log('-1-- AFTER DISTINCT'))
    )


    const batch$ = source$.pipe(
        bufferWhen(() =>
            race(
                distinctEvent$,
                source$.pipe(debounceTime(5)),
            ),
        ),
    );
    it('test', () => {

         batch$.subscribe(() => {
            console.log('--only');
        });
    });
});
user1595858
  • 3,700
  • 15
  • 66
  • 109

0 Answers0