Skip to content

Commit

Permalink
fix: merge request options
Browse files Browse the repository at this point in the history
  • Loading branch information
zoocityboy committed Feb 9, 2021
1 parent 8e6c777 commit 9fbe2f7
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 29 deletions.
3 changes: 0 additions & 3 deletions example/ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,6 @@
/* Begin XCBuildConfiguration section */
249021D3217E4FDB00AE95B9 /* Profile */ = {
isa = XCBuildConfiguration;
baseConfigurationReference = 7AFA3C8E1D35360C0083082E /* Release.xcconfig */;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_NONNULL = YES;
Expand Down Expand Up @@ -392,7 +391,6 @@
};
97C147031CF9000F007C117D /* Debug */ = {
isa = XCBuildConfiguration;
baseConfigurationReference = 9740EEB21CF90195004384FC /* Debug.xcconfig */;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_NONNULL = YES;
Expand Down Expand Up @@ -448,7 +446,6 @@
};
97C147041CF9000F007C117D /* Release */ = {
isa = XCBuildConfiguration;
baseConfigurationReference = 7AFA3C8E1D35360C0083082E /* Release.xcconfig */;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_NONNULL = YES;
Expand Down
21 changes: 19 additions & 2 deletions lib/src/dio_cache_interceptor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class DioCacheInterceptor extends Interceptor {
}

final options = _getCacheOptions(request);
request.extra = options.toExtra();

if (options.policy == CachePolicy.refresh) {
return super.onRequest(request);
Expand Down Expand Up @@ -73,8 +74,11 @@ class DioCacheInterceptor extends Interceptor {
}

// Cache response into store
var x = _hasCacheDirectives(response);
if (cacheOptions.policy == CachePolicy.cacheStoreForce ||
((cacheOptions.policy == CachePolicy.refresh ||
cacheOptions.policy == CachePolicy.requestFirst ||
cacheOptions.policy == CachePolicy.refresh) &&
cacheOptions.forceSave == true) ||
_hasCacheDirectives(response)) {
final cacheResp = await _buildCacheResponse(
cacheOptions.keyBuilder(response.request),
Expand Down Expand Up @@ -159,7 +163,20 @@ class DioCacheInterceptor extends Interceptor {
}

CacheOptions _getCacheOptions(RequestOptions request) {
return CacheOptions.fromExtra(request) ?? _options;
var requestCacheOptions = CacheOptions.fromExtra(request);

var value = requestCacheOptions != null
? _options.copyWith(
policy: requestCacheOptions.policy,
hitCacheOnErrorExcept: requestCacheOptions.hitCacheOnErrorExcept,
maxStale: requestCacheOptions.maxStale,
priority: requestCacheOptions.priority,
store: requestCacheOptions.store,
decrypt: requestCacheOptions.decrypt,
encrypt: requestCacheOptions.encrypt,
cancelAfterDelay: requestCacheOptions.cancelAfterDelay)
: _options;
return value;
}

CacheStore _getCacheStore(CacheOptions options) {
Expand Down
53 changes: 29 additions & 24 deletions lib/src/model/cache_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ class CacheOptions {
/// Option cancel request before end after delay
final int cancelAfterDelay;

/// Option for force save after request finished
final bool forceSave;

// Key to retrieve options from request
static const _extraKey = '@cache_options@';

Expand All @@ -96,7 +99,8 @@ class CacheOptions {
this.decrypt,
this.encrypt,
this.store,
this.cancelAfterDelay = 2})
this.cancelAfterDelay = 2,
this.forceSave = false})
: assert(policy != null),
assert(keyBuilder != null),
assert(priority != null),
Expand All @@ -119,29 +123,29 @@ class CacheOptions {
return Options(extra: toExtra());
}

CacheOptions copyWith({
CachePolicy policy,
List<int> hitCacheOnErrorExcept,
CacheKeyBuilder keyBuilder,
Duration maxStale,
CachePriority priority,
CacheStore store,
Decrypt decrypt,
Encrypt encrypt,
int cancelAfterDelay,
}) {
CacheOptions copyWith(
{CachePolicy policy,
List<int> hitCacheOnErrorExcept,
CacheKeyBuilder keyBuilder,
Duration maxStale,
CachePriority priority,
CacheStore store,
Decrypt decrypt,
Encrypt encrypt,
int cancelAfterDelay,
bool forceSave}) {
return CacheOptions(
policy: policy ?? this.policy,
hitCacheOnErrorExcept:
hitCacheOnErrorExcept ?? this.hitCacheOnErrorExcept,
keyBuilder: keyBuilder ?? this.keyBuilder,
maxStale: maxStale ?? this.maxStale,
priority: priority ?? this.priority,
store: store ?? this.store,
decrypt: decrypt ?? this.decrypt,
encrypt: encrypt ?? this.encrypt,
cancelAfterDelay: cancelAfterDelay ?? this.cancelAfterDelay,
);
policy: policy ?? this.policy,
hitCacheOnErrorExcept:
hitCacheOnErrorExcept ?? this.hitCacheOnErrorExcept,
keyBuilder: keyBuilder ?? this.keyBuilder,
maxStale: maxStale ?? this.maxStale,
priority: priority ?? this.priority,
store: store ?? this.store,
decrypt: decrypt ?? this.decrypt,
encrypt: encrypt ?? this.encrypt,
cancelAfterDelay: cancelAfterDelay ?? this.cancelAfterDelay,
forceSave: forceSave ?? this.forceSave);
}

@override
Expand All @@ -152,7 +156,8 @@ class CacheOptions {
maxStale: $maxStale
priority: $priority
store: $store,
cancelAfterDelay: $cancelAfterDelay
cancelAfterDelay: $cancelAfterDelay,
forceSave: $forceSave
''';
}
}

0 comments on commit 9fbe2f7

Please sign in to comment.