Giter Site home page Giter Site logo

piyalidas10 / github-users-search-using-angular-debounce- Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 883 KB

Github Users Search using Angular + Rxjs Debounce

JavaScript 12.32% TypeScript 80.44% HTML 6.14% CSS 1.10%
angular search rxjs debounce switchmap distinctuntilchanged github github-user-search user-search

github-users-search-using-angular-debounce-'s Introduction

AngularSearchBar

This project was generated with Angular CLI version 10.2.3.

Live URL

https://piyalidas10.github.io/angular-search-bar/

github_users

Github API URL

https://api.github.com/

this.searchSubcription = fromEvent(this.txtField.nativeElement, 'keyup').subscribe(data => console.log(data))

FromEvent method to create an observable from DOM events directly, so you have to subscribe it to get data. If you directly subscribe fromEvent data, it will return keyboardEvent value.

But We want the value which we are typing in the search box, so we have to transform it into the value using pipe.

Here is the code to get value from seach box

this.searchSubcription = fromEvent<any>(this.txtField.nativeElement, 'keyup')
    .pipe(
      map(event => event.target.value),
    )
    .subscribe(data => console.log(data));

keyboardEvent_value

Now i have to call API to get data from backend. On each keystokes i cann't hit API, so i have to use debounceTime to control API calls. The first very useful operator is debounceTime. In debounceTime, we have to pass time in milliseconds as a parameter. So, you will not get value emitted if it is within the specified time frame.

Here new value emits only if nothing happens for specified time frame. In our example, once the user stops writing then after 1000 milliseconds new updated value will emit.

this.searchSubcription = fromEvent<any>(this.txtField.nativeElement, 'keyup')
    .pipe(
      // Time in milliseconds between key events
      debounceTime(1000),
      map(event => event.target.value),      
    )
    .subscribe(data => console.log(data));

But here also one issue is there. If user writes “india” then pause so “india” value emits, then again start writing “indiapy” (adding “py” characters), and then immediately removes newly added characters (in our case “py”) and then pause, so again same value “india” emits. We can also prevent same value emitting again with RxJS distinctUntilChanged operator.

distinctUntilChanged() only emit when the current value is different than the last.

this.searchSubcription = fromEvent<any>(this.txtField.nativeElement, 'keyup')
    .pipe(
      // Time in milliseconds between key events
      debounceTime(1000),
      map(event => event.target.value),
      distinctUntilChanged()
    )
    .subscribe(data => console.log(data));

Final code using switchMap

this.searchSubcription = fromEvent<any>(this.txtField.nativeElement, 'keyup')
    .pipe(
      // Time in milliseconds between key events
      debounceTime(1000),
      map(event => event.target.value),
      distinctUntilChanged(),
      switchMap(val => this.apiService.getUsersByLocation(val))
    )
    .subscribe(data => console.log(data));

Let’s examine a simple scenario:

  1. a user types ‘india’ into the input,
  2. switchMap operator is reached and a call to getUsersByLocation function is made with a given query string,
  3. a mocked request is pending,
  4. if a user change the input’s value to 'america' and the switchMap operator is reached while the previous request is still pending, the former request will be aborted and a new one will be processed. The previous subscription will be unsubscribed and the new one will be made,
  5. the value emitted by the resulting observable will be in sync with the current input’s value 'america'.

switchMap

The switchMap() operator has three important characteristics.

  1. It takes a function argument that returns an observable. EmployeeSearchService.search returns an observable, as other data service methods do.
  2. If a previous search request is still in-flight (as when the connection is poor), it cancels that request and sends a new one.
  3. It returns service responses in their original request order, even if the server returns them out of order.

Reference URLS

https://www.youtube.com/watch?v=HfVTp4yo12A
https://www.youtube.com/watch?v=ET2UPbsgPL8
https://www.freakyjolly.com/angular-rxjs-debounce-time-optimize-search-for-server-response/
https://stackoverflow.com/questions/42454740/angular-2-subscribing-to-observable-fromevent-error-invalid-event-target
https://github.com/Rinlama/AngularTools/tree/rxjsDebounceDistinctUntilChanged
https://www.freakyjolly.com/angular-rxjs-debounce-time-optimize-search-for-server-response/
https://stackblitz.com/edit/angular-debounce-search?file=src%2Fapp%2Fapp.component.ts
https://stackblitz.com/edit/angular-debounce-search-field?file=src%2Fapp%2Fapp.component.ts

github-users-search-using-angular-debounce-'s People

Contributors

piyalidas10 avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.