【问题标题】:How to use RxJS 5 buffer function?如何使用 RxJS 5 缓冲功能?
【发布时间】:2016-07-22 19:51:10
【问题描述】:

我用谷歌搜索了很多,试图查看 d.ts 文件,但仍然无法理解问题所在。找不到 RxJs5 如何使用这个功能的例子。

var source = Observable.fromEvent(document.body, 'keypress');

    var delayedSource = source.delay(1000);

    var obs = source



      .buffer(() => {
        return   delayedSource;
      })

      .map((clickBuffer) => {
        return clickBuffer.length;
      });

我收到错误:

错误:(166, 15) TS2345: '() => Observable' 类型的参数不可分配给'Observable' 类型的参数。 类型“() => Observable”中缺少属性“_isScalar”。

buffer.d.ts 看起来是这样,我想我应该从中理解,但我不能。

import { Observable } from '../Observable';
/**
 * Buffers the incoming observable values until the passed `closingNotifier`
 * emits a value, at which point it emits the buffer on the returned observable
 * and starts a new buffer internally, awaiting the next time `closingNotifier`
 * emits.
 *
 * <img src="./img/buffer.png" width="100%">
 *
 * @param {Observable<any>} closingNotifier an Observable that signals the
 * buffer to be emitted} from the returned observable.
 * @returns {Observable<T[]>} an Observable of buffers, which are arrays of
 * values.
 */
export declare function buffer<T>(closingNotifier: Observable<any>): Observable<T[]>;
export interface BufferSignature<T> {
    (closingNotifier: Observable<any>): Observable<T[]>;
}

这个问题来自: Counting keypresses per second with angular 2 Rxjs

【问题讨论】:

  • 你传入了一个函数,但你应该传入 observable。

标签: rxjs5


【解决方案1】:

根据 Benjamin Gruenbaum 的评论,这解决了问题:

var source = Observable.fromEvent(document.body, 'keypress');

var delayedSource = source.delay(1000);

var obs = source

      .buffer(delayedSource)

      .map((clickBuffer) => {
        return clickBuffer.length;
      })

【讨论】:

    猜你喜欢
    • 2017-05-20
    • 2017-09-09
    • 2023-02-08
    • 1970-01-01
    • 2017-02-10
    • 2019-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多