cancel method
Cancels an ongoing HTTP request.
This method is used to cancel an ongoing HTTP request that is associated with a specific path or the current request if no path is provided. When a path is provided, it looks up the corresponding cancellation token and uses it to cancel the request. If no path is provided, it cancels the current request.
The cancellation of the request is logged for debugging purposes.
Usage: To cancel a specific request:
cancel('path/to/request');
To cancel the current request (if no other path is specified):
cancel();
Parameters:
path(optional): The path of the request to be canceled. If no path is provided, the current request is canceled.
Note: The cancellation token associated with the path (if provided) is removed from the tracking list after the cancellation.
Implementation
void cancel([String? path]) {
final token = _cancelTokens[path];
// cancel specific request
if (path != null && token != null) {
token.cancel('Request for $path is canceled');
logg('Request for $path is canceled', name: 'Fetchly');
}
// cancel current request
else if (_currentToken != null) {
_currentToken?.cancel('Request is canceled');
logg('Request for $_currentPath is canceled', name: 'Fetchly');
}
}