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.
We start the observable chain by calling `asObservable` on our `Subject` to return an observable that represents text changes as we are typing into the search box.
411
+
412
+
### debounceTime(300)
413
+
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.
414
+
415
+
### distinctUntilChanged
416
+
`distinctUntilChanged` tells `RxJs` to only react if the value of the textbox actually changed.
417
+
418
+
### switchMap
419
+
`switchMap` is where observables from key events and observables from http calls converge on a single observable.
420
+
421
+
Every qualifying key event will trigger an http call, so we may get into a situation where we have multiple http calls in flight.
422
+
423
+
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.
424
+
425
+
We short circuit the http call and return an observable containing an empty array if the search box is empty.
426
+
427
+
### subscribe
428
+
`subscribe` is similar to `then` in the promise world.
429
+
430
+
The first callback is the success callback where we grab the result of the search and assign it to our `heroes` array.
431
+
432
+
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.
433
+
434
+
In our current implementation we are just logging the error to the console, but a real life application should do better.
435
+
436
+
### Import operators
437
+
The `RxJs` operators are not included by default, so we have to include them using `import` statements.
438
+
439
+
We have combined all operator imports in a single file.
0 commit comments