Skip to content

Commit

Permalink
support RN 0.32+
Browse files Browse the repository at this point in the history
  • Loading branch information
tdzl2003 committed Nov 11, 2016
1 parent 46bec3b commit c563199
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 107 deletions.
3 changes: 1 addition & 2 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,5 @@ android {

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.1'
compile "com.facebook.react:react-native:0.29.+"
compile 'com.facebook.react:react-native:+'
}
29 changes: 15 additions & 14 deletions android/src/main/java/cn/reactnative/httpcache/HttpCacheModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.imagepipeline.core.ImagePipelineFactory;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
Expand All @@ -32,25 +33,25 @@ public String getName() {
}

@ReactMethod
public void clearCache(Callback callback){
public void clearCache(Promise promise){
try {
Cache cache = OkHttpClientProvider.getOkHttpClient().cache();
if (cache != null) {
cache.delete();
}
callback.invoke();
promise.resolve(null);
} catch(IOException e){
callback.invoke(e.getMessage());
promise.reject(e);
}
}

@ReactMethod
public void getHttpCacheSize(Callback callback){
public void getHttpCacheSize(Promise promise){
try {
Cache cache = OkHttpClientProvider.getOkHttpClient().cache();
callback.invoke(null, cache != null ? ((double)cache.size()) : 0);
promise.resolve(cache != null ? ((double)cache.size()) : 0);
} catch(IOException e){
callback.invoke(e.getMessage());
promise.reject(e);
}
}

Expand All @@ -64,14 +65,14 @@ private void updateCacheSize(DiskStorageCache cache) throws NoSuchMethodExceptio
}

@ReactMethod
public void getImageCacheSize(Callback callback){
public void getImageCacheSize(Promise promise){
FileCache cache1 = ImagePipelineFactory.getInstance().getMainDiskStorageCache();
long size1 = cache1.getSize();
if (size1 < 0){
try {
updateCacheSize((DiskStorageCache)cache1);
} catch (Exception e){
callback.invoke(e.getMessage());
promise.reject(e);
return;
}
size1 = cache1.getSize();
Expand All @@ -82,20 +83,20 @@ public void getImageCacheSize(Callback callback){
try {
updateCacheSize((DiskStorageCache)cache2);
} catch (Exception e){
callback.invoke(e.getMessage());
promise.reject(e);
return;
}
size2 = cache2.getSize();
}
callback.invoke(null, ((double)(size1+size2)));
promise.resolve(((double)(size1+size2)));
}

@ReactMethod
public void clearImageCache(Callback callback){
FileCache cache1 = ImagePipelineFactory.getInstance().getMainDiskStorageCache();
public void clearImageCache(Promise promise){
FileCache cache1 = ImagePipelineFactory.getInstance().getMainFileCache();
cache1.clearAll();
FileCache cache2 = ImagePipelineFactory.getInstance().getSmallImageDiskStorageCache();
FileCache cache2 = ImagePipelineFactory.getInstance().getSmallImageFileCache();
cache2.clearAll();
callback.invoke();
promise.resolve(null);
}
}
56 changes: 9 additions & 47 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,24 @@
* Created by tdzl2_000 on 2015-12-29.
*/
import { NativeModules } from 'react-native';
import promisify from 'es6-promisify';

const native = NativeModules.HttpCache;

// Used only with promisify. Transform callback to promise result.
function translateError(err, result) {
if (!err) {
return this.resolve(result);
}
if (typeof err === 'object') {
if (err instanceof Error) {
return this.reject(ret);
}
const {message, ...other} = err;
return this.reject(Object.assign(new Error(err.message), other));
} else if (typeof err === 'string') {
return this.reject(new Error(err));
}
this.reject(Object.assign(new Error(), { origin: err }));
}

function wrapApi(nativeFunc, argCount) {
if (!nativeFunc) {
return undefined;
}
const promisified = promisify(nativeFunc, translateError);
if (argCount != undefined){
return (...args) => {
let _args = args;
if (_args.length < argCount) {
_args[argCount - 1] = undefined;
} else if (_args.length > argCount){
_args = _args.slice(0, args);
}
return promisified(..._args);
};
} else {
return () => {
return promisified();
};
}
}
export const clearHttpCache = wrapApi(native.clearCache);
export const clearHttpCache = native.clearCache;

export const getHttpCacheSize = wrapApi(native.getHttpCacheSize);
export const getHttpCacheSize = native.getHttpCacheSize;

export const clearImageCache = wrapApi(native.clearImageCache);
export const clearImageCache = native.clearImageCache;

export const getImageCacheSize = wrapApi(native.getImageCacheSize);
export const getImageCacheSize = native.getImageCacheSize;

export async function getSize(){
export async function getCacheSize(){
const arr = await Promise.all([getHttpCacheSize(), getImageCacheSize()]);

console.log(arr.join(','));
// Get sum of all cache type.
return arr.reduce((a,b)=>a+b, 0);
}

export async function clear(){
export async function clearCache(){
await Promise.all([clearHttpCache(), clearImageCache()]);
}
}
49 changes: 9 additions & 40 deletions ios/RCTHttpCache/RCTHttpCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#import "RCTHttpCache.h"
#import "RCTImageLoader.h"
#import "RCTImageCache.h"
#import "RCTBridge.h"

@implementation RCTHttpCache
Expand All @@ -16,60 +17,28 @@ @implementation RCTHttpCache

RCT_EXPORT_MODULE(HttpCache);

RCT_EXPORT_METHOD(getHttpCacheSize:(RCTResponseSenderBlock)resolve)
RCT_EXPORT_METHOD(getHttpCacheSize:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject)
{
NSURLCache *httpCache = [NSURLCache sharedURLCache];
resolve(@[[NSNull null], @([httpCache currentDiskUsage])]);
resolve(@([httpCache currentDiskUsage]));
}

RCT_EXPORT_METHOD(clearCache:(RCTResponseSenderBlock)resolve)
RCT_EXPORT_METHOD(clearCache:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject)
{
NSURLCache *httpCache = [NSURLCache sharedURLCache];
[httpCache removeAllCachedResponses];
resolve(@[[NSNull null]]);
resolve(nil);
}


RCT_EXPORT_METHOD(getImageCacheSize:(RCTResponseSenderBlock)resolve)
RCT_EXPORT_METHOD(getImageCacheSize:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject)
{
NSURLCache *imageCache = [self imageCache];
dispatch_queue_t queue = [self imageCacheQueue];
if (imageCache == nil || queue == nil) {
resolve(@[@"cache not found"]);
}
dispatch_async(queue, ^{
resolve(@[[NSNull null], @([imageCache currentDiskUsage])]);
});
resolve(@0);
}

RCT_EXPORT_METHOD(clearImageCache:(RCTResponseSenderBlock)resolve)
RCT_EXPORT_METHOD(clearImageCache:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject)
{
NSURLCache *imageCache = [self imageCache];
dispatch_queue_t queue = [self imageCacheQueue];

if (imageCache == nil || queue == nil) {
resolve(@[@"cache not found"]);
}

dispatch_async(queue, ^{
[imageCache removeAllCachedResponses];
resolve(@[[NSNull null]]);
});
}

- (NSURLCache *)imageCache
{
RCTImageLoader* loader = _bridge.imageLoader;
NSURLCache *cache = [loader valueForKey:@"_URLCache"];

return cache;
}

- (dispatch_queue_t)imageCacheQueue
{
RCTImageLoader* loader = _bridge.imageLoader;
dispatch_queue_t queue = [loader valueForKey:@"_URLCacheQueue"];
return queue;
resolve(nil);
}


Expand Down
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-http-cache",
"version": "0.1.1",
"version": "0.2.0",
"description": "Control http cache used by fetch/XMLHttpRequest",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -29,8 +29,5 @@
"eslint-config-airbnb": "^2.1.1",
"eslint-plugin-import": "^0.12.1",
"eslint-plugin-react": "^3.13.1"
},
"dependencies": {
"es6-promisify": "^3.0.0"
}
}

0 comments on commit c563199

Please sign in to comment.