Hone logo
Hone
Problems

Implementing switchMap for Dynamic API Calls in Angular

This challenge focuses on a fundamental RxJS operator, switchMap, crucial for handling asynchronous operations in Angular, particularly when dealing with API calls. You will implement a scenario where user input triggers an API request, and switchMap ensures that only the latest request's response is processed, cancelling any previous pending requests. This is vital for preventing race conditions and ensuring UI consistency.

Problem Description

You are tasked with building a feature that allows a user to search for items. As the user types in a search input, an Angular component should make an API call to fetch search results. The core requirement is to use the switchMap operator to handle these asynchronous calls efficiently.

What needs to be achieved:

  • Create an Angular component that includes a search input field.
  • As the user types, debounce the input to avoid excessive API calls.
  • Trigger an asynchronous operation (simulated API call) based on the debounced input.
  • Utilize RxJS's switchMap to ensure that if new input arrives while a previous API call is still pending, the older call is automatically unsubscribed from, and only the result of the latest call is processed.
  • Display the search results in the component.

Key requirements:

  • Use Angular services to encapsulate the "API call" logic.
  • Employ RxJS operators like fromEvent, debounceTime, map, and switchMap.
  • The component should react to changes in the search input.
  • Handle scenarios where no search results are found.

Expected behavior:

  1. The user starts typing in the search input.
  2. After a short delay (e.g., 300ms) without further typing, an API call is simulated.
  3. If the user types again before the previous API call completes, the previous call is cancelled, and a new API call is initiated.
  4. Only the results from the most recent API call are displayed.
  5. If an API call returns no results, a "No results found" message should be displayed.

Important edge cases to consider:

  • Empty input: What happens when the user clears the search input?
  • Rapid typing: Ensure debounceTime effectively handles bursts of input.
  • API errors: While not the primary focus, consider how a real-world application might handle API call failures (though for this challenge, simulating success is sufficient).

Examples

Example 1:

  • Input: User types "app".
  • Process:
    • debounceTime (e.g., 300ms) waits for no more input.
    • switchMap is triggered with the value "app".
    • An API call to GET /api/items?search=app is initiated.
  • Output: A list of items matching "app" (e.g., "Apple", "Application", "Appliance") is displayed.

Example 2:

  • Input: User types "a", then quickly types "ap".
  • Process:
    • "a" is debounced. API call for "a" is initiated.
    • Before the "a" call completes, "p" is typed, making it "ap".
    • The previous API call for "a" is cancelled by switchMap.
    • A new API call for "ap" is initiated.
  • Output: A list of items matching "ap" (e.g., "Apple", "Apply") is displayed. The results from the "a" search are ignored.

Example 3:

  • Input: User types "xyz" which yields no search results.
  • Process:
    • debounceTime waits for no more input.
    • switchMap is triggered with "xyz".
    • An API call to GET /api/items?search=xyz is initiated.
    • The API returns an empty array.
  • Output: A "No results found" message is displayed.

Constraints

  • Angular version: 14 or later.
  • RxJS version: 6 or later.
  • The simulated API call should return an array of strings or an empty array.
  • The debounceTime should be set to a value between 200ms and 500ms.
  • The component should be implemented using TypeScript.

Notes

  • You can simulate an API call using setTimeout within an RxJS Observable.
  • Think about how to represent the "cancellation" of previous requests. switchMap handles this automatically by unsubscribing from the inner observable.
  • Consider using Angular's HttpClient in a real-world scenario, but for this exercise, a simple Observable.create with setTimeout is sufficient to demonstrate switchMap.
  • The focus is on the correct implementation and understanding of switchMap in the context of user input and asynchronous operations.
Loading editor...
typescript