You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Dec 4, 2017. It is now read-only.
The http call in `HeroSearchService` is not that different from our previous http calls, but we no longer call `toPromise`. This means we will return an observable instead of a promise.
`ValueChanges` is an observable that represents text changes triggered by typing into the search textbox.
356
+
357
+
### debounceTime(300)
358
+
By specifying a debounce time of 300 ms we are telling `RxJs` that we only want to be notified of key events after intervals of 300 ms. This is a performance measure meant to prevent us from hitting the api too often with partial search queries.
359
+
360
+
### distinctUntilChanged
361
+
`distinctUntilChanged` tells `RxJs` to only react if the value of the textbox actually changed.
362
+
363
+
### do
364
+
`do` is an operator for triggering side effects outside the observable chain. In our case we are using it to set a flag to hide the search results if the search textbox is empty.
365
+
366
+
### filter
367
+
By specifying a `filter` we are telling `RxJs` to only process search values that meet our filter condition.
368
+
369
+
### switchMap
370
+
`switchMap` is where observables from key events and observables from http calls converge on a single observable.
371
+
372
+
Every qualifying key event will trigger an http call, so we may get into a situation where we have multiple http calls in flight.
373
+
374
+
Normally this could lead to results being returned out of order, but `switchMap` has built in support for ensuring that only the most recent http call will be processed. Results from prior calls will be discarded.
375
+
376
+
### subscribe
377
+
`subscribe` is similar to `then` in the promise world.
378
+
379
+
The first callback is the success callback where we grab the result of the search and assign it to our `heroes` array.
380
+
381
+
The second callback is the error callback. This callback will execute if there is an error - either from the http call or in our processing of key events.
382
+
383
+
In our current implementation we are just logging the error to the console, but a real life application should do better.
384
+
385
+
None of these operators are not included by default, so we have to load each one using `import` statements before using them.
386
+
387
+
We have combined all the operator imports in a single file.
0 commit comments