packages/core/src/lib/services/queue/api-queue.service.ts
Service class for managing and processing a queue of API requests.
This service provides functionality to add, remove, process, and manage HTTP requests stored in a queue. It enables sequential or bulk processing of requests, ensuring controlled execution and handling of API calls. The service methods offer features like processing individual items, processing all items in the queue either sequentially or all at once, and removing items from the queue. Each method is designed to handle specific aspects of queue management and processing, with attention to details like order of execution and error handling.
Usage of this service is ideal in scenarios where API requests need to be managed in a queue structure, allowing for controlled execution and monitoring of these requests.
Example :// Adding a new item to the queue
apiQueueService.addQueueItem({ id: 'item124', method: 'POST', uri: '/api/data', payload: {...} });// Processing a single item from the queue apiQueueService.processQueueItem('item123').subscribe(result => { console.log('Processed item 123 with result:', result); });
// Processing all items in the queue in sequential order apiQueueService.processAllQueueItemsSequential(false).subscribe(results => { console.log('Processed all items in descending order of their timestamp:', results); });
// Removing an item from the queue apiQueueService.removeQueueItem('item123');
// Processing all items in the queue and continuing on error apiQueueService.processAllQueueItems(true).subscribe(results => { console.log('Processed all items in the queue with continuation on errors:', results); });
Methods |
constructor()
|
| addQueueItem | ||||||||||||
addQueueItem(id: string, item: ApiQueueItem)
|
||||||||||||
|
Adds an item to the queue by dispatching an action to the store.
This function is specifically designed to enqueue API call requests represented by
Parameters :
Returns :
void
This function does not return a value. It adds the item to the queue via a dispatch action. |
| getQueueItem | ||||||||
getQueueItem(id: string)
|
||||||||
|
Retrieves a specific item from the API queue by its ID. This method subscribes to the store and selects a single item from the API queue based on the provided ID.
It employs a pipeline to fetch the item: the This method is ideal for accessing a specific queue item when its ID is known, allowing for targeted operations or checks on that particular item. If no item with the given ID exists in the queue, the Observable emits
Parameters :
Returns :
Observable<ApiQueueItem>
An Observable emitting the requested ApiQueueItem.
If no item with the given ID exists in the queue, the Observable emits |
| processAllQueueItemsSequential | ||||||||||
processAllQueueItemsSequential(ascending: unknown)
|
||||||||||
Type parameters :
|
||||||||||
|
Processes all items in the queue sequentially, based on their timestamp ordering. This method first orders the items in the API queue by their timestamp, either in ascending or descending
order as specified by the The function currently returns
Parameters :
Returns :
Observable<T>
An Observable emitting the responses of the HTTP requests in the order they were processed. |
| processQueueItem | ||||||||
processQueueItem(id: string)
|
||||||||
Type parameters :
|
||||||||
|
Processes an item from the API queue identified by its ID. Retrieves the specified item from the queue and applies processing logic as defined in If the item does not exist, the Observable emits
Parameters :
Returns :
Observable<T>
An Observable that emits the result of processing the queue item.
If the item does not exist, the Observable emits |
| removeAllQueueItem |
removeAllQueueItem()
|
|
Clears all items from the API queue. This method dispatches an action to empty the entire API queue, effectively removing all items that are currently queued. It is useful for scenarios where a complete reset of the queue is required, such as during initialization or after processing a batch of items. The function does not return any value, as its primary purpose is to trigger a state change in the store. Note: Use this method with caution as it will irreversibly clear all items in the queue. Ensure that this action does not disrupt any ongoing processes that rely on the queue's contents. Example :
Returns :
void
|
| removeQueueItem | ||||||||
removeQueueItem(id: string)
|
||||||||
|
Removes a specific item from the API queue using its ID. This method dispatches an action to remove an item from the queue, identified by the provided ID. It is essential for managing the queue by eliminating items that are no longer needed or have been processed. The function does not return any value, as it primarily triggers an action within the store. Note: This method assumes the existence of the item in the queue. It does not perform a check to confirm the presence of the item before attempting to remove it. Therefore, it's recommended to verify the item's existence in the queue if uncertainty exists. Example :
Parameters :
Returns :
void
|