【问题标题】:switch/mergeAll/flatten is not a function - while using http driver in cyclejsswitch/mergeAll/flatten 不是函数-在cyclejs中使用http驱动程序时
【发布时间】:2016-07-03 03:11:07
【问题描述】:

我正在学习 CycleJS,我发现在使用 Cycle 的 HTTP 驱动程序时,我必须使用 RxJS switch/mergeAll 合并 response stream stream 以达到流级别。但是当我尝试应用这些函数时,我收到了一个类型错误:switch is not a function(在响应流上)。

  const response$$ = sources.HTTP
            .filter(response$ => response$.request.url === 'http://jsonplaceholder.typicode.com/users/1')
  const response$ = response$$.switch()

如果我遗漏了什么,请告诉我吗?

【问题讨论】:

    标签: javascript rxjs reactivex cyclejs


    【解决方案1】:

    @cycle/http filter 返回一个 metastream,因此它没有 stream 的功能。

    要获得,在filter 之后,使用response$$ 从生成的元流 中提取response$$ 流,然后使用flatten 它:

    const response$$ = sources.HTTP
      .filter(response$ => response$.request.url === 'http://jsonplaceholder.typicode.com/users/1')
      .response$$
    const response$ = response$$.flatten()
    

    现在您可以继续使用map 等(可用的运算符取决于您正在使用的Cycle.js 的版本。最新使用xstream 作为它的 引擎。)

    @cycle/http response$$

    有了HTTP Source,你也可以使用httpSource.response$$来获取 元流。您应该在使用元流之前将其展平, 然后生成的响应流将发出响应对象 通过超级代理接收。


    或者你可以简单地:

    const response$ = sources.HTTP
      .filter(response$ => response$.request.url === 'http://jsonplaceholder.typicode.com/users/1')
      .response$$.flatten()
    

    现在您可以按预期使用response$

    【讨论】:

      【解决方案2】:

      switch 是一个 RxJS 操作符,它在 xstream 中的类似物是flatten。流引擎取决于您从哪个*-run 包导入run 函数。例如,如果您使用import {run} from 'rx-run',所有来自驱动程序的源流都将带有稳定的 RxJS 4 接口。

      所有这些多流引擎的东西都是相当新的,在最新版本中引入。你可以考虑阅读migration guide

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-30
        • 1970-01-01
        • 2013-03-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多