Skip to content

Commit

Permalink
48 prefetch and clean cache methods in promise ways (#49)
Browse files Browse the repository at this point in the history
* feat(iOS): wip

* feat(iOS): add prefetch and clean cache methods

* refactor(iOS): prefetch and clean cache methods in promise way

* build: upgrade yarn and packages

* build: yarn version
  • Loading branch information
duguyihou authored Nov 21, 2023
1 parent 3f44380 commit 08ac4a5
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 38 deletions.
15 changes: 15 additions & 0 deletions example/ios/TurboImageExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,7 @@
GCC_PREPROCESSOR_DEFINITIONS = (
"DEBUG=1",
"$(inherited)",
_LIBCPP_ENABLE_CXX17_REMOVED_UNARY_BINARY_FUNCTION,
);
GCC_SYMBOLS_PRIVATE_EXTERN = NO;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
Expand Down Expand Up @@ -609,6 +610,11 @@
"-DFOLLY_MOBILE=1",
"-DFOLLY_USE_LIBCPP=1",
);
OTHER_LDFLAGS = (
"$(inherited)",
"-Wl",
"-ld_classic",
);
REACT_NATIVE_PATH = "${PODS_ROOT}/../../node_modules/react-native";
SDKROOT = iphoneos;
};
Expand Down Expand Up @@ -650,6 +656,10 @@
"EXCLUDED_ARCHS[sdk=iphonesimulator*]" = i386;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_NO_COMMON_BLOCKS = YES;
GCC_PREPROCESSOR_DEFINITIONS = (
"$(inherited)",
_LIBCPP_ENABLE_CXX17_REMOVED_UNARY_BINARY_FUNCTION,
);
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
GCC_WARN_UNDECLARED_SELECTOR = YES;
Expand All @@ -674,6 +684,11 @@
"-DFOLLY_MOBILE=1",
"-DFOLLY_USE_LIBCPP=1",
);
OTHER_LDFLAGS = (
"$(inherited)",
"-Wl",
"-ld_classic",
);
REACT_NATIVE_PATH = "${PODS_ROOT}/../../node_modules/react-native";
SDKROOT = iphoneos;
VALIDATE_PRODUCT = YES;
Expand Down
14 changes: 7 additions & 7 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@
},
"dependencies": {
"react": "18.2.0",
"react-native": "0.72.3"
"react-native": "0.72.7"
},
"devDependencies": {
"@babel/core": "^7.20.0",
"@babel/preset-env": "^7.20.0",
"@babel/runtime": "^7.20.0",
"@react-native/eslint-config": "^0.72.2",
"@react-native/metro-config": "^0.72.9",
"metro-react-native-babel-preset": "0.76.7",
"babel-plugin-module-resolver": "^5.0.0"
"@react-native/eslint-config": "^0.74.0",
"@react-native/metro-config": "^0.73.2",
"babel-plugin-module-resolver": "^5.0.0",
"metro-react-native-babel-preset": "0.77.0"
},
"engines": {
"node": ">=16"
"node": ">=18"
}
}
}
13 changes: 9 additions & 4 deletions ios/TurboImage/TurboImageViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,18 @@ @interface RCT_EXTERN_MODULE(TurboImageViewManager, RCTViewManager)

#pragma mark - methods

RCT_EXTERN_METHOD(prefetch:(NSArray *)urlArray)
RCT_EXTERN_METHOD(prefetch:(NSArray *)urlArray
resolve:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject)

RCT_EXTERN_METHOD(clearAllCache)
RCT_EXTERN_METHOD(clearAllCache:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject)

RCT_EXTERN_METHOD(clearMemoryCache)
RCT_EXTERN_METHOD(clearMemoryCache:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject)

RCT_EXTERN_METHOD(clearDiskCache)
RCT_EXTERN_METHOD(clearDiskCache:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject)

@end

22 changes: 14 additions & 8 deletions ios/TurboImage/TurboImageViewManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,31 @@ class TurboImageViewManager: RCTViewManager {
}

