如何跳过流的第一个值,除非它是使用 RxJS 的复制粘贴?

问题描述 投票:0回答:1

我目前使用skip(1)来避免键入第一个值时不必要的计算。然而,我发现我的用户经常使用复制粘贴来过滤数据。因此,skip(1) 会跳过整个粘贴的值。

仅当第一个输入的字符不是来自粘贴的值时,我才想跳过它。我对复制粘贴并不真正感兴趣,但更感兴趣的是第一个值可能是具有多个字符的完整过滤器。

这是我当前的代码:

    public ngAfterViewInit(): void {
    this.quickFilterValueChange
        .pipe(
            skip(1),
            debounceTime(150),
            distinctUntilChanged(),
            tap(() => {
                this.store.dispatch(someAction);
            }),
            takeUntilDestroyed(this.destroyRef),
        )
        .subscribe();
}
javascript typescript rxjs rxjs-pipeable-operators
1个回答
0
投票

您需要使用

debounceTime
并增加阈值来跳过不必要的计算,而不是省略初始值的
skip

这样做的好处是,如果字符在阈值内,则仅采用最后一个值,其余的将被丢弃。

public ngAfterViewInit(): void {
    this.quickFilterValueChange
        .pipe(
            // filter(() => this.searchStr.length > 3), // you can also use this to limit the initial typing of the search field.
            debounceTime(500), // <- threshold is 500ms
            distinctUntilChanged(),
            tap(() => {
                this.store.dispatch(someAction);
            }),
            takeUntilDestroyed(this.destroyRef),
        )
        .subscribe();
}
© www.soinside.com 2019 - 2024. All rights reserved.