提交 c563199d authored 作者: tdzl2003's avatar tdzl2003

support RN 0.32+

上级 46bec3b6
......@@ -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:+'
}
......@@ -7,6 +7,7 @@ import com.facebook.cache.disk.FileCache;
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;
......@@ -32,25 +33,25 @@ public class HttpCacheModule extends ReactContextBaseJavaModule {
}
@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);
}
}
......@@ -64,14 +65,14 @@ public class HttpCacheModule extends ReactContextBaseJavaModule {
}
@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();
......@@ -82,20 +83,20 @@ public class HttpCacheModule extends ReactContextBaseJavaModule {
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);
}
}
......@@ -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()]);
}
\ No newline at end of file
}
......@@ -8,6 +8,7 @@
#import "RCTHttpCache.h"
#import "RCTImageLoader.h"
#import "RCTImageCache.h"
#import "RCTBridge.h"
@implementation RCTHttpCache
......@@ -16,60 +17,28 @@
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);
}
......
{
"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": {
......@@ -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"
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论