extension TurboImageViewManager {

@objc
func prefetch(_ urlArray: [String]) {
func prefetch(_ urlArray: [String],
resolve: @escaping RCTPromiseResolveBlock,
reject: @escaping RCTPromiseRejectBlock) {
let urls = urlArray.map { url in URL(string: url )}.compactMap{ $0 }
let prefetcher = ImagePrefetcher(urls: urls)
prefetcher.start()
resolve(prefetcher.start())
}

@objc
func clearAllCache() {
ImageCache.default.clearCache()
func clearAllCache(_ resolve: @escaping RCTPromiseResolveBlock,
reject: @escaping RCTPromiseRejectBlock) {
resolve(ImageCache.default.clearCache())
}

@objc
func clearMemoryCache() {
ImageCache.default.clearMemoryCache()
func clearMemoryCache(_ resolve: @escaping RCTPromiseResolveBlock,
reject: @escaping RCTPromiseRejectBlock) {
resolve(ImageCache.default.clearMemoryCache())
}

@objc
func clearDiskCache() {
ImageCache.default.clearDiskCache()
func clearDiskCache(_ resolve: @escaping RCTPromiseResolveBlock,
reject: @escaping RCTPromiseRejectBlock) {
resolve(ImageCache.default.clearDiskCache())
}
}
30 changes: 15 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,25 @@
"registry": "https://registry.npmjs.org/"
},
"devDependencies": {
"@commitlint/config-conventional": "^17.0.2",
"@commitlint/config-conventional": "^18.4.2",
"@evilmartians/lefthook": "^1.2.2",
"@react-native-community/eslint-config": "^3.0.2",
"@release-it/conventional-changelog": "^5.0.0",
"@types/jest": "^28.1.2",
"@types/react": "~17.0.21",
"@types/react-native": "0.70.0",
"commitlint": "^17.0.2",
"@release-it/conventional-changelog": "^8.0.1",
"@types/jest": "^29.5.8",
"@types/react": "18.2.37",
"@types/react-native": "0.72.6",
"commitlint": "^18.4.2",
"del-cli": "^5.0.0",
"eslint": "^8.4.1",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^4.0.0",
"jest": "^28.1.1",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-prettier": "^5.0.1",
"jest": "^29.7.0",
"pod-install": "^0.1.0",
"prettier": "^2.0.5",
"prettier": "^3.1.0",
"react": "18.2.0",
"react-native": "0.72.3",
"react-native-builder-bob": "^0.20.0",
"release-it": "^15.0.0",
"react-native": "0.72.7",
"react-native-builder-bob": "^0.23.1",
"release-it": "^17.0.0",
"turbo": "^1.10.7",
"typescript": "^5.0.2"
},
Expand All @@ -85,9 +85,9 @@
"react-native": "*"
},
"engines": {
"node": ">= 16.0.0"
"node": ">= 18.0.0"
},
"packageManager": "^yarn@1.22.15",
"packageManager": "yarn@1.22.21",
"jest": {
"preset": "react-native",
"modulePathIgnorePatterns": [
Expand Down
16 changes: 12 additions & 4 deletions src/TurboImage.ios.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,17 @@ const TurboImage = (props: TurboImageProps) => {
return <TurboImageView {...props} />;
};

TurboImage.prefetch = (urls: string[]) => TurboImageViewManager.prefetch(urls);
TurboImage.clearAllCache = () => TurboImageViewManager.clearAllCache();
TurboImage.clearMemoryCache = () => TurboImageViewManager.clearMemoryCache();
TurboImage.clearDiskCache = () => TurboImageViewManager.clearDiskCache();
TurboImage.prefetch = async (urls: string[]) => {
return await TurboImageViewManager.prefetch(urls);
};
TurboImage.clearAllCache = async () => {
return await TurboImageViewManager.clearAllCache();
};
TurboImage.clearMemoryCache = async () => {
return await TurboImageViewManager.clearMemoryCache();
};
TurboImage.clearDiskCache = async () => {
return await TurboImageViewManager.clearDiskCache();
};

export default TurboImage;

0 comments on commit 08ac4a5

Please sign in to comment.