Race Observable下的distantUntilChanged未能按预期工作

时间:2019-11-11 17:46:50

标签: javascript rxjs rxjs-observables

我们要缓冲数据,直到达到以下两个事件,例如去抖动时间达到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');
        });
    });
});

0 个答案:

没有答案