Skip to content

Commit

Permalink
add callbacks to GraphqlOfflineQueueLink
Browse files Browse the repository at this point in the history
  • Loading branch information
devj3ns committed Oct 15, 2024
1 parent 69b2692 commit 327caad
Showing 1 changed file with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,17 @@ class GraphqlOfflineQueueLink extends Link {

final GraphqlRequestSqliteCacheManager requestManager;

GraphqlOfflineQueueLink(this.requestManager)
: _logger = Logger('GraphqlOfflineQueueLink#${requestManager.databaseName}');
/// A callback triggered when a request failed, but will be reattempted.
final void Function(Request request, int statusCode)? onReattemptableResponse;

/// A callback triggered when a request throws an exception during execution.
final void Function(Request request, Object error)? onRequestError;

GraphqlOfflineQueueLink(
this.requestManager, {
this.onReattemptableResponse,
this.onRequestError,
}) : _logger = Logger('GraphqlOfflineQueueLink#${requestManager.databaseName}');

@override
Stream<Response> request(Request request, [NextLink? forward]) async* {
Expand All @@ -33,6 +42,7 @@ class GraphqlOfflineQueueLink extends Link {
yield* forward!(request).handleError(
(e) async {
_logger.warning('GraphqlOfflineQueueLink#request: error $e');
onRequestError?.call(request, e);
final db = await requestManager.getDb();
await cacheItem.unlock(db);
},
Expand All @@ -46,6 +56,8 @@ class GraphqlOfflineQueueLink extends Link {
// request was successfully sent and can be removed
_logger.finest('removing from queue: ${cacheItem.toSqlite()}');
await cacheItem.delete(db);
} else {
onReattemptableResponse?.call(request, response.response['statusCode'] as int);
}
final db = await requestManager.getDb();
await cacheItem.unlock(db);
Expand Down

0 comments on commit 327caad

Please sign in to comment.