Use cached network data with RxJS

InstructorAndré Staltz

Share this video with your friends

Send Tweet

In this lesson, we learn how to build the final feature of our user interface: how to use the cached network response to replace a suggested user when the user clicks on the 'x' close button for that user.

Przemysław
~ 9 years ago

Is it possible to siplyfy the example by

var requestOnRefreshStream = refreshClickStream .map(ev => { var randomOffset = Math.floor(Math.random()*500); return 'https://api.github.com/users?since=' + randomOffset; });

var requestStream = requestOnRefreshStream.startWith('https://api.github.com/users');

Or var requestStream = refreshClickStream.startWith('click') .map(ev => { var randomOffset = Math.floor(Math.random()*500); return 'https://api.github.com/users?since=' + randomOffset; });

Is this correct ?

André Staltzinstructor
~ 9 years ago

Correct, there are multiple ways of achieving the same behavior. The code presented in the video is the easiest for beginners to follow what is happening.

Przemysław
~ 9 years ago

Why you didn't model stream to : responseStream.flatMap(function(listUsers) { return Rx.Observable.from(listUsers); }).subscribe(function(user) { console.log("user ->"+user.login);});

  • We get a stream of users entities which can be consumed by subscribers.
Oren
~ 8 years ago

Hi Andre, Can I merge more than 2 Observables? Thanks

André Staltzinstructor
~ 8 years ago

Hi Oren, yes you can: Rx.Observable.merge(a, b, c)

Sequoia McDowell
~ 8 years ago

I like the series but it raises a ton of unanswered questions. For me, primarily,

  1. What's up with all the repetition/copypasta & is there a RX idiomatic way to remove that repetition?
  2. Each stream doesn't know about the others' state (yay!) so they can each load the same user (boo!). Glossing over this (how do I handle streams that need to be aware of the state of other streams or what they did?) seems like skipping a crucial question-- personally I need to understand this before I could consider using RX.
  3. Is it considered no big deal that some streams reach way back & access their upstream-streams in different ways? I.e. the "update suggested user" stream that is a grand-child (if you'll allow the expression) of the refresh click stream, but also directly consumes that stream to emit nulls. Marble diagram now needs to be 3D: needs arrows that go OVER other streams & skip 2 or 3 streams down. Is this kosher in the RX world?

Thanks!

Rafael Bitencourt
~ 8 years ago

Got this working on v5 using switchMapTo operator instead of the switchLatestFrom.

Shailesh
~ 8 years ago

@andrestaltz refreshclick.map(ev => null) executed in createSuggestionsStream and refreshClickStream.map(...//which returns URL to get the user) are passed the same event every time refresh is clicked correct? So they are two different streams consuming same "click"? what guarantees that null is always returned first before the actual result? the async nature of the fetching user? what if we are returning the user from cached array? Will still null be returned first?

André Staltzinstructor
~ 8 years ago

There are different ways of accomplishing this, and better ways where we wouldn't have race conditions. In this case, because it is an introduction only, I went for the simplest option where we rely on the fact that user fetching will be asynchronous and click reactions are synchronous.

Shailesh
~ 8 years ago

ok thanks Andre. can you please give me pointers for further reading regarding the race condition avoiding operators and ways. may be resources which explain such gotchas and possible ways. Ofcourse I will be continuously following all the courses here and elsewhere but getting this from "horse's mouth" will be best way to go about this.

André Staltzinstructor
~ 8 years ago

If you haven't yet, Watch the other courses on RxJS here in Egghead. I also plan to release the course about flatMap and similar operators, which are useful for many kinds of chaining and dependencies among observables. These are ways of handling race conditions as well.

preko96
~ 5 years ago

Wow.... this was the point I started to think in streams. Making those marble diagrams was extremely useful. Especially when you created the result stream first, and tried to figure out how could we achieve that.