0

我们希望缓冲数据,直到达到以下两个事件,例如去抖时间达到 5 或值更改。但是根据下面的测试,我们发现它不能正常工作。

我想要实现的行为是。

a)当值发生变化时,我想发出这些缓冲值。

b)当达到去抖时间时,我想发出缓冲值。

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');
        });
    });
});
4

0 回答 0