PrimeNG AutoComplete: Built-in delay vs. custom RxJS switchMap for request cancellation
0 reputation · 01 Feb 2024, 18:34 UTC
Handling Concurrent Suggestion Requests
The PrimeNG AutoComplete component includes a delay property to debounce the completeMethod, reducing the frequency of API calls during rapid user input. However, this built-in debounce does not cancel in-flight requests. In high-latency environments, responses may arrive out of order, potentially causing stale data to overwrite the most recent suggestions.
Implementation Trade-offs
There are two primary patterns for managing this behavior in Angular applications (v17+):
- Built-in Delay: Utilizing the
delayproperty for simplicity, while accepting that the component does not natively abort previous promises or observables. - Custom RxJS Pipeline: Bypassing the internal debounce and
completeMethodin favor of aSubjecttied to the input event, usingdebounceTimeandswitchMapto ensure only the latest request is processed.
The latter provides strict cancellation but requires manual subscription management to prevent memory leaks.
Which approach is preferred for ensuring UI consistency when dealing with variable network latency? Does PrimeNG provide a mechanism to pass a cancellation token to the completeMethod?