1

I am learning CycleJS and I see that while using Cycle's HTTP Driver, I have to merge the response stream stream using RxJS switch/mergeAll to get to the stream level. But when I try to apply those functions, I'm getting a type error: switch is not a function (on response stream stream).

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

Could you please let me know if I am missing something?

Vikram
  • 73
  • 8

2 Answers2

2

@cycle/http filter returns a metastream so it's doesn't have the functionality of a stream.

To get a stream, after you filter, pull the response$$ stream from the resulting metastream using response$$, and then flatten it:

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

Now you can continue on with map, etc. (Available operators depend on the version of Cycle.js you're using. The latest uses xstream for it's stream engine.)

@cycle/http response$$

With an HTTP Source, you can also use httpSource.response$$ to get the metastream. You should flatten the metastream before consuming it, then the resulting response stream will emit the response object received through superagent.


Alternatively you can simply:

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

Now you have the response$ to work with as you expected.

bloodyKnuckles
  • 11,551
  • 3
  • 29
  • 37
0

switch is a RxJS operator, its analogue in xstream is flatten. The stream engine depends on which *-run package do you import your run function from. For example, if you do import {run} from 'rx-run', all the source streams from drivers will come with the stable RxJS 4 interface.

All this multiple stream engine stuff is quite new, introduced with latest release. You may consider reading the migration guide

Philipp Ryabchun
  • 736
  • 1
  • 5
  • 